# simoxeur/cub3d_42 architecture

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

- Interactive diagram: https://gitdiagram.com/simoxeur/cub3d_42
- Repository: https://github.com/simoxeur/cub3d_42

## Overview

##### Project type and purpose
- Cub3D is a small 3D game engine that parses a simple map file, executes a continuous game loop (handling input, updating game state, performing raycasting, and rendering frames), and displays graphics via the MLX42 windowing library.

##### File structure
- Top-level modules:
– parse/ : map-file parsing and validation
– srcs/ : core engine (initialization, game loop, input hook, rendering, raycasting, minimap, torch)
– get_next_line/ : line-oriented file reader used by the parser
– utils/ : generic helper functions (string, memory, error handling)
– maps/ and textures/ : external resources (map definitions, sprite/texture files)
- Build orchestrated by a Makefile, relies on external MLX42 library

##### README insights
- Single executable invoked with a .cub map file
- Controls map to input hooks that drive player movement, torch, and camera rotation

##### Main components & their interactions
a. Resource Manager (maps/, textures/, get_next_line/)
– Reads .cub file using get_next_line
– Validates map and loads texture paths
– Outputs in-memory structures (map grid, texture references)
b. Initialization Module (srcs/initialisation.c)
– Configures MLX42 window/context
– Allocates and sets up core game state structures
c. Game Loop & Input Handler (srcs/key_hook.c, srcs/input_handle.c)
– Receives keyboard/mouse events from MLX42
– Updates player position, orientation, torch state
d. Raycasting Engine (srcs/ray_handler.c, ray_cast_utils*.c)
– Casts rays per vertical screen stripe
– Computes wall distances and texture coordinates
e. Renderer (srcs/render3d.c, render_utils.c, graphic_handle.c)
– Pushes buffer to the MLX42 window
f. Cleanup & Error Handling (srcs/clean_close.c, srcs/error_mlx.c)
– Frees allocations, closes window, exits gracefully

##### Architectural patterns & principles
- Event-driven game loop: clear separation of input → update → render stages
- Modular layering:
– Low-level utils and file I/O
– Parser and resource loading
– Core game logic (init, update, raycast)
– Rendering and external library interface
- Single process, single threaded: no networking or external services

- Continuous loop: Input → Update (raycast + state) → Render → back to Input
- Initialization and cleanup as distinct steps outside the main loop

## Diagram (Mermaid)

```mermaid
flowchart BT
    %% Layer 1: External Resources & Libraries
    subgraph "External Resources & Libraries"
        direction TB
        Maps{{"maps/ (Disk)"}}
        Textures{{"textures/ (Disk)"}}
        MLX["MLX42 (Library)"]
    end

    %% Layer 2: Utilities & Parser
    subgraph "Utilities & Parser"
        direction TB
        GNL["get_next_line"]
        Utils["utils"]
        Parser["Parser"]
    end

    %% Layer 3: Core Engine
    subgraph "Core Engine"
        direction TB
        Init["Initialization"]
        subgraph "Game Loop"
            direction TB
            Input["Input Handler"]
            Raycast["Raycasting Engine"]
            Render["Renderer"]
        end
        Cleanup["Cleanup & Error Handling"]
    end

    %% Layer 4: Output
    Window(["Window Display"])

    %% Connections
    Maps -->|loads .cub file| Parser
    Textures -->|provides textures| Parser
    GNL -->|reads file| Parser
    Utils -->|helpers| Parser
    Parser -->|outputs MapData| Init

    Utils -->|helpers| Init
    MLX -->|context setup| Init

    Init -->|starts loop| Input
    MLX -->|events| Input
    Input -->|player state| Raycast
    Raycast -->|draw data| Render
    Render -->|frame| MLX
    Render -->|updates window| Window

    Cleanup -->|frees resources| Init

    %% Click Events
    click Maps "https://github.com/simoxeur/cub3d_42/tree/main/maps/"
    click Textures "https://github.com/simoxeur/cub3d_42/tree/main/textures/"
    click GNL "https://github.com/simoxeur/cub3d_42/tree/main/get_next_line/"
    click Utils "https://github.com/simoxeur/cub3d_42/tree/main/utils/"
    click Parser "https://github.com/simoxeur/cub3d_42/tree/main/parse/"
    click Init "https://github.com/simoxeur/cub3d_42/blob/main/srcs/initialisation.c"
    click Input "https://github.com/simoxeur/cub3d_42/blob/main/srcs/input_handle.c"
    click Raycast "https://github.com/simoxeur/cub3d_42/blob/main/srcs/ray_handler.c"
    click Render "https://github.com/simoxeur/cub3d_42/blob/main/srcs/render3d.c"
    click Cleanup "https://github.com/simoxeur/cub3d_42/blob/main/srcs/clean_close.c"

    %% Styles
    classDef external fill:#cccccc,stroke:#999,stroke-width:1px
    classDef utility fill:#9f6,stroke:#393,stroke-width:1px
    classDef internal fill:#6cf,stroke:#039,stroke-width:1px
    classDef resource fill:#f96,stroke:#c60,stroke-width:1px

    class Maps,Textures resource
    class MLX external
    class GNL,Utils utility
    class Parser internal
    class Init,Input,Raycast,Render,Cleanup internal
    class Window external
```
