# bithack/principia architecture

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

- Interactive diagram: https://gitdiagram.com/bithack/principia
- Repository: https://github.com/bithack/principia

## Overview

##### Project Type and Purpose
**Project Type:** Principia is a **sandbox physics game**. It is a **cross-platform application** originally developed for Android and later extended to Windows, Linux, and experimental ports for Haiku OS and macOS. The project involves complex interactions between game logic, physics simulation, rendering, and user interactions.

**Purpose:** The primary purpose of Principia is to provide an interactive sandbox environment where users can experiment with physics-based constructions and simulations. As an open-source project, it also serves as a platform for community-driven development and enhancements.

##### File Structure
- **Top-Level Directories:**
- `android/`: Contains Android-specific build configurations and source code.
- `data-src/`: Houses all game assets, including models, textures, sounds, and levels.
- `lib/`: Includes third-party libraries such as Box2D (physics engine), SDL (multimedia library), imgui (GUI library), and Lua (scripting language).
- `packaging/`: Scripts and configurations for building and packaging the application across different platforms.
- `src/`: Core source code of the Principia game.
- `utils/`: Utility scripts for tasks like version management and menu updates.

- **Patterns and Architectural Choices:**
- **Modular Architecture:** Separation of concerns is evident with distinct directories for assets (`data-src`), libraries (`lib`), and source code (`src`).
- **Dependency Management:** Utilizes `CMakeLists.txt` for configuring builds and managing dependencies through `lib/`.
- **Platform-Specific Implementations:** Platform-specific code resides in separate directories (e.g., `android/`, `packaging/`) to handle differences in OS requirements.
- **Scripting Support:** Integration of Lua scripting for game logic and flexibility.

##### README for Additional Insights
- **Architecture Description:** While the README provides a high-level overview, it indicates the use of Box2D for physics, SDL for rendering and input, imgui for in-game GUI, and Lua for scripting.
- **Technical Stack:** C++ is the primary programming language, with dependencies managed through CMake. The project supports multiple platforms, suggesting the use of cross-platform libraries and abstractions.
- **Community-Driven Development:** Emphasizes open-source collaboration, which implies a scalable and maintainable codebase to accommodate multiple contributors.

##### a. Main Components
- **Game Engine (`src/`):** Core logic handling game states, entity management, and overall game flow.
- **Physics Engine (Box2D in `lib/Box2D`):** Manages physics simulations, including collisions, forces, and rigid body dynamics.
- **Rendering Engine (SDL in `lib/SDL_mixer`, `lib/SDL_image`):** Handles graphics rendering, audio playback, and input processing.
- **GUI System (imgui in `lib/imgui`):** Provides in-game user interfaces for menus, dialogs, and HUD elements.
- **Scripting Engine (Lua in `lib/lua`):** Facilitates game customization and dynamic behavior through scripts.
- **Asset Management (`data-src/`):** Stores and organizes game assets like models, textures, sounds, and levels.
- **Platform-Specific Modules (`android/`, `packaging/`):** Contains code and configurations tailored for different operating systems.
- **Build and Deployment Scripts (`packaging/`, `CMakeLists.txt`):** Automates the building, packaging, and deployment processes.

##### b. Relationships and Interactions
- **Game Engine ↔ Physics Engine:** The game engine sends physical properties and actions to Box2D, which returns simulation results.
- **Game Engine ↔ Rendering Engine:** The game engine issues rendering commands to SDL to display graphics and handle user input.
- **Game Engine ↔ GUI System:** Utilizes imgui for rendering in-game menus and interfaces.
- **Game Engine ↔ Scripting Engine:** Executes Lua scripts to modify game behavior dynamically.
- **Rendering Engine ↔ Asset Management:** Loads and manages graphical assets from `data-src/` for rendering.
- **Platform-Specific Modules ↔ Rendering/Physics/GUI:** Interfaces with platform-specific APIs to ensure compatibility and performance.
- **Build Scripts ↔ All Components:** CMake orchestrates the compilation and linking of all components, while packaging scripts handle platform-specific builds.

##### c. Architectural Patterns and Design Principles
- **Modular Design:** Encourages separation of concerns, making the system maintainable and scalable.
- **Layered Architecture:** Distinct layers for engine, physics, rendering, GUI, and scripting facilitate independent development and testing.
- **Cross-Platform Abstraction:** Utilizes abstraction layers to handle platform-specific implementations, ensuring portability.
- **Use of Established Libraries:** Leveraging robust third-party libraries like Box2D, SDL, and imgui accelerates development and ensures reliability.

##### d. Relevant Technologies and Frameworks
- **Programming Language:** C++
- **Physics Engine:** Box2D
- **Multimedia Library:** SDL (including SDL_image and SDL_mixer)
- **GUI Library:** imgui
- **Scripting Language:** Lua
- **Build System:** CMake
- **Version Control and CI:** GitHub Actions (`.github/workflows/`)
- **Packaging Tools:** Scripts for AppImage, Windows installers, etc.

##### Elements
- **Clear Labels:** Each component (e.g., Game Engine, Physics Engine, Rendering Engine) should be distinctly labeled.
- **Blue Rectangles:** Core Engines (Game, Physics)
- **Green Ovals:** Libraries and Frameworks (Box2D, SDL, imgui)
- **Red Diamonds:** Platform-Specific Modules

##### Core Layers
- **Game Engine**
- Communicates with Physics Engine
- Interfaces with Rendering Engine
- Utilizes GUI System
- Executes Scripts via Scripting Engine
- **Physics Engine (Box2D)**
- **Rendering Engine (SDL)**
- **GUI System (imgui)**
- **Scripting Engine (Lua)**

##### Supporting Layers
- **Asset Management (`data-src/`)**
- **Platform-Specific Modules (`android/`, `packaging/`)**
- **Build and Deployment (`CMakeLists.txt`, `packaging/`)**

##### External Dependencies
- **Third-Party Libraries (`lib/`)**
- **Community Contributions and CI (`.github/workflows/`)**

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Define nodes
    GAME["Game Engine"]:::coreEngine
    BOX2D((Box2D)):::library
    SDLIMG((SDL_image)):::library
    SDLMIX((SDL_mixer)):::library
    IMGUI((imgui)):::library
    LUA((Lua)):::library
    ASSETS([Asset Management]):::dataStore
    ANDROID{{android}}:::platformModule
    PACKAGING{{packaging}}:::platformModule
    CMAKE["CMakeLists.txt"]:::buildScripts

    %% Define connections
    GAME -->|"uses"| BOX2D
    GAME -->|"uses"| SDLIMG
    GAME -->|"uses"| SDLMIX
    GAME -->|"uses"| IMGUI
    GAME -->|"uses"| LUA
    GAME -->|"loads assets from"| ASSETS
    GAME -->|"builds with"| CMAKE

    SDLIMG --> ASSETS
    SDLMIX --> ASSETS
    IMGUI --> ASSETS

    ANDROID --> GAME
    PACKAGING --> GAME
    PACKAGING --> BOX2D
    PACKAGING --> SDLIMG
    PACKAGING --> SDLMIX
    PACKAGING --> IMGUI
    PACKAGING --> LUA
    PACKAGING --> CMAKE

    %% Click Events
    click GAME "https://github.com/bithack/principia/tree/master/src/"
    click BOX2D "https://github.com/bithack/principia/tree/master/lib/Box2D/"
    click SDLIMG "https://github.com/bithack/principia/tree/master/lib/SDL_image/"
    click SDLMIX "https://github.com/bithack/principia/tree/master/lib/SDL_mixer/"
    click IMGUI "https://github.com/bithack/principia/tree/master/lib/imgui/"
    click LUA "https://github.com/bithack/principia/tree/master/lib/lua/"
    click ASSETS "https://github.com/bithack/principia/tree/master/data-src/"
    click ANDROID "https://github.com/bithack/principia/tree/master/android/"
    click PACKAGING "https://github.com/bithack/principia/tree/master/packaging/"
    click CMAKE "https://github.com/bithack/principia/blob/master/CMakeLists.txt"

    %% Styles
    classDef coreEngine fill:#00BFFF,stroke:#000,stroke-width:1px;
    classDef library fill:#32CD32,stroke:#000,stroke-width:1px;
    classDef dataStore fill:#FFD700,stroke:#000,stroke-width:1px;
    classDef platformModule fill:#FF4500,stroke:#000,stroke-width:1px;
    classDef buildScripts fill:#A9A9A9,stroke:#000,stroke-width:1px;
```
