Overview
Cub3D is a 3D raycasting engine that renders a first-person perspective game world similar to Wolfenstein 3D. The engine uses the Digital Differential Analysis (DDA) algorithm to cast rays through a 2D grid-based map and render walls with textured surfaces.Core Components
The system is organized into several functional components:Map Parser
Validates and loads
.cub map files containing textures, colors, and map layoutRaycasting Engine
Implements DDA algorithm to calculate wall intersections and distances
Rendering Pipeline
Converts raycasting data into textured vertical wall slices
Input System
Handles keyboard events for player movement and camera rotation
File Organization
The codebase follows a modular structure with clear separation of concerns:Main Entry Point
main.c - Program initialization and main loop setup
main.c (lines 74-100)
Parsing System
parselineutils.c
parselineutils.c
Parses texture paths, floor/ceiling colors from the map file header
parsefloorceilingdirections()- Main parsing loopcheckatributes()- Validates individual attribute lines
asignatributes.c
asignatributes.c
Assigns parsed values to the map structure
asign_directions()- Validates and stores texture pathsasign_colors()- Validates and stores RGB color values
utils.c, utils2.c
utils.c, utils2.c
Map validation and grid parsing
ismap()- Validates.cubfile extensioncheck3atributtes()- Orchestrates parsing pipelinemapcpy()- Copies map grid from file
utils3.c, utils4.c
utils3.c, utils4.c
Map validation and flood fill
checkns()- Validates player starting positionflood_fill()- Ensures map is properly enclosed
Raycasting System
raycasting.c - Core DDA implementation
raycasting()- Main loop that casts rays for each screen columninit_dda()- Initializes ray parameters and step directionsperform_dda()- Executes DDA algorithm to find wall hitscalculate_wall_data()- Computes perpendicular distance and wall heightselect_texture()- Determines which texture to use based on ray direction
Rendering System
drawing.c - Converts raycasting data to pixels
clear_image()- Clears the frame bufferdraw_3d_dda()- Draws ceiling, walls, and floor for a vertical slicedraw_wall()- Samples texture data and renders wall columncalculate_and_draw_pixel()- Maps texture coordinates to screen pixels
utils7.c - Low-level drawing utilities
put_pixel()- Writes a single pixel to the image bufferparse_color()- Converts RGB string to integer color value
Input System
utils5.c, utils6.c, utils7.c - Event handling and movement
handle_key()/key_release()- Keyboard event handlerscubed_render()- Main render loop called every framemove_cubed()- Updates player position based on inputrotate_cubed()- Updates camera angleis_wall()- Collision detection
MiniLibX Integration
The engine uses MiniLibX, a simple X-Window graphics library for rendering:main.c (lines 51-72)
Rendering Pipeline
The rendering process follows this sequence each frame:Update Player State
- Process keyboard input for movement and rotation
- Update player position (
xx,yy) with collision detection - Update camera direction vectors (
dir_x,dir_y) - Update camera plane vectors (
plane_x,plane_y)
Cast Rays
For each of the 1732 screen columns:
- Calculate ray direction based on camera position
- Initialize DDA parameters
- Step through grid until wall is hit
- Calculate perpendicular distance to wall
- Determine wall height on screen
Texture Selection
Based on which side of the wall was hit (North/South/East/West), select appropriate texture
Draw Column
- Draw ceiling pixels above wall (sky blue:
0x87CEEB) - Sample texture and draw textured wall pixels
- Draw floor pixels below wall (dark gray:
0x444444)
Frame Timing
utils6.c (lines 74-91)
The render loop is called continuously by MiniLibX’s event system. There is no explicit frame rate limiting, so rendering speed depends on hardware capabilities.
Main Execution Flow
Configuration Constants
Key constants defined incub3.h:
| Constant | Value | Description |
|---|---|---|
WIDTH | 1732 | Screen width in pixels |
HEIGHT | 1000 | Screen height in pixels |
SIZE | 30 | Grid cell size (affects movement scale) |
POV | 1.17 | Field of view multiplier |
PI | 3.14159265358979323846 | Pi constant for angle calculations |
Error Handling
The system includes comprehensive error checking:- Argument validation: Checks for exactly 2 arguments and
.cubextension - File validation: Verifies map file can be opened and is not empty
- Texture validation: Ensures all texture files exist and can be loaded
- Map validation: Verifies map is closed, contains exactly one player, and has valid characters
- Memory allocation: Checks all allocations and frees resources on error
- Color validation: Ensures RGB values are in 0-255 range