# daverlon/candy2d architecture

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

- Interactive diagram: https://gitdiagram.com/daverlon/candy2d
- Repository: https://github.com/daverlon/candy2d

## Overview

##### Project Type and Purpose
- This project is a 2D game engine intended to simplify game development.
- It makes use of SDL2 (and its related libraries) and is designed around the ECS pattern—with entities holding components and systems operating on them—to provide a flexible, modular approach for game development.

##### Main Components
- Core Game Loop – represented by the src/main.cpp, src/Game.cpp, and src/Game.h files. This component initializes the engine, processes game state, and drives the update-render loop.
- ECS Architecture – Break this down into three main groups:
– Entities: Represented in src/GameClasses/Entity.h and src/GameClasses/EntityManager.h.
– Components: Include various components like Animator, Collider, EnemyAI, Player, SkullBullet, Sprite, Tilemap, Transform (found under src/Components) as well as UI components (under src/UI).
– Systems: Found in src/Systems (e.g., AnimatorSystem, ColliderSystem, etc.). Each manages a specific aspect of the game such as rendering (SpriteSystem), collision detection, enemy AI, and more.
- Scenes – Represented by files in src/Scenes (BaseScene, MainScene). These control what is active during different parts of the game.
- Game Classes and Utilities – Other supporting modules such as GameCamera, GameTime, GameViewPort (src/GameClasses) and utility functions (src/Utils) that handle debugging and math.
- External Resources and Setup – The dependencies folder (with the setup script), compile scripts, and bin directory output illustrate how the engine is built and run.

##### Relationships and Interactions
- That the main game loop (src/main.cpp) initializes the game and enters a loop, which continuously delegates tasks to various systems.
- That the EntityManager handles the creation and lifecycle of entities, which aggregate one or more components.
- Systems (like AnimatorSystem, PlayerSystem, ColliderSystem, etc.) query and manipulate these entities based on the components they hold.
- Scenes determine which entities and systems are active at any given moment, controlling rendering and input flow.
- Also, note that the UI components are managed separately within the UI module to maintain a clear separation from game logic.

##### Key Architectural Patterns and Design Principles
- The separation between game logic (e.g., systems and components) and rendering/input (handled maybe by SDL2 via Game and UI subsystems).
- The modular approach (each system/component is designed to be extensible and replaceable).

##### Specific Elements
– Core Engine/Bootstrapping (Game.cpp, main.cpp)
– ECS elements (Components, Entities, Systems)
– UI layers and support utilities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Engine & Game Loop
    subgraph "Core Engine & Game Loop"
        A1("src/main.cpp"):::core
        A2("src/Game.cpp"):::core
        A3("src/Game.h"):::core
    end

    %% ECS Architecture
    subgraph "ECS Architecture"
        subgraph "Entity Management"
            B1("Entity.h"):::ecs
            B2("EntityManager.h"):::ecs
        end
        subgraph "Components (Game Logic)"
            C1("Components Directory"):::ecs
        end
        subgraph "Systems"
            D1("Systems Directory"):::ecs
        end
    end

    %% UI & Scenes
    subgraph "UI & Scenes"
        subgraph "UI Components"
            E1("UI Directory"):::ui
        end
        subgraph "Scenes Management"
            F1("Scenes Directory"):::ui
        end
    end

    %% Game Classes & Utilities
    subgraph "Game Classes & Utilities"
        subgraph "Game Classes"
            G1("GameCamera, GameTime, GameViewPort"):::util
        end
        subgraph "Utilities"
            G2("Utils Directory"):::util
        end
    end

    %% External Resources & Setup
    subgraph "External Resources & Setup"
        H1("dependencies/setup"):::external
        H2("compile_flags.txt"):::external
        H3("bin Directory"):::external
    end

    %% Relationships and Interactions
    A1 -->|"init_ECS"| B2
    A1 -->|"triggers_update"| D1
    B2 -->|"manages"| C1
    D1 -->|"operates_on"| B2
    F1 -->|"controls_state"| A1
    E1 -->|"renders_UI"| D1
    A3 -->|"uses_GameClasses"| G1
    A3 -->|"uses_Utils"| G2
    H1 -->|"provides_dependencies"| A1
    H2 -->|"provides_build_flags"| A1
    H3 -->|"provides_resources"| A1

    %% Click Events
    click A1 "https://github.com/daverlon/candy2d/blob/main/src/main.cpp"
    click A2 "https://github.com/daverlon/candy2d/blob/main/src/Game.cpp"
    click A3 "https://github.com/daverlon/candy2d/blob/main/src/Game.h"
    click B1 "https://github.com/daverlon/candy2d/blob/main/src/GameClasses/Entity.h"
    click B2 "https://github.com/daverlon/candy2d/blob/main/src/GameClasses/EntityManager.h"
    click C1 "https://github.com/daverlon/candy2d/tree/main/src/Components"
    click D1 "https://github.com/daverlon/candy2d/tree/main/src/Systems"
    click E1 "https://github.com/daverlon/candy2d/tree/main/src/UI"
    click F1 "https://github.com/daverlon/candy2d/tree/main/src/Scenes"
    click G1 "https://github.com/daverlon/candy2d/tree/main/src/GameClasses"
    click G2 "https://github.com/daverlon/candy2d/tree/main/src/Utils"
    click H1 "https://github.com/daverlon/candy2d/tree/main/dependencies/setup"
    click H2 "https://github.com/daverlon/candy2d/blob/main/compile_flags.txt"
    click H3 "https://github.com/daverlon/candy2d/tree/main/bin Directory"

    %% Styles
    classDef core fill:#ffcccc,stroke:#990000,stroke-width:2px;
    classDef ecs fill:#ccffcc,stroke:#339900,stroke-width:2px;
    classDef ui fill:#ccccff,stroke:#333399,stroke-width:2px;
    classDef util fill:#ffffcc,stroke:#999900,stroke-width:2px;
    classDef external fill:#e6e6e6,stroke:#666666,stroke-width:2px;
```
