# phatcao1205/flappy-nyan-sdl2 architecture

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

- Interactive diagram: https://gitdiagram.com/phatcao1205/flappy-nyan-sdl2
- Repository: https://github.com/phatcao1205/flappy-nyan-sdl2

## Overview

##### Project Type & Purpose
- This is a C++ game project that recreates the mechanics of Flappy Bird but with a Nyan Cat theme. It is built as a desktop game using the SDL2 graphics library.
- The game features core mechanics (character physics, collision detection, scoring, and game state management), animated graphics, and audio playback.

##### Main Components to Model
a. Game Engine / Main Module:
- Represented by src/main.cpp and src/Game.cpp.
- Acts as the central orchestrator that sets up the game loop, initializes SDL2, loads resources, and manages overall game state transitions (Menu, Playing, Game Over, Pause, etc).

b. Game Logic & Entity Modules:
- Bird Module: (include/Bird.h, src/Bird.cpp) encapsulating the player character behavior, movement (gravity and jump dynamics), and animation handling.
- Pipe Module: (include/Pipe.h, src/Pipe.cpp) for handling obstacles, generating pipes with a gap, and managing their movement across the screen.
- Background Module: (include/background.h; src/background.cpp) to manage the scrolling backgrounds and parallax effects.
- Menu Module: (include/menu.h; src/menu.cpp) to handle UI components for various game states (Menu, Pause, Info screens, etc).
- Score Module: (include/score.h; src/score.cpp) to manage in-game scoring, display of scores using imagery (digits), and updating high score records saved in highScore.txt.

c. Audio Manager:
- Sound Module: (include/sound.h; src/sound.cpp) handling sound effects and background music (assets in sounds directory).

d. Resource Management:
- Assets Management for loading images and other graphics (assets directory) and sounds (sounds directory).
- File I/O for persistent high score storage (include/highScore.txt).

##### Relationships and Interactions
- The Main Module initializes all subsystems (graphics, audio, input).
- The Game Engine interacts with the Bird, Pipe, and Background modules to update their states each frame.
- The Menu and Score components are used during the different game state screens (Start, Game Over, Pause).
- The Sound module is invoked by other modules (e.g., Bird jumps, collision detection) to play the appropriate effects.
- Input events (keyboard and mouse) are processed by the main loop and key dispatches go to change game states or trigger actions in the game logic.
- File I/O is used at startup and when scoring updates occur (reading/writing highScore.txt).

##### Important Architectural Patterns or Design Principles
- Modular design: Each game component (Bird, Pipe, Menu, Score, Sound, etc.) is encapsulated in its own module.
- State Machine: The game state transitions (Menu, Playing, Game Over, Pause, etc.) are clearly managed; this should be highlighted as controlling the flow of the game.
- Separation of Concerns: Rendering, collision logic, audio, and input are split across different modules.

##### Elements

- At the center: the Game Engine (from main.cpp and Game.cpp).
- Surrounding the engine: the core gameplay modules (Bird, Pipe, Background) that update per frame and interact with the engine.
- Off to one side: UI components, including Menu and Score modules, which are invoked based on game state.
- At the bottom or periphery: the Resource Manager area that handles assets (graphics and sounds) and file I/O for high scores.
- Audio Module: depicted as a connected service that receives events from various modules for triggering sound effects.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Core Engine
    subgraph "Core Engine"
        GE["Game Engine"]:::engine
    end

    %% Gameplay Modules
    subgraph "Gameplay Modules"
        B["Bird Module"]:::gameplay
        P["Pipe Module"]:::gameplay
        BG["Background Module"]:::gameplay
    end

    %% UI Modules
    subgraph "UI Modules"
        M["Menu Module"]:::ui
        S["Score Module"]:::ui
    end

    %% Auxiliary Services
    subgraph "Auxiliary Services"
        AM["Audio Manager"]:::audio
        RA["Asset Management"]:::asset
        HS["High Score Storage"]:::storage
    end

    %% Relationships
    GE -->|"initializes"| B
    GE -->|"initializes"| P
    GE -->|"initializes"| BG
    GE -->|"triggers"| M
    GE -->|"triggers"| S
    GE -->|"loads"| RA

    B -->|"playsSound"| AM
    P -->|"triggersSound"| AM

    S -->|"read/write"| HS

    %% Styles
    classDef engine fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef gameplay fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef ui fill:#FFD700,stroke:#000,stroke-width:2px;
    classDef audio fill:#DA70D6,stroke:#000,stroke-width:2px;
    classDef asset fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef storage fill:#FFFACD,stroke:#000,stroke-width:2px;

    %% Click Events for Component Mapping
    click GE "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/src/main.cpp"
    click GE "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/src/Game.cpp"

    click B "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/include/Bird.h"
    click B "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/src/Bird.cpp"

    click P "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/include/Pipe.h"
    click P "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/src/Pipe.cpp"

    click BG "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/include/background.h"
    click BG "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/src/background.cpp"

    click M "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/include/menu.h"
    click M "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/src/menu.cpp"

    click S "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/include/score.h"
    click S "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/src/score.cpp"
    click S "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/include/highScore.txt"

    click AM "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/include/sound.h"
    click AM "https://github.com/phatcao1205/flappy-nyan-sdl2/blob/main/src/sound.cpp"
    click AM "https://github.com/phatcao1205/flappy-nyan-sdl2/tree/main/sounds"

    click RA "https://github.com/phatcao1205/flappy-nyan-sdl2/tree/main/assets"
```
