# razgrom47/snakecpp architecture

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

- Interactive diagram: https://gitdiagram.com/razgrom47/snakecpp
- Repository: https://github.com/razgrom47/snakecpp

## Overview

##### Project Type and Purpose
- SnakeCPP is a native desktop game written in C++ that uses the SDL2 library for graphics, input, audio, and timing.
- It is a single executable, monolithic application (no client/server tiers), centered around a real-time game loop.

##### File Structure
- Top-level folders:
- assets – holds fonts and other media
- src – all C++ source files and headers
- src/include – project headers plus bundled SDL2 headers
- src/lib – static SDL2 libraries and CMake config files
- .vscode – IDE build/debug configurations
- Key code modules in src:
- main.cpp – entry point, initializes subsystems
- Game.cpp/Game.h – game loop, state management
- Snake.cpp/Snake.h – snake entity logic
- UI.cpp/UI.h – rendering and user‐input wrappers

##### Extract Insights from README
- Built on Windows 11 with MSYS2/Mingw and VS Code
- SDL2 is bundled in the include/SDL2 and lib folders
- Uses a Google font (assets/Shizuru-Regular.ttf) for on-screen text
- Build: compile main.cpp + link libSDL2*.a → snake_game.exe
- Run: execute snake_game.exe

##### Main Components
a. Game Engine Layer
- SDL2 Core (Video, Audio, Events, Timer modules)
- Static libraries (libSDL2.a, libSDL2_image.a, etc.)
b. Application Layer
- main.cpp – initialization & teardown
- Game Module – game loop, state transitions
- Snake Module – snake data structure & movement logic
- UI Module – rendering calls, event dispatching
- Resource/Asset Manager – loading fonts/images from assets
c. Assets
- Shizuru-Regular.ttf (in assets)
d. Build/IDE Configuration
- .vscode/tasks.json & launch.json (compiler and debugger settings)

##### Relationships and Data Flows
- main.cpp calls “SDL_Init()” → hands off to Game::run()
- Game::run() enters loop:
- Poll SDL2 events → pass to UI or Game for processing
- Update Snake & game state → using Constants
- Render frame → UI invokes SDL_Render*
- Delay/tick → SDL_Delay or SDL_Timer
- Resource Manager loads font via SDL_ttf
- All render calls go through SDL2 renderer subsystem

##### Architectural Patterns
- Game Loop pattern: single loop that processes input, updates state, renders
- Module separation:
- UI layer abstracts SDL calls
- Game logic is kept separate from rendering
- Snake behavior encapsulated in its own class
- Static linking of SDL2 – no dynamic load at runtime

- Version note: “SDL2 bundled in src/lib & src/include”

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Application Layer
    subgraph "Application Layer"
        direction TB
        Main["Entry Point\nmain.cpp"]:::app
        GameMod["Game Module"]:::app
        SnakeMod["Snake Module"]:::app
        UIMod["UI Module"]:::app
        Const["Configuration\nConstants.h"]:::app
        ResMgr["Resource Manager"]:::app
    end

    %% Game Engine Layer
    subgraph "Game Engine Layer"
        direction TB
        subgraph "SDL2 Core"
            direction TB
            Video["Video Renderer"]:::engine
            Events["Event Subsystem"]:::engine
            Audio["Audio Subsystem"]:::engine
            Timer["Timer"]:::engine
        end
        subgraph "SDL2 Libraries"
            direction TB
            Libs["Static Libs (.a)"]:::engine
            CMake["CMake Configs"]:::engine
            Pkg["pkgconfig (.pc)"]:::engine
        end
    end

    %% Assets
    Font["Shizuru-Regular.ttf"]:::asset

    %% Build/IDE Configuration
    subgraph "Build & IDE Config"
        direction TB
        Tasks["VSCode Tasks"]:::config
        Launch["VSCode Launch"]:::config
    end

    %% Relationships
    Main -->|"initializes SDL2"| Video
    Main -->|"calls Game::run()"| GameMod
    GameMod -->|"gameLoop"| Events
    Events -->|"pollEvent"| UIMod
    UIMod -->|"dispatchInput"| GameMod
    GameMod -->|"update()"| SnakeMod
    GameMod -->|"render()"| UIMod
    UIMod -->|"drawFrame"| Video
    UIMod -->|"playSound"| Audio
    GameMod -->|"tick"| Timer
    SnakeMod -->|"uses"| Const
    UIMod -->|"uses"| Const
    UIMod -->|"loadFont"| ResMgr
    ResMgr -->|"reads"| Font

    %% Click Events
    click Video "https://github.com/razgrom47/snakecpp/tree/main/src/include/SDL2"
    click Libs "https://github.com/razgrom47/snakecpp/blob/main/src/lib/libSDL2.a"
    click CMake "https://github.com/razgrom47/snakecpp/tree/main/src/lib/cmake/"
    click Pkg "https://github.com/razgrom47/snakecpp/tree/main/src/lib/pkgconfig/"
    click Main "https://github.com/razgrom47/snakecpp/blob/main/main.cpp"
    click GameMod "https://github.com/razgrom47/snakecpp/blob/main/src/Game.cpp"
    click GameMod "https://github.com/razgrom47/snakecpp/blob/main/src/include/Game.h"
    click SnakeMod "https://github.com/razgrom47/snakecpp/blob/main/src/Snake.cpp"
    click SnakeMod "https://github.com/razgrom47/snakecpp/blob/main/src/include/Snake.h"
    click UIMod "https://github.com/razgrom47/snakecpp/blob/main/src/UI.cpp"
    click UIMod "https://github.com/razgrom47/snakecpp/blob/main/src/include/UI.h"
    click Const "https://github.com/razgrom47/snakecpp/blob/main/src/include/Constants.h"
    click Font "https://github.com/razgrom47/snakecpp/blob/main/assets/Shizuru-Regular.ttf"
    click Tasks "https://github.com/razgrom47/snakecpp/blob/main/.vscode/tasks.json"
    click Launch "https://github.com/razgrom47/snakecpp/blob/main/.vscode/launch.json"

    %% Styles
    classDef app fill:#cce5ff,stroke:#004085
    classDef engine fill:#d4edda,stroke:#155724
    classDef asset fill:#fff3cd,stroke:#856404
    classDef config fill:#e2e3e5,stroke:#6c757d
```
