Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ cub3d Public

A raycasting-based 3D engine built in C using MiniLibX inspired by Wolfenstein 3D. Learn low-level graphics, raycasting, and memory-safe engine design through the 42 Cub3D project.

Notifications You must be signed in to change notification settings

xidruk/cub3d

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 

Repository files navigation

Cub3D ~ 42 Project

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.


>> Project Concept

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

>> Game Overview

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

>> Project Architecture

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)

>> Core Systems

Memory Management -> galloc

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.

File Reader -> get_line

A lightweight version of get_next_line used to read .cub configuration files safely and efficiently.

Program Cargo Unit -> PCU

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.

Parsing and Configuration

The .cub parser collects:

  1. Texture paths (N/S/E/W)
  2. Floor and ceiling colors
  3. The map grid itself

Once validated, the data is packed into a t_right_config structure and passed to the game engine.


>> Raycasting 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.

Texture Mapping

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.


>> Technical Details

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

>> Available Maps / Screenshots

Below are the included map files and accompanying screenshots located in the media/ directory.

Map Name Preview
Cerberus Cerberus
Lion of Venice Lion of Venice
Zellige Zellige

Each map features unique layouts, textures, and lighting styles. Use these screenshots to showcase the level design and texture work.


>> How to Run

make
./cub3D static/maps/cerberus-map.cub

You can replace cerberus-map.cub with any map file available in static/maps/.


>> What We Learned

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.


>> Credits

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.

About

A raycasting-based 3D engine built in C using MiniLibX inspired by Wolfenstein 3D. Learn low-level graphics, raycasting, and memory-safe engine design through the 42 Cub3D project.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •