# orangeduck/corange architecture

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

- Interactive diagram: https://gitdiagram.com/orangeduck/corange
- Repository: https://github.com/orangeduck/corange

## Overview

##### Project Type and Purpose
- This is Corange, a pure-C, cross-platform game engine library built on SDL2 and OpenGL (with optional OpenCL interop).
- It is not a standalone game but a framework providing core subsystems (asset loading, rendering, audio, physics, UI, entities, data structures) plus a set of demos that consume the library to showcase features.

##### File Structure Analysis
- Top‐level:
- /       → Public headers for each subsystem (cengine, cgraphics, caudio, cphysics, cui, cnet, plus asset, entity, UI, data and rendering subfolders).
- src/           → Library implementation mirroring the include/ hierarchy.
- assets_core/   → Shared engine assets: fonts, shaders, textures, objects, UI skins, LUTs, water data.
- demos/         → Multiple demo executables (rendering, metaballs, noise, platformer, sea, scotland, tessellation, UI, physics, particles, parallax, teapot). Each has its own Makefile, source, assets.
- Makefile, README.md, LICENSE.md.

##### Key Insights from README
- Corange offers: deferred rendering, post‐effects, SSAO, shadows, skeletal animation, asset/entity/UI managers, terrain, OpenCL support, file loaders, math utilities.
- Demos illustrate how client code initializes Corange (`corange_init`), drives the main loop, sets up scenes, and renders with different techniques.

##### Components
a. External Dependencies
- SDL2 (window, input)
- SDL_Mixer (audio)
- SDL_Net (network)
- OpenGL (graphics API)
- OpenCL (compute kernels in some demos)
b. Core Engine Library (static or shared)
- Initialization & Shutdown (corange_init / corange_finish)
- Subsystems (each a module in include/ + src/):
- Asset Manager
- Entity Manager
- UI Manager
- Rendering Subsystem
– Deferred Renderer
– Shader Pipeline
– Sky, Water, Post-Effects
- Audio Subsystem
- Physics Subsystem
- Networking Subsystem
- Input / Joystick Subsystem
- Data Structures (lists, hashtables, splines, random)
c. Demos (clients)
- Each demo links against the engine core
- They supply their own assets and specialized initialization (e.g. load shaders, set up scene graph, dispatch OpenCL kernels)
d. Data Flow / Interactions
- Main Loop: Poll SDL events → UI Manager → Entity updates (physics, scripts) → Asset requests → Rendering → Audio updates → Networking
- Physics Subsystem updates Entity transforms (collision detection).
- Audio Subsystem mixes sound assets from Asset Manager and outputs via SDL_Mixer.
- Networking Subsystem uses SDL_Net to send/receive data, updating Entities or game state.
- Optional: OpenCL kernels (in metaballs demo) fetch entity data, compute volume or particles, return to GPU via OpenGL interop.

##### Architectural Patterns and Principles
- Layered Architecture:
- External APIs (SDL2, GL, CL) at the bottom
- Core Engine Modules in the middle
- Demos (applications) on top
- Manager/Facade Pattern: Asset, Entity, UI managers provide centralized access and lifecycle.
- Plugin‐like modularity: each subsystem lives in its own folder, clean separation via header files.
- Data‐driven: assets (shaders, textures, models) stored on disk under assets_core and per‐demo asset folders.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External APIs
    subgraph "External APIs"
        direction TB
        SDL2["SDL2 (Window/Input)"]:::external
        SDLMixer["SDL_Mixer (Audio)"]:::external
        SDLN{"SDL_Net (Network)"}:::external
        OpenGL["OpenGL (Graphics API)"]:::external
        OpenCL["OpenCL (Compute)"]:::external
    end

    %% Engine Core
    subgraph "Corange Engine" 
        direction TB
        Init["Initialization & Shutdown"]:::engine
        Asset["Asset Manager"]:::engine
        Entity["Entity Manager"]:::engine
        UI["UI Manager"]:::engine
        Rendering["Rendering Subsystem"]:::engine
        Audio["Audio Subsystem"]:::engine
        Physics["Physics Subsystem"]:::engine
        Network["Networking Subsystem"]:::engine
        InputJS["Input/Joystick Subsystem"]:::engine
        DS["Data Structures"]:::engine
    end

    %% Client Demos
    subgraph "Demo Applications" 
        direction TB
        D1["metaballs"]:::client
        D2["noise"]:::client
        D3["parallax"]:::client
        D4["particles"]:::client
        D5["physics"]:::client
        D6["platformer"]:::client
        D7["rendering"]:::client
        D8["scotland"]:::client
        D9["sea"]:::client
        D10["teapot"]:::client
        D11["tessellation"]:::client
        D12["ui"]:::client
    end

    %% Demo to Engine arrows
    D1 -->|Initialize| Init
    D2 -->|Initialize| Init
    D3 -->|Initialize| Init
    D4 -->|Initialize| Init
    D5 -->|Initialize| Init
    D6 -->|Initialize| Init
    D7 -->|Initialize| Init
    D8 -->|Initialize| Init
    D9 -->|Initialize| Init
    D10 -->|Initialize| Init
    D11 -->|Initialize| Init
    D12 -->|Initialize| Init

    D1 -->|Load Assets| Asset
    D2 -->|Load Assets| Asset
    D3 -->|Load Assets| Asset
    D4 -->|Load Assets| Asset
    D5 -->|Load Assets| Asset
    D6 -->|Load Assets| Asset
    D7 -->|Load Assets| Asset
    D8 -->|Load Assets| Asset
    D9 -->|Load Assets| Asset
    D10 -->|Load Assets| Asset
    D11 -->|Load Assets| Asset
    D12 -->|Load Assets| Asset

    %% Main Loop Interactions
    Init -->|Start Loop| UI
    UI -->|UI events| InputJS
    InputJS -->|Input data| Entity
    Entity -->|Update Entities| Physics
    Physics -->|Transforms| Entity
    Entity -->|Fetch scene graph| Rendering
    Asset -->|mesh/texture lookup| Rendering
    Rendering -->|draw calls| OpenGL
    Rendering -->|optional interop| OpenCL
    Asset -->|audio buffers| Audio
    Audio -->|play sounds| SDLMixer
    Network -->|network packet| SDLN

    %% UI Rendering
    UI -->|UI draw calls| Rendering

    %% Class Definitions
    classDef external fill:#d3d3d3,stroke:#888,stroke-width:1px
    classDef engine fill:#add8e6,stroke:#000,stroke-width:1px
    classDef client fill:#90ee90,stroke:#000,stroke-width:1px

    %% Click Events for Component Mapping
    click Init "https://github.com/orangeduck/corange/blob/master/include/cengine.h"
    click Init "https://github.com/orangeduck/corange/blob/master/src/cengine.c"
    click Init "https://github.com/orangeduck/corange/blob/master/include/corange.h"
    click Init "https://github.com/orangeduck/corange/blob/master/src/corange.c"
    click Asset "https://github.com/orangeduck/corange/tree/master/include/assets/"
    click Asset "https://github.com/orangeduck/corange/tree/master/src/assets/"
    click Entity "https://github.com/orangeduck/corange/blob/master/include/centity.h"
    click Entity "https://github.com/orangeduck/corange/blob/master/src/centity.c"
    click Entity "https://github.com/orangeduck/corange/tree/master/include/entities/"
    click Entity "https://github.com/orangeduck/corange/tree/master/src/entities/"
    click UI "https://github.com/orangeduck/corange/blob/master/include/cui.h"
    click UI "https://github.com/orangeduck/corange/blob/master/src/cui.c"
    click UI "https://github.com/orangeduck/corange/tree/master/include/ui/"
    click UI "https://github.com/orangeduck/corange/tree/master/src/ui/"
    click Rendering "https://github.com/orangeduck/corange/blob/master/include/cgraphics.h"
    click Rendering "https://github.com/orangeduck/corange/blob/master/src/cgraphics.c"
    click Rendering "https://github.com/orangeduck/corange/blob/master/include/rendering/renderer.h"
    click Rendering "https://github.com/orangeduck/corange/blob/master/src/rendering/renderer.c"
    click Rendering "https://github.com/orangeduck/corange/blob/master/include/rendering/sky.h"
    click Rendering "https://github.com/orangeduck/corange/blob/master/src/rendering/sky.c"
    click Audio "https://github.com/orangeduck/corange/blob/master/include/caudio.h"
    click Audio "https://github.com/orangeduck/corange/blob/master/src/caudio.c"
    click Physics "https://github.com/orangeduck/corange/blob/master/include/cphysics.h"
    click Physics "https://github.com/orangeduck/corange/blob/master/src/cphysics.c"
    click Network "https://github.com/orangeduck/corange/blob/master/include/cnet.h"
    click Network "https://github.com/orangeduck/corange/blob/master/src/cnet.c"
    click InputJS "https://github.com/orangeduck/corange/blob/master/include/cjoystick.h"
    click InputJS "https://github.com/orangeduck/corange/blob/master/src/cjoystick.c"
    click DS "https://github.com/orangeduck/corange/tree/master/include/data/"
    click DS "https://github.com/orangeduck/corange/tree/master/src/data/"
    click D1 "https://github.com/orangeduck/corange/tree/master/demos/metaballs/"
    click D2 "https://github.com/orangeduck/corange/tree/master/demos/noise/"
    click D3 "https://github.com/orangeduck/corange/tree/master/demos/parallax/"
    click D4 "https://github.com/orangeduck/corange/tree/master/demos/particles/"
    click D5 "https://github.com/orangeduck/corange/tree/master/demos/physics/"
    click D6 "https://github.com/orangeduck/corange/tree/master/demos/platformer/"
    click D7 "https://github.com/orangeduck/corange/tree/master/demos/rendering/"
    click D8 "https://github.com/orangeduck/corange/tree/master/demos/scotland/"
    click D9 "https://github.com/orangeduck/corange/tree/master/demos/sea/"
    click D10 "https://github.com/orangeduck/corange/tree/master/demos/teapot/"
    click D11 "https://github.com/orangeduck/corange/tree/master/demos/tessellation/"
    click D12 "https://github.com/orangeduck/corange/tree/master/demos/ui/"
```
