# scallyw4g/bonsai architecture

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

- Interactive diagram: https://gitdiagram.com/scallyw4g/bonsai
- Repository: https://github.com/scallyw4g/bonsai

## Overview

##### Main Components

a) Core Engine Systems:
- Renderer (OpenGL 3.3+)
- Physics System
- Entity Management
- Work Queue (MT Job System)
- Asset Management
- World Management
- Input System

b) Rendering Pipeline:
- Deferred Shading System
- HDR Lighting
- Transparency Handler
- Bloom Effect
- Shadow Mapping
- SSAO (Screen Space Ambient Occlusion)

c) Resource Management:
- Asset Loaders (Collada, OBJ, VOX)
- Texture Management
- Model Management
- Memory Management

**[Game Layer]**
- ↑↓
**[Engine API Layer]**
- ↑↓
**[Core Systems Layer]**
- ↑↓
**[Platform/Hardware Layer]**

##### Key Subsystems Detail

a) Renderer Pipeline:
**[G-Buffer] → [Deferred Lighting] → [Shadow Pass] → [SSAO] → [Transparency] → [HDR/Bloom] → [Final Output]**

b) World Management:
**[World Chunks] → [Voxel Synthesis] → [Mesh Generation] → [Physics Integration] → [Rendering]**

c) Asset Pipeline:
**[Asset Files] → [Loaders] → [Asset Cache] → [Resource Management] → [Engine Systems]**

##### Implementation Notes

- Rectangles: Core systems
- Rounded rectangles: Managers/Controllers
- Hexagons: Processing nodes

##### Critical Paths

Main processing pipelines:
- Asset loading and processing
- Frame update cycle
- Physics simulation
- Render pipeline
- Input processing

##### Performance-Critical Areas

Systems requiring special attention:
- Voxel mesh generation
- Particle systems
- Physics calculations
- Render state management

##### Debug/Development Tools

Supporting systems:
- Performance profiler
- Memory tracker
- Debug visualization
- Hot reload manager

This system design should reflect both the current state of the engine and allow for the planned features mentioned in the wishlist section of the README.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Define styles
    classDef core fill:#ff9999
    classDef render fill:#9999ff
    classDef physics fill:#99ff99
    classDef asset fill:#ffff99
    classDef debug fill:#e699ff
    classDef platform fill:#cccccc

    %% Platform Layer
    subgraph PlatformLayer
        OpenGL["OpenGL 3.3+"]:::platform
        Hardware["Hardware Interface"]:::platform
    end

    %% Core Systems Layer
    subgraph CoreSystems
        Renderer["Core Renderer"]:::render
        Physics["Physics System"]:::physics
        EntityMgr["Entity Management"]:::core
        WorkQueue["Work Queue System"]:::core
        AssetMgr["Asset Management"]:::asset
        WorldMgr["World Management"]:::core
        InputSys["Input System"]:::core
        ResMgr["Resource Management"]:::asset
        Camera["Camera System"]:::core
        MeshSys["Mesh System"]:::core
    end

    %% Rendering Pipeline
    subgraph RenderPipeline
        GBuffer["G-Buffer"]:::render
        DeferredShading["Deferred Shading"]:::render
        ShadowMap["Shadow Mapping"]:::render
        SSAO["SSAO"]:::render
        Transparency["Transparency"]:::render
        HDRBloom["HDR/Bloom"]:::render
        FinalOutput["Final Output"]:::render
    end

    %% Asset Pipeline
    subgraph AssetPipeline
        Loaders["Asset Loaders"]:::asset
        AssetCache["Asset Cache"]:::asset
        ColladaLoader["Collada Loader"]:::asset
        OBJLoader["OBJ Loader"]:::asset
        VOXLoader["VOX Loader"]:::asset
    end

    %% Debug Systems
    subgraph DebugSystems
        Profiler["Performance Profiler"]:::debug
        MemTracker["Memory Tracker"]:::debug
        DebugVis["Debug Visualization"]:::debug
        HotReload["Hot Reload Manager"]:::debug
    end

    %% Relationships
    OpenGL --> Renderer
    Hardware --> Physics
    
    Renderer --> GBuffer
    GBuffer --> DeferredShading
    DeferredShading --> ShadowMap
    ShadowMap --> SSAO
    SSAO --> Transparency
    Transparency --> HDRBloom
    HDRBloom --> FinalOutput

    AssetMgr --> Loaders
    Loaders --> AssetCache
    Loaders --> ColladaLoader
    Loaders --> OBJLoader
    Loaders --> VOXLoader
    
    WorldMgr --> MeshSys
    WorldMgr --> Physics
    EntityMgr --> Physics
    Camera --> Renderer

    %% Debug connections
    Profiler -.-> CoreSystems
    MemTracker -.-> CoreSystems
    DebugVis -.-> Renderer
    HotReload -.-> CoreSystems

    %% Click events
    click Renderer "https://github.com/scallyw4g/bonsai/blob/master/src/engine/render/render_init.h"
    click Physics "https://github.com/scallyw4g/bonsai/blob/master/src/engine/physics.h"
    click EntityMgr "https://github.com/scallyw4g/bonsai/blob/master/src/engine/entity.h"
    click WorkQueue "https://github.com/scallyw4g/bonsai/blob/master/src/engine/work_queue.h"
    click AssetMgr "https://github.com/scallyw4g/bonsai/blob/master/src/engine/asset.h"
    click WorldMgr "https://github.com/scallyw4g/bonsai/blob/master/src/engine/world.h"
    click ResMgr "https://github.com/scallyw4g/bonsai/blob/master/src/engine/resources.h"
    click Camera "https://github.com/scallyw4g/bonsai/blob/master/src/engine/camera.h"
    click MeshSys "https://github.com/scallyw4g/bonsai/blob/master/src/engine/mesh.h"
    click DeferredShading "https://github.com/scallyw4g/bonsai/blob/master/shaders/gBuffer.fragmentshader"
    click HDRBloom "https://github.com/scallyw4g/bonsai/blob/master/shaders/bloom_downsample.fragmentshader"
    click ShadowMap "https://github.com/scallyw4g/bonsai/blob/master/src/engine/render/shadow_map.h"
    click Transparency "https://github.com/scallyw4g/bonsai/blob/master/shaders/3DTransparency.fragmentshader"
    click ColladaLoader "https://github.com/scallyw4g/bonsai/blob/master/src/engine/loaders/collada.cpp"
    click OBJLoader "https://github.com/scallyw4g/bonsai/blob/master/src/engine/loaders/obj.cpp"
    click VOXLoader "https://github.com/scallyw4g/bonsai/blob/master/src/engine/loaders/vox.cpp"
    click DebugVis "https://github.com/scallyw4g/bonsai/blob/master/src/engine/debug.h"

    %% Legend
    subgraph Legend
        L1["Core Systems"]:::core
        L2["Rendering"]:::render
        L3["Physics"]:::physics
        L4["Asset Management"]:::asset
        L5["Debug Systems"]:::debug
        L6["Platform"]:::platform
    end
```
