# madebypixel02/cub3d architecture

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

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

## Overview

##### Project type and purpose
- This is a single‐process, native C “game engine” implementing a Wolfenstein-style renderer (a raycaster) using the MiniLibX graphics library.

##### File structure
- inc/ : public headers defining data structures and function prototypes (cub3d.h, get_next_line.h, keycodes.h, my_mlx.h).
- src/ : modules implementing
- main.c (entry point, init, game loop)
- parse_map.c, map.c (map file parsing, validation)
- get_next_line.c(.utils) (file I/O)
- raycast.c, render.c (ray‐casting logic, fisheye fix, frame generation)
- player.c, game.c, minimap.c, utils.c, end_*.c, error.c (input, state updates, bonus features, cleanup).
- textures/ : XPM sprite assets for walls, doors, Pac-Man, ghosts, torches.
- maps/ : sample .cub maps + MAPS.md.
- Build files (Makefile, .gitignore).

##### README for additional insights
- No network or database; pure graphics+math+I/O.

##### Components
a. Main Components
- CLI & Config Loader
- Parser
- Core Game Loop
- Input Handler
- World Updater (player movement, door toggling, collisions, animations)
- Raycasting Engine (distance calculator, fisheye corrector)
- Renderer (texture sampler, vertical stripe drawer)
- MiniLibX Graphics Subsystem (window, event hooks, pixel buffer)
- Cleanup/Exit Handler
b. Relationships & Data Flow
1. User invokes `cub3d map.cub` on the CLI → Map Parser reads .cub file via get_next_line → builds in‐memory Map data structure + header settings.
2. Resource Manager loads XPM textures (file I/O) into memory.
3. Enter Game Loop:
– Input Handler receives key/mouse events from MiniLibX → updates Player orientation/commands.
– World Updater applies movement vectors & door toggles → updates Map/Player state.
– Raycasting Engine casts N rays per frame (window width) → outputs distance slices & wall side info.
– Renderer uses distance values + textures to produce a pixel buffer.
4. MiniLibX draws the pixel buffer to the window each frame.
5. Loop repeats until exit; Cleanup Handler frees memory and closes window.
c. Architectural Patterns & Principles
- Module separation (parsing vs. rendering vs. input vs. resource loading).
- Event‐driven loop (MiniLibX event hooks).
- Data‐driven rendering pipeline (map+player → rays → frame).
- Single‐threaded, synchronous loop (no concurrency).
d. Key Technologies & Libraries
- C99
- MiniLibX (graphics/window/event API)
- POSIX file I/O (open, read) via get_next_line
- math.h for trigonometry
- XPM file format for textures

##### Detailed capture of essential elements
- Do not fire‐hose every C file; group related .c/.h into higher‐level modules.
- Cover all critical paths: startup → game loop → shutdown.
- How textures flow from disk → memory → pixel buffer → screen.
- Call out the raycasting math block (distance computation, fisheye correction, wall height formula).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Initialization Stage
    SampleMaps["Sample Maps"]:::external
    CLI["CLI & Config Loader"]:::core
    FileIO["File I/O / Line Reader"]:::core
    MapParser["Map Parser"]:::core
    ResourceLoader["Resource Manager (Textures & Colors)"]:::resource
    InitComplete["Initialization Complete"]:::core

    %% Game Loop
    subgraph "Game Loop"
        direction TB
        Input["Input Handler"]:::core
        Update["World Updater"]:::core
        Raycast["Raycasting Engine"]:::core
        Render["Renderer"]:::core
        MLXDraw["Draw Frame (MiniLibX)"]:::external
    end

    %% Cleanup
    Cleanup["Cleanup / Exit Handler"]:::core

    %% Data Flow
    SampleMaps -->|"user runs cub3d map.cub"| CLI
    CLI -->|"open/read file"| FileIO
    FileIO -->|"lines of config"| MapParser
    MapParser -->|"map data + header"| ResourceLoader
    ResourceLoader -->|"textures & colors loaded"| InitComplete
    InitComplete --> Input
    Input -->|"controls"| Update
    Update -->|"world state"| Raycast
    Raycast -->|"distance slices"| Render
    Render -->|"pixel buffer"| MLXDraw
    MLXDraw --> Input
    Input -->|"exit event"| Cleanup

    %% Click Events
    click CLI "https://github.com/madebypixel02/cub3d/blob/main/src/main.c"
    click FileIO "https://github.com/madebypixel02/cub3d/blob/main/src/get_next_line.c"
    click MapParser "https://github.com/madebypixel02/cub3d/blob/main/src/parse_map.c"
    click ResourceLoader "https://github.com/madebypixel02/cub3d/blob/main/src/textures.c"
    click Input "https://github.com/madebypixel02/cub3d/blob/main/src/player.c"
    click Update "https://github.com/madebypixel02/cub3d/blob/main/src/minimap.c"
    click Raycast "https://github.com/madebypixel02/cub3d/blob/main/src/raycast.c"
    click Render "https://github.com/madebypixel02/cub3d/blob/main/src/render.c"
    click MLXDraw "https://github.com/madebypixel02/cub3d/blob/main/src/my_mlx.c"
    click Cleanup "https://github.com/madebypixel02/cub3d/blob/main/src/end_linux.c"
    click SampleMaps "https://github.com/madebypixel02/cub3d/tree/main/maps/"

    %% Styles
    classDef core fill:#D0E8FF,stroke:#0366D6,stroke-width:1px color:#03396c
    classDef resource fill:#E8F5D0,stroke:#228B22,stroke-width:1px color:#2B662B
    classDef external fill:#F0F0F0,stroke:#666666,stroke-width:1px color:#333333
```
