Cub3D is a raycasting-based 3D engine inspired by the classic Wolfenstein 3D game.
It’s built from scratch in C using the MiniLibX graphical library, with a custom memory management system, map parser, and rendering engine.
The goal of Cub3D is to learn and implement raycasting, the technique used to render pseudo-3D environments using 2D maps.
Each frame is generated by casting rays from the player’s point of view, calculating wall distances, and mapping textures accordingly.
This project trains you to:
- Understand low-level graphics rendering
- Manage complex project architecture in C
- Build clean and safe memory management systems
- Parse structured files and convert them into runtime configurations
Step into a simple 3D world built entirely from a 2D map!
- Move around using W, A, S, D
- Rotate the camera using ← / →
- Exit safely using ESC
Each .cub file defines:
- Wall textures (north, south, west, east)
- Floor and ceiling colors
- The map layout and player start position
macOS/
├── Makefile
├── mandatory/
│ ├── galloc/ # Custom garbage collector
│ ├── get_line/ # get_next_line-like system for file reading
│ ├── groot/ # Core game logic, parsing, rendering, and movement
│ ├── includes/ # Header files (definitions, structs, prototypes)
│ ├── main.c # Entry point
│ ├── mlxlib/ # MiniLibX static library
│ ├── pcargo/ # PCU system: manages runtime containers
│ └── uglob/ # Utility functions (memory ops, string utils, etc.)
└── static/
├── maps/ # Map configuration files (.cub)
└── textures/ # Game textures (.xpm)We use a custom garbage collector implemented in galloc/, responsible for tracking all allocated memory via a pointer pool.
This ensures every dynamically allocated element is properly freed before exit — no leaks, no chaos.
A lightweight version of get_next_line used to read .cub configuration files safely and efficiently.
All runtime data is wrapped inside a central container:
typedef struct s_pcu {
t_galloc *galloc_container; // Memory manager
t_fds *fd_pool; // File descriptors
t_config *config; // Parsed configuration
t_right_config *rconfig; // Validated runtime config
t_game *game; // Main game struct
t_ray *ray; // Raycasting data
} t_pcargo;This system lets us easily access, manage, and clean every subsystem — from textures to map data — without scattering resources.
The .cub parser collects:
- Texture paths (N/S/E/W)
- Floor and ceiling colors
- The map grid itself
Once validated, the data is packed into a t_right_config structure and passed to the game engine.
We implemented a Digital Differential Analyzer (DDA) algorithm to trace rays from the player’s position through the map grid.
- Each ray detects wall intersections.
- The distance is used to compute the correct height of the wall slice.
- The texture is then mapped proportionally to the wall face for realism.
Each wall face (N, S, E, W) has its own .xpm image texture.
We map these onto the walls dynamically based on the hit direction and ray impact point.
This teaches how to project large textures into smaller pixel grids efficiently.
| Feature | Description |
|---|---|
| Language | C |
| Graphics | MiniLibX (macOS) |
| Memory | Custom garbage collector |
| Parsing | Custom configuration parser |
| Algorithm | Raycasting (DDA) |
| Exit Handling | Full cleanup through galloc and PCU systems |
Below are the included map files and accompanying screenshots located in the media/ directory.
| Map Name | Preview |
|---|---|
| Cerberus | |
| Lion of Venice | |
| Zellige |
Each map features unique layouts, textures, and lighting styles. Use these screenshots to showcase the level design and texture work.
make
./cub3D static/maps/cerberus-map.cubYou can replace cerberus-map.cub with any map file available in static/maps/.
Working on Cub3D was a deep dive into:
- 3D rendering logic with 2D maps
- Raycasting and DDA mathematics
- Texture mapping and scaling
- Project architecture and modularity
- Team collaboration and version control
- Memory management and clean program exit
This project blends mathematics, graphics, and systems programming — a perfect foundation for deeper computer graphics or engine development.
Developed as part of the 42 Network curriculum by kbarkan and oadouz. Special thanks to MiniLibX and the 42 community for the technical and creative inspiration.