# bengschor/cub3d architecture

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

- Interactive diagram: https://gitdiagram.com/bengschor/cub3d
- Repository: https://github.com/bengschor/cub3d

## Overview

##### Main Components
- Raycasting Engine – The heart of the system; this module (located in “srcs/raycasting” and “bns_srcs/raycasting”) is responsible for computing the camera ray for each screen column, performing the Digital Differential Analysis (DDA), determining wall distances, and selecting textures.
- Player & Input Handling – The player movement (rotation, forward/backward movement) is integrated via the “srcs/hooks” and “bns_srcs/hooks” code, which captures and interprets key inputs.
- Utility Functions – Various helper functions found in “srcs/utils” and “bns_srcs/utils”, such as memory management, string handling, and drawing primitives (e.g., Bresenham algorithm for some directions).

##### Relationships and Interactions
- Initialization:
- The parsing modules (either in srcs/parsing or bns_srcs/parsing for bonus features) set up the game state.
- Game Loop:
- Once initialized, the main loop takes over, where for each frame several actions happen:
a) Input is captured (from srcs/hooks or bns_srcs/hooks).
b) The raycasting module computes the ray direction based on the player’s current position and camera plane. This involves camera ray calculation, determining step directions, side distance initialization, iterating via DDA, and finally computing perpendicular wall distances.
c) Based on distances, wall slices are computed and drawn using texture data.
- Rendering:
- The computed image is rendered on a window managed by the MinilibX library. The rendering process also deals with auxiliary components like an optional minimap.
- Error Management:
- Throughout the process, the utility functions handle errors and ensure resources are managed properly (memory allocation/deallocation, input validation).

##### Architectural Patterns & Design Principles
- Modular Design – Each of the primary functionalities (parsing, raycasting, input handling, rendering, utilities) is separated into its own directory. This separation aids clarity and maintainability.
- The bottom layer includes “minilibx” as the interface to the operating system’s graphical subsystem.
- The middle layers include core game logic (raycasting engine, player input) and parsing.
- The top layer reflects the high-level application control (main loop and state management).
- Reusability and Extensibility – The bonus directories (e.g. bns_srcs) indicate additional features or extensions built on top of the core engine.

##### Technologies & External Dependencies
- C language for the core logic.
- MinilibX library – This is a crucial dependency used for windowing and rendering.
- Custom asset loading of textures stored in XPM format (the “assets” directory).

##### Very Detailed System Design
- Error handling and memory management as supporting features across modules.
- Optionally, include a section to note that the project supports bonus features (as indicated by the separate “bns_srcs” directories) which could be annotated as extended components enhancing the core functionality.

## Diagram (Mermaid)

```mermaid
flowchart TD

  subgraph "Initialization"
    M1["Main Game Loop (Core)"]:::initial
    M2["Main Game Loop (Bonus)"]:::initial
  end

  subgraph "Map & Asset Loader"
    P1["Map & Asset Loader (Core)"]:::parsing
    P2["Map & Asset Loader (Bonus)"]:::parsing
  end

  subgraph "Player & Input Handling"
    I1["Player & Input (Core)"]:::input
    I2["Player & Input (Bonus)"]:::input
  end

  subgraph "Raycasting Engine"
    R1["Raycasting Engine (Core)"]:::raycasting
    R2["Raycasting Engine (Bonus)"]:::raycasting
  end

  subgraph "Rendering Engine"
    RG["Rendering Engine (MinilibX)"]:::rendering
  end

  subgraph "Utility Functions"
    U1["Utility Functions (Core)"]:::utility
    U2["Utility Functions (Bonus)"]:::utility
  end

  %% Connections for game flow
  M1 -->|"init"| P1
  M2 -->|"init"| P2

  M1 -->|"start"| I1
  M2 -->|"start"| I2

  I1 -->|"input"| R1
  I2 -->|"input"| R2

  R1 -->|"render"| RG
  R2 -->|"render"| RG

  RG -.->|"loop"| M1
  RG -.->|"loop"| M2

  %% Dotted arrows from modules to utility functions for support
  P1 -.-> U1
  P2 -.-> U2
  I1 -.-> U1
  I2 -.-> U2
  R1 -.-> U1
  R2 -.-> U2
  RG -.-> U1

  %% Click Events
  click M1 "https://github.com/bengschor/cub3d/blob/main/srcs/main.c"
  click M2 "https://github.com/bengschor/cub3d/blob/main/bns_srcs/main_bonus.c"
  click P1 "https://github.com/bengschor/cub3d/tree/main/srcs/parsing"
  click P2 "https://github.com/bengschor/cub3d/tree/main/bns_srcs/parsing"
  click I1 "https://github.com/bengschor/cub3d/tree/main/srcs/hooks"
  click I2 "https://github.com/bengschor/cub3d/tree/main/bns_srcs/hooks"
  click R1 "https://github.com/bengschor/cub3d/tree/main/srcs/raycasting"
  click R2 "https://github.com/bengschor/cub3d/tree/main/bns_srcs/raycasting"
  click RG "https://github.com/bengschor/cub3d/tree/main/minilibx"
  click U1 "https://github.com/bengschor/cub3d/tree/main/srcs/utils"
  click U2 "https://github.com/bengschor/cub3d/tree/main/bns_srcs/utils"

  %% Styles
  classDef initial fill:#f3722c,stroke:#000,stroke-width:2px;
  classDef parsing fill:#f9c74f,stroke:#000,stroke-width:2px;
  classDef input fill:#f94144,stroke:#000,stroke-width:2px;
  classDef raycasting fill:#90be6d,stroke:#000,stroke-width:2px;
  classDef rendering fill:#577590,stroke:#000,stroke-width:2px;
  classDef utility fill:#277da1,stroke:#000,stroke-width:2px;
```
