System Architecture
Cub3D is a Wolf3D-style raycasting game engine built in C with SDL2. The architecture follows a modular design with clear separation of concerns across rendering, entity management, input handling, and game logic.High-Level Architecture
Module Structure
Core Modules
ft_utils - Utility Functions
ft_utils - Utility Functions
Location:
src/ft_utils/Provides fundamental data structures and utilities:- Hashmap: Key-value storage for sprites, fonts, sounds
- Linked Lists: Dynamic entity management
- Time Utilities: Delta time, FPS tracking
- Math Utilities: Vector operations, angle calculations
- String Operations: Parsing, manipulation
ft_mlx_utils - Graphics & Window Management
ft_mlx_utils - Graphics & Window Management
Location:
src/ft_mlx_utils/SDL2 wrapper providing:- Window Management: Creation, event handling, rendering
- Image Operations: Loading, manipulation, pixel operations
- Text Rendering: Bitmap font support
- Input Controllers: Keyboard, mouse, gamepad
window/: Window lifecycle managementimage/: Image loading and manipulationtext/: Font rendering (IBM VGA 8x16)controllers/: Input handling
ft_audio - Audio System
ft_audio - Audio System
Location:
src/ft_audio/Audio playback using miniaudio library:- Sound effect management
- Background music
- 3D positional audio support
- Audio resource loading/cleanup
ft_threads - Threading System
ft_threads - Threading System
Location: Used primarily for parallel raycasting across CPU cores.
src/ft_threads/Pthread wrapper for parallel processing:Entities System
Entities System
Location:
src/utils/entities/Entity Component System managing all game objects:entity/: Base entity implementationbillboard/: Sprite-based entitiescharacter/: Living entities with health/inventoryplayer/: Player-controlled characterswall/: Static wall entitiesdoor/: Interactive door entitieselevator/: Map transition entitiesdrop/: Item drops in the worldcontroller/: AI and player controllers
Items System
Items System
Location:
src/utils/items/Item and inventory management:item/: Base item implementationweapon/: Weapons with damage/rangecollectible/: Health/ammo/score pickups
Render System
Render System
Location:
src/utils/render/Raycasting and rendering pipeline:camera/: 3D raycasting enginewalls/: Wall renderingbillboards/: Sprite rendering
hud/: Heads-up displayminimap/: 2D map overlaystats/: Health/ammo/score displaydebug/: Debug information overlay
Map System
Map System
Location:
src/utils/map/Map parsing and representation:.cubfile parser- Entity type mapping
- Map validation
- Dynamic map loading
Game Loop
Game Loop
Location:
src/utils/loop/Main game loop coordination:- Event processing
- Frame timing
- Entity updates
- Rendering orchestration
Sprite System
Sprite System
Location:
src/utils/sprites/Sprite and animation management:- Sprite loading/caching
- Animation state machine
- Directional sprites (8-way)
Threading Model
Parallel Raycasting
Cub3D uses 4 parallel threads for raycasting to maximize performance:Thread Distribution
Each thread renders a vertical slice of the screen:- Thread 0: columns 0-255
- Thread 1: columns 256-511
- Thread 2: columns 512-767
- Thread 3: columns 768-1023
Synchronization
The main game state is protected by a mutex:Data Flow
Frame Update Cycle
Entity Update Flow
frame() implementation:
- Player: Process input, move, update camera
- Character: AI decision making, weapon usage
- Door: Animation state, auto-close timer
- Billboard: Sprite animation updates
- Drop: Item pickup detection
Rendering Pipeline
Raycast Walls
For each screen column (parallelized across 4 threads):
- Cast ray from player position
- Find wall intersection
- Calculate wall height on screen
- Sample texture and draw vertical strip
Render Billboards
- Sort billboards by distance (painter’s algorithm)
- For each billboard:
- Project 3D position to screen space
- Draw sprite with depth-based scaling
- Apply transparency
Render HUD
Draw UI elements on top of 3D view:
- Stats (health, ammo, score)
- Minimap
- Debug info (FPS, position)
- Action prompts
Memory Management
Resource Lifecycle
Hashmap Usage
Hashmaps provide O(1) resource lookup:2D sprite cache (icons, UI elements)
3D directional sprites (8-way character sprites)
Audio clip cache
Loaded font resources
Entity type constructors keyed by map character
Performance Characteristics
FPS Management
Frame Time
Delta time starts at 0.016s (62.5 FPS) and dynamically adjusts based on actual frame timing for consistent physics.
Raycasting Optimization
Number of sub-rays cast per screen column for anti-aliasing and hit detection accuracy.
Maximum entities tracked per ray for shooting/interaction.
Maximum ray distance when no wall is hit (prevents infinite rays).
See Also
- Configuration Reference - All configurable constants
- Entities API - Entity system details
- Items & Weapons - Item system reference