Core Structures
Cub3D uses two primary structures defined incub3.h to manage game state and texture data.
t_map Structure
Thet_map structure is the central data container that holds all game state, configuration, and runtime data:
cub3.h (lines 79-162)
Field Categories
Map Configuration (Parsed from .cub file)
Map Configuration (Parsed from .cub file)
| Field | Type | Description |
|---|---|---|
northtexture | char* | Path to north wall texture (e.g., ”./textures/north.xpm”) |
southtexture | char* | Path to south wall texture |
westtexture | char* | Path to west wall texture |
easttexture | char* | Path to east wall texture |
floorcolor | char* | Floor color as string (e.g., “220,100,0”) |
ceilingcolor | char* | Ceiling color as string |
floor_color | int | Floor color as packed RGB integer |
ceiling_color | int | Ceiling color as packed RGB integer |
path | char* | Path to the .cub map file |
name | char* | Map name extracted from filename (used as window title) |
Map Data
Map Data
| Field | Type | Description |
|---|---|---|
map[500][500] | char | Static buffer for initial map parsing |
final_map | char** | Dynamically allocated final map used during gameplay |
toflood_map | char** | Copy of map used for flood fill validation |
spaceless_map | char** | Map variant without spaces (unused in current implementation) |
map_height | int | Not used (see row instead) |
map_width | int | Not used (see column instead) |
row | int | Number of rows in the map |
column | int | Number of columns in the map |
The map uses ‘1’ for walls, ‘0’ for empty space, and ‘N’/‘S’/‘E’/‘W’ for the player starting position and orientation.
Keyboard Input State
Keyboard Input State
| Field | Type | Description |
|---|---|---|
key_w | int | 1 if W key pressed (move forward), 0 otherwise |
key_s | int | 1 if S key pressed (move backward), 0 otherwise |
key_a | int | 1 if A key pressed (strafe left), 0 otherwise |
key_d | int | 1 if D key pressed (strafe right), 0 otherwise |
key_left | int | 1 if Left Arrow pressed (rotate left), 0 otherwise |
key_right | int | 1 if Right Arrow pressed (rotate right), 0 otherwise |
wall_dir | int | Wall direction (unused in current implementation) |
Key states are set to 1 on key press and 0 on key release, enabling smooth continuous movement.
Player Position and Movement
Player Position and Movement
| Field | Type | Description |
|---|---|---|
x | int | Initial player grid X position (row in map) |
y | int | Initial player grid Y position (column in map) |
xx | double | Current player X position in pixels |
yy | double | Current player Y position in pixels |
new_x | double | Proposed new X position (for collision detection) |
new_y | double | Proposed new Y position (for collision detection) |
map_x | int | Current grid cell X (updated during DDA) |
map_y | int | Current grid cell Y (updated during DDA) |
speed | int | Movement speed in pixels per frame (set to 1) |
Raycasting Variables
Raycasting Variables
| Field | Type | Description |
|---|---|---|
ray_dir_x | double | X component of current ray direction vector |
ray_dir_y | double | Y component of current ray direction vector |
delta_dist_x | double | Distance ray travels to cross one X-axis grid cell |
delta_dist_y | double | Distance ray travels to cross one Y-axis grid cell |
side_dist_x | double | Distance from ray origin to next vertical grid line |
side_dist_y | double | Distance from ray origin to next horizontal grid line |
step_x | int | Direction to step in X (-1 or +1) |
step_y | int | Direction to step in Y (-1 or +1) |
side | int | Which wall face was hit (0=vertical, 1=horizontal) |
line_height | int | Height of wall slice to draw (inversely proportional to distance) |
wall_x | double | Exact X or Y coordinate where ray hit wall (for texture mapping) |
d | int | Texture Y offset calculation variable |
end | int | Bottom pixel of wall slice |
Camera Direction Vectors
Camera Direction Vectors
| Field | Type | Description |
|---|---|---|
dir_x | double | X component of camera direction (forward vector) |
dir_y | double | Y component of camera direction (forward vector) |
plane_x | double | X component of camera plane (perpendicular to direction) |
plane_y | double | Y component of camera plane (perpendicular to direction) |
starting_angle | double | Player’s current angle in radians [0, 2π] |
camera_x | double | Current camera space X coordinate for ray being cast |
ray_x | double | Unused |
ray_y | double | Unused |
set_player_dir_and_plane():MiniLibX Graphics Context
MiniLibX Graphics Context
| Field | Type | Description |
|---|---|---|
mlx_ptr | void* | MiniLibX connection pointer (from mlx_init()) |
win_ptr | void* | Window pointer (from mlx_new_window()) |
img_ptr | void* | Image buffer pointer (from mlx_new_image()) |
pixel_ptr | char* | Direct pixel data pointer (from mlx_get_data_addr()) |
bpp | int | Bits per pixel (typically 32) |
line_length | int | Bytes per screen row (WIDTH * bpp / 8) |
endian | int | Endianness flag (0=little-endian, 1=big-endian) |
Texture References
Texture References
| Field | Type | Description |
|---|---|---|
tex_north | t_texture | North wall texture data |
tex_south | t_texture | South wall texture data |
tex_east | t_texture | East wall texture data |
tex_west | t_texture | West wall texture data |
current_tex | t_texture* | Pointer to currently selected texture for drawing |
select_texture() based on side and ray_dir.t_texture Structure
Holds texture image data loaded from XPM files:cub3.h (lines 68-77)
Field Descriptions
| Field | Type | Description |
|---|---|---|
img_ptr | void* | MiniLibX image pointer (from mlx_xpm_file_to_image()) |
data | char* | Raw pixel data buffer (from mlx_get_data_addr()) |
width | int | Texture width in pixels |
height | int | Texture height in pixels |
bpp | int | Bits per pixel |
line_len | int | Bytes per texture row |
endian | int | Endianness (0=little, 1=big) |
Texture Loading
main.c (lines 29-41)
Texture Sampling
During rendering, pixels are sampled fromdata using:
drawing.c (lines 26-27)
tex_x= horizontal position in texture [0, width)tex_y= vertical position in texture [0, height)offset= byte offset into pixel data array
Textures must be in XPM format. The engine does not perform any filtering or mipmapping - pixels are sampled directly.
Configuration Constants
Defined incub3.h (lines 24-66):
Display Constants
| Constant | Value | Usage |
|---|---|---|
WIDTH | 1732 | Screen/window width in pixels |
HEIGHT | 1000 | Screen/window height in pixels |
SIZE | 30 | Grid cell size (affects map scale and collision) |
POV | 1.17 | Point-of-view constant (unused in current implementation) |
Mathematical Constants
Color Constants
0x00RRGGBB.
Hardcoded colors in drawing.c:
- Ceiling:
0x87CEEB(sky blue) - Floor:
0x444444(dark gray)
Key Code Constants
XK_w) from <X11/keysym.h>.
Minimap Constants (Unused)
Error Message Macros
Extensive error messages defined as string literals:msg() utility:
Memory Layout Considerations
Memory Ownership
- Texture paths: Dynamically allocated with
ft_strdup(), freed byfreeall() - Map arrays:
final_mapandtoflood_mapare dynamically allocated withft_calloc(), freed byfreeme() - Texture data: Managed by MiniLibX, freed with
mlx_destroy_image() - MiniLibX context: Freed with
mlx_destroy_window(),mlx_destroy_display(), andfree(mlx_ptr)
Typical Memory Usage
| Component | Approximate Size |
|---|---|
t_map structure | ~250KB (mostly the static map buffer) |
| Screen framebuffer | ~6.7MB (1732×1000×4 bytes) |
| Four textures | Varies (e.g., 4×64×64×4 = 64KB for 64x64 textures) |
| Map arrays | Depends on map size (row count × column count) |
| Total | ~257MB + map data |
The actual memory usage is much smaller than the theoretical maximum since the 500×500 map buffer is rarely fully utilized.