# kripken/bananabread architecture

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

- Interactive diagram: https://gitdiagram.com/kripken/bananabread
- Repository: https://github.com/kripken/bananabread

## Overview

##### Project type and purpose
- This is BananaBread, a port of the Cube 2/Sauerbraten 3D game engine to the web.
- Its purpose is to compile C++ engine code (rendering, physics, AI, game logic) to JavaScript/WebAssembly via Emscripten, enabling a complete FPS in the browser using WebGL.

##### File structure
- Top-level:
- assets/   – 3D models, textures, sounds, weapon assets
- cube2/    – engine source, demos, web build, native build, assets, packages
- tools/    – auxiliary scripts (e.g. socket_relay.py, websockify)
- cube2/src:
- engine/    – core engine modules (rendering, world, physics, sound)
- fpsgame/   – game rules, AI, entities, weapons, server/client code
- shared/    – cross-platform code (serialization, command handling, i/o)
- src/web/   – Emscripten build targets, web glue code (bb.js, HTML entry points)
- src/native/ & src/vcpp/ & src/xcode/ – native build configurations (SDL)
- src/enet/  – networking library reused by engine
- README.md: describes features, build steps (emmake make client), running via Python SimpleHTTPServer.

##### Key architecture insights from README
- Single codebase for native and web, with platform-specific “glue” layers.
- Build pipeline: Emscripten transforms C++ → asm.js/wasm + JavaScript glue.
- Runtime: browser hosts bb.html, loads wasm, assets, calls into engine API via Module.cwrap.
- Assets and packages (maps/models) are loaded at runtime over HTTP.

##### Components
a. Build Pipeline
- Source code (C++ engine, game, shared, enet)
- Emscripten toolchain
- Outputs: bb.js, bb.wasm, HTML shell, supporting JS (glue)
b. Asset Pipeline
- assets/, cube2/packages/
- Optional Blender source files → exported .iqm/.dds/.ogz
- Static file hosting (SimpleHTTPServer or any web server)
c. Runtime Environment
- Browser:
- WebAssembly module + JS glue
- WebGL context for rendering
- Web Audio / HTML5 Audio for sounds
- DOM / Canvas for input and display
- Engine modules at runtime:
- Rendering (shaders, water, particles)
- Physics (collision, ragdoll)
- AI/Bot subsystem
- Game logic (entities, scoring, weapons)
- Networking stub (enet) for future multiplayer
d. Development Tools
- Python SimpleHTTPServer (or custom HTTP server)
- websockify/socket_relay for remote streaming or debugging
- Native build targets (Linux, Windows, macOS via SDL)

##### Relationships and data flow
- Source code → (emmake) → JS/WASM build
- Assets & packages served by HTTP server → fetched by client on startup
- Browser initializes WebGL + Audio, loads wasm, then calls into engine main()
- Optional future: Engine calls enet networking layer → WebSocket/server relay

##### Architectural patterns / design principles
- Modular layering:
- Core engine (platform-agnostic)
- Platform abstraction layers (SDL for native, Emscripten/HTML5 for web)
- Game logic plugin layer (fpsgame)
- Single-source, multi-target build
- Asset decoupling: static files, loaded at runtime
- Separation of concerns: rendering vs physics vs audio vs AI

##### Essential details without clutter
- Major layers (source, build, runtime, assets)
- Data/control flow: how code and assets move from source to user’s browser to in-memory engine components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Source Code Modules
    subgraph "Source Code"
        direction TB
        CoreEngine["Core Engine Modules"]:::code
        GameLogic["Game Logic & AI"]:::code
        SharedCode["Cross-platform Shared Code"]:::code
        NetworkingStub["Networking Stub (ENet)"]:::code
    end

    %% Build Pipeline
    subgraph "Build Pipeline"
        direction TB
        Emscripten["Emscripten Toolchain"]:::build
        Emmake["emmake make client"]:::build
        Outputs["Web Artifacts:\nbb.js, bb.wasm, bb.html"]:::build
    end

    %% Asset Pipeline
    subgraph "Asset Pipeline"
        direction TB
        Assets["Exported Models/Textures/Sounds"]:::assets
        Packages["Asset Packages (maps/models)"]:::assets
    end

    %% Runtime Environment
    subgraph "Browser Runtime"
        direction TB
        Browser["Browser Client"]:::runtime
        subgraph "WebAssembly & JS Glue"
            WebGlue["JS Glue & HTML Entry Points"]:::code
        end
        subgraph "Browser APIs"
            WebGL["WebGL Renderer"]:::runtime
            AudioAPI["Web Audio/HTML5 Audio"]:::runtime
            DOMInput["DOM/Canvas Input"]:::runtime
        end
        subgraph "Engine at Runtime"
            direction TB
            RuntimeCore["Engine Core Modules"]:::code
            RuntimeGame["Game Logic & AI"]:::code
            RuntimePhys["Physics Subsystem"]:::runtime
            RuntimeAI["AI/Bot Subsystem"]:::runtime
            RuntimeNet["Networking Stub"]:::runtime
        end
    end

    %% Development Tools
    subgraph "Development Tools"
        direction TB
        HTTPServer["Python SimpleHTTPServer"]:::devtools
        SocketRelay["WebSocket Relay & Debug"]:::devtools
    end

    %% Native Builds
    subgraph "Native Build Targets"
        direction TB
        NativeSDL["SDL Native Builds"]:::build
        VCPP["Windows/MSVC Projects"]:::build
        Xcode["macOS Xcode Projects"]:::build
    end

    %% Connections
    CoreEngine -->|feeds| Emscripten
    GameLogic -->|feeds| Emscripten
    SharedCode -->|feeds| Emscripten
    NetworkingStub -->|feeds| Emscripten
    Emscripten -->|via emmake| Emmake
    Emmake -->|produces| Outputs

    Assets -->|hosted by| HTTPServer
    Packages -->|hosted by| HTTPServer
    Outputs -->|served by| HTTPServer
    HTTPServer -->|HTTP GET| Browser

    Browser --> WebGlue
    WebGlue --> RuntimeCore
    Browser --> WebGL
    Browser --> AudioAPI
    Browser --> DOMInput

    RuntimeCore --> RuntimePhys
    RuntimeCore --> RuntimeAI
    RuntimeCore --> RuntimeGame
    RuntimeCore --> RuntimeNet

    %% Native flow
    CoreEngine --> NativeSDL
    GameLogic --> NativeSDL
    SharedCode --> NativeSDL

    NativeSDL --> VCPP
    NativeSDL --> Xcode

    %% Click Events
    click CoreEngine "https://github.com/kripken/bananabread/tree/master/cube2/src/engine/"
    click GameLogic "https://github.com/kripken/bananabread/tree/master/cube2/src/fpsgame/"
    click SharedCode "https://github.com/kripken/bananabread/tree/master/cube2/src/shared/"
    click NetworkingStub "https://github.com/kripken/bananabread/tree/master/cube2/src/enet/"
    click WebGlue "https://github.com/kripken/bananabread/tree/master/cube2/src/web/"
    click VCPP "https://github.com/kripken/bananabread/tree/master/cube2/src/vcpp/"
    click Xcode "https://github.com/kripken/bananabread/tree/master/cube2/src/xcode/"
    click Assets "https://github.com/kripken/bananabread/tree/master/assets/"
    click Packages "https://github.com/kripken/bananabread/tree/master/cube2/packages/"
    click SocketRelay "https://github.com/kripken/bananabread/blob/master/tools/socket_relay.py"
    click SocketRelay "https://github.com/kripken/bananabread/tree/master/tools/websockify/"

    %% Styles
    classDef code fill:#f9f,stroke:#333,stroke-width:1px
    classDef build fill:#ff9,stroke:#333,stroke-width:1px
    classDef runtime fill:#9ff,stroke:#333,stroke-width:1px
    classDef assets fill:#9f9,stroke:#333,stroke-width:1px
    classDef devtools fill:#f99,stroke:#333,stroke-width:1px
```
