# trung24012001/opengl_gravity_duck architecture

> Architecture diagram of the GitHub repository trung24012001/opengl_gravity_duck, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/trung24012001/opengl_gravity_duck
- Repository: https://github.com/trung24012001/opengl_gravity_duck

## Overview

##### Project type and purpose
- This is a stand-alone 2D platformer game (“Gravity Duck”) written in C++ using OpenGL for graphics and SDL2/SDL2_mixer for audio.
- It reads plain-text map files, loads sprites and sounds, runs a game loop to process input, update game logic (gravity, collisions, enemies), render frames, and play audio.

##### File structure
- Top-level source files (main.cpp, game.cpp, menu.cpp, move.cpp, sound.cpp, aFunc.cpp) contain the application entry point, game loop, input handling, rendering calls, audio calls, and utility functions.
- “cPlayer.cpp, cEnemy.cpp, cBullet.cpp, cSwitch.cpp” implement entity classes and their behaviors.
- Resource folders:
- Images/ (textures, sprite sheets organized by Enemies, Game, Menu)
- Sounds/ (background music and effects)
- Maps/ (.txt files describing level layout, switches, enemy placement)
- Build artifacts and scripts: Makefile.win, Dev C++ project file (Gravity_Duck.dev, .layout).

##### Key insights from README
- Dependencies and linker flags (OpenGL, GLU, GLUT, SDL2, SDL2_mixer)
- File format (duck spawn, goal, tile grid, switches, enemies)
- Of enemy types and their parameters
- Single-binary Win32/Ubuntu builds, no client-server split

##### Components
a. Main modules
1. Application Entry & Initialization (main.cpp)
2. Game Loop (game.cpp)
3. Input Manager (SDL2 event polling)
4. Resource Manager / Map Loader (parses Maps/*.txt, loads textures/sounds)
5. Entity System (cPlayer, cEnemy, cBullet, cSwitch classes)
6. Physics & Collision (gravity direction, tile collisions, switch logic)
8. Audio Manager (sound.cpp using SDL2_mixer)
9. Menu & UI (menu.cpp)
10. Utility Functions (aFunc.cpp, move.cpp helper routines)
b. External libraries
– OpenGL (graphics pipeline)
– GLU/GLUT (window/context creation, input hookup)
– SDL2 (input, window management)
– SDL2_mixer (audio playback)
c. Data stores
– Map files (Maps/*.txt)
– Image assets (Images/)
– Sound assets (Sounds/)

##### Relationships and data flows
- Startup → initialize SDL2, OpenGL, audio
- Game Loop:
- Input Manager → reads keyboard/joystick events → pushes to Game Logic
- Resource Manager → provides textures, sound buffers, map data
- Game Logic → updates Entities (position, velocity, collisions)
- Renderer ← draws current game state
- Audio Manager ← plays background/stage music and effects
- Menu component branches off from Game Loop on pause/start

##### Architectural patterns and design principles
- Modular separation: Input, Logic, Rendering, Audio in distinct modules
- Entity-component style: each game object (player, enemy, bullet, switch) has its own class and update/render methods
- Single-process, event-driven game loop
- Resource centralization: one loader for all assets to avoid duplication

##### Final checklist
- All major modules are present and labeled
- Resource inputs (Maps, Images, Sounds) are clearly linked
- External libraries are shown

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Modules
    subgraph "Core Modules"
        Entry["Application Entry & Initialization"]:::core
        Loop["Game Loop"]:::core
        Input["Input Manager"]:::core
        Resource["Resource Manager / Map Loader"]:::core
        subgraph "Entity System"
            Player["cPlayer.cpp"]:::core
            Enemy["cEnemy.cpp"]:::core
            Bullet["cBullet.cpp"]:::core
            Switch["cSwitch.cpp"]:::core
        end
        Physics["Physics & Collision"]:::core
        Renderer["Renderer"]:::core
        Audio["Audio Manager"]:::core
        Menu["Menu & UI"]:::core
        subgraph "Utility Functions"
            AFunc["aFunc.cpp"]:::core
        end
    end

    %% Data Stores
    subgraph "Data Stores"
        Maps["Maps"]:::data
        Images["Images"]:::data
        SoundsAsset["Sounds"]:::data
    end

    %% External Libraries
    subgraph "External Libraries"
        SDL["SDL2"]:::external
        OpenGL["OpenGL/GLUT/GLU"]:::external
        Mixer["SDL2_mixer"]:::external
    end

    %% Connections
    Entry -->|init SDL2| SDL
    Entry -->|init OpenGL| OpenGL
    Entry -->|init SDL2_mixer| Mixer
    Entry -->|start loop| Loop

    Loop -->|poll events| Input
    Loop -->|load assets| Resource
    Loop -->|update entities| Player
    Loop -->|update entities| Enemy
    Loop -->|update entities| Bullet
    Loop -->|update entities| Switch
    Loop -->|physics update| Physics
    Loop -->|render frame| Renderer
    Loop -->|play audio| Audio
    Loop -->|invoke menu| Menu

    Input -->|events| Loop

    Resource -->|reads| Maps
    Resource -->|reads| Images
    Resource -->|reads| SoundsAsset
    Resource -->|provides data| Player
    Resource -->|provides data| Enemy
    Resource -->|provides data| Bullet
    Resource -->|provides data| Switch
    Resource -->|provides textures| Renderer
    Resource -->|provides audio| Audio

    Player -->|state| Physics
    Enemy -->|state| Physics
    Bullet -->|state| Physics
    Switch -->|state| Physics
    Physics -->|collision info| Player
    Physics -->|collision info| Enemy
    Physics -->|collision info| Bullet
    Physics -->|collision info| Switch

    Player -->|draw calls| Renderer
    Enemy -->|draw calls| Renderer
    Bullet -->|draw calls| Renderer
    Switch -->|draw calls| Renderer

    Player -->|sound triggers| Audio
    Enemy -->|sound triggers| Audio
    Bullet -->|sound triggers| Audio
    Switch -->|sound triggers| Audio

    SDL --> Input
    OpenGL --> Renderer
    Mixer --> Audio

    AFunc -->|helpers| Physics
    AFunc -->|helpers| Resource
    AFunc -->|helpers| Loop

    %% Legend
    subgraph Legend
        C[Core Modules]:::core
        D[Data Stores]:::data
        E[External Libraries]:::external
    end

    %% Click Events
    click Entry "https://github.com/trung24012001/opengl_gravity_duck/blob/master/main.cpp"
    click Loop "https://github.com/trung24012001/opengl_gravity_duck/blob/master/game.cpp"
    click Input "https://github.com/trung24012001/opengl_gravity_duck/blob/master/game.cpp"
    click Maps "https://github.com/trung24012001/opengl_gravity_duck/tree/master/Maps/"
    click Images "https://github.com/trung24012001/opengl_gravity_duck/tree/master/Images/"
    click SoundsAsset "https://github.com/trung24012001/opengl_gravity_duck/tree/master/Sounds/"
    click Player "https://github.com/trung24012001/opengl_gravity_duck/blob/master/cPlayer.cpp"
    click Enemy "https://github.com/trung24012001/opengl_gravity_duck/blob/master/cEnemy.cpp"
    click Bullet "https://github.com/trung24012001/opengl_gravity_duck/blob/master/cBullet.cpp"
    click Switch "https://github.com/trung24012001/opengl_gravity_duck/blob/master/cSwitch.cpp"
    click Physics "https://github.com/trung24012001/opengl_gravity_duck/blob/master/move.cpp"
    click Renderer "https://github.com/trung24012001/opengl_gravity_duck/blob/master/game.cpp"
    click Audio "https://github.com/trung24012001/opengl_gravity_duck/blob/master/sound.cpp"
    click Menu "https://github.com/trung24012001/opengl_gravity_duck/blob/master/menu.cpp"
    click AFunc "https://github.com/trung24012001/opengl_gravity_duck/blob/master/aFunc.cpp"

    %% Styles
    classDef core fill:#D0E6FF,stroke:#0366D6,stroke-width:1px;
    classDef data fill:#E6FFDD,stroke:#28A745,stroke-width:1px;
    classDef external fill:#FFE6CC,stroke:#D2691E,stroke-width:1px;
```
