# cxong/cdogs-sdl architecture

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

- Interactive diagram: https://gitdiagram.com/cxong/cdogs-sdl
- Repository: https://github.com/cxong/cdogs-sdl

## Overview

Based on our analysis of the provided file tree and README, C-Dogs SDL is a classic overhead run‐and‐gun game ported to SDL2 that supports multiplayer (both co-op and deathmatch), custom campaigns, and cross‐platform functionality. The project is written mostly in C and organized into many modules (graphics, audio, AI, networking, game state, and even dedicated tools like a level/campaign editor in the “cdogsed” directory). The code also uses several third‐party libraries such as SDL2, enet (for networking), base64 utilities, and nanopb for protocol buffers in the “proto” directory.

##### Main Components
- Game Engine Core: This includes the game loop (see files like mainmenu.c, game_loop.c, game.c, prep.c, etc.), event processing, state management, and the integration of subsystems.
- Input Handling: Organized into keyboard.c, joystick.c, and event modules that capture (keyboard, mouse, and joystick) input and pass events to the game engine.
- Audio Subsystem: Located in modules (audio-related source files) that integrate sound effects and music (the audio modules in “src/cdogs/cwolfmap/…” and “src/cdogs/music.c”). Also note the presence of directories for sounds and music.
- AI and NPC: Modules such as ai.c, ai_context.c, and ai_utils.c that encapsulate in-game bot behavior.
- Networking: With modules like net_client.c, net_server.c, and net_util.c, this subsystem handles multiplayer networking (both cooperative and deathmatch).
- Data/Asset Management: Handling game assets such as custom missions/campaigns (seen in “data”, “missions”, and “dogfights” directories), configuration files, and utility modules like config_io.c and campaign-related modules.
- Tools and Editors: The “cdogsed” directory contains a level/campaign editor while the “proto” directory supports protocol buffers (using nanopb) for configuration and networking messages.
- External Libraries and Build Tools: The project makes use of SDL2, enet, nanopb, and others. There are also build scripts (CMakeLists.txt, Appveyor, etc.) spread throughout the tree.

##### Relationships and Interactions
- Central Game Loop: At the heart of the application is the game loop (in files like game_loop.c) that sequentially processes input, updates game state (including AI and physics), and triggers rendering calls.
- Input and Event Handling: User input from devices is captured and passed to the game loop for updating state and triggering audio feedback.
- Network and Multiplayer: The networking modules interact with the core engine by sending and receiving game state updates between clients and server.
- Data Management: Game assets (campaigns, maps, characters, graphics) are loaded from external files (organized in “data”, “missions”, “graphics”, etc.) and parsed by the engine (e.g., via JSON or custom binary formats).
- Tools as Separate Applications: The Editor (cdogsed) and campaign conversion tools run as independent applications that share common code with the game (using similar libraries and modules for configuration and asset management).

##### Architectural Patterns and Design Principles
- Modular Separation: The project is organized into independent modules that encapsulate functionality. This provides a clear separation between core game logic, rendering, input, and auxiliary services (like networking and configuration).
- Event-Driven Design: The game loop reacts to input and game events, propagating changes across subsystems in a sequential and well-defined order.
- Platform Abstraction: The use of SDL2 abstracts platform-specific details (keyboard, joystick, audio, video) so that the same core code can run on Windows, Linux, and macOS.
- Data-Driven Customization: Custom campaigns, assets, and configuration files allow for extensibility and modding, which is central to the game’s design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Handling Subgraph
    subgraph "Input Handling"
        A["Keyboard"]:::internal
        B["Joystick"]:::internal
        C["Mouse"]:::internal
        D["Input Manager"]:::internal
        A --> D
        B --> D
        C --> D
    end

    %% Core Game Engine
    D --> E["Game Engine Core"]:::internal

    %% Rendering/Graphics Subgraph
    subgraph "Rendering/Graphics Engine"
        F["Rendering Modules"]:::internal
        G["Graphics Assets"]:::internal
        F --> G
    end
    E --> F

    %% Audio Subsystem Subgraph
    subgraph "Audio Subsystem"
        H["Music Module"]:::internal
        I["Audio Modules"]:::internal
        H --> I
    end
    G --> H

    %% AI and NPC Subgraph
    subgraph "AI & NPC"
        J1["AI"]:::internal
        J2["AI Context"]:::internal
        J3["AI Utils"]:::internal
        J1 --> J2
        J2 --> J3
    end
    I --> J1

    %% Networking Subgraph (bidirectional with Game Engine Core)
    subgraph "Networking"
        K["Net Client"]:::internal
        L["Net Server"]:::internal
        M["Net Util"]:::internal
        N["Enet Integration"]:::internal
        K --> L
        L --> M
        M --> N
    end
    E <==> K

    %% Data/Asset Management Subgraph
    subgraph "Data/Asset Management"
        O["Data Assets"]:::internal
        P["Missions"]:::internal
        Q["Dogfights"]:::internal
        R["Config IO"]:::internal
        S["Config"]:::internal
        O --> P
        P --> Q
        Q --> R
        R --> S
    end
    O --> E

    %% Tools and Editors Subgraph
    subgraph "Tools & Editors"
        T["cdogsed Editor"]:::internal
        U["Protocol Buffers"]:::internal
        T --> U
    end
    T --> O

    %% External Libraries & Build Tools Subgraph
    subgraph "External Libs & Build Tools"
        V["Base64"]:::external
        W["Enet Library"]:::external
        X["Nanopb"]:::external
        Y["CMake Scripts"]:::build
        V --> E
        W --> K
        X --> E
        Y --> E
    end

    %% Click Events for Input Handling
    click A "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/keyboard.c"
    click B "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/joystick.c"
    click C "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/mouse.c"
    
    %% Click Event for Game Engine Core
    click E "https://github.com/cxong/cdogs-sdl/blob/master/src/game.c"

    %% Click Events for Rendering/Graphics Engine
    click F "https://github.com/cxong/cdogs-sdl/tree/master/src/cdogs/draw"
    click G "https://github.com/cxong/cdogs-sdl/tree/master/graphics/"

    %% Click Events for Audio Subsystem
    click H "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/music.c"
    click I "https://github.com/cxong/cdogs-sdl/tree/master/src/cdogs/cwolfmap"

    %% Click Events for AI & NPC
    click J1 "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/ai.c"
    click J2 "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/ai_context.c"
    click J3 "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/ai_utils.c"

    %% Click Events for Networking
    click K "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/net_client.c"
    click L "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/net_server.c"
    click M "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/net_util.c"
    click N "https://github.com/cxong/cdogs-sdl/tree/master/src/cdogs/enet"

    %% Click Events for Data/Asset Management
    click O "https://github.com/cxong/cdogs-sdl/tree/master/data/"
    click P "https://github.com/cxong/cdogs-sdl/tree/master/missions/"
    click Q "https://github.com/cxong/cdogs-sdl/tree/master/dogfights/"
    click R "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/config_io.c"
    click S "https://github.com/cxong/cdogs-sdl/blob/master/src/cdogs/config.c"

    %% Click Events for Tools & Editors
    click T "https://github.com/cxong/cdogs-sdl/tree/master/src/cdogsed"
    click U "https://github.com/cxong/cdogs-sdl/tree/master/src/proto"

    %% Click Events for External Libs & Build Tools
    click V "https://github.com/cxong/cdogs-sdl/tree/master/src/base64"
    click W "https://github.com/cxong/cdogs-sdl/tree/master/src/cdogs/enet"
    click X "https://github.com/cxong/cdogs-sdl/tree/master/src/proto/nanopb"
    click Y "https://github.com/cxong/cdogs-sdl/blob/master/CMakeLists.txt"

    %% Styles
    classDef internal fill:#B3D7FF,stroke:#1F78C1,stroke-width:2px;
    classDef external fill:#C1E1C1,stroke:#2E8B57,stroke-width:2px;
    classDef build fill:#FFECB3,stroke:#FFA500,stroke-width:2px;
```
