Skip to main content

Overview

Cub3D is organized into modular source files, each handling specific responsibilities in the raycasting engine. The codebase follows the 42 school norm and is structured for clarity and maintainability.

Project Structure

cub3d/
├── cub3.h              # Main header file with all declarations
├── main.c              # Entry point and initialization
├── raycasting.c        # DDA algorithm and ray calculations
├── drawing.c           # Pixel manipulation and rendering
├── utils*.c            # Helper functions (utils through utils7)
├── freemem.c           # Memory management and cleanup
├── parselineutils.c    # Map file parsing utilities
├── asignatributes.c    # Configuration attribute assignment
├── Makefile            # Build system
├── libft/              # Custom C library
└── minilibx-linux/     # Graphics library

Core Modules

main.c - Entry Point & Initialization

Handles program startup, MLX initialization, and the main game loop.
void	cubed_init(t_map *mapa)
{
	mapa->xx = mapa->y * SIZE + (SIZE / 2);
	mapa->yy = mapa->x * SIZE + (SIZE / 2);
	mapa->speed = 1;
	mapa->key_a = 0;
	mapa->key_w = 0;
	mapa->key_d = 0;
	mapa->key_s = 0;
	mapa->key_left = 0;
	mapa->key_right = 0;
	set_player_dir_and_plane(mapa);
	mapa->floor_color = parse_color(mapa->floorcolor);
	mapa->ceiling_color = parse_color(mapa->ceilingcolor);
	mapa->mlx_ptr = mlx_init();
	load_all_textures(mapa);
	mapa->win_ptr = mlx_new_window(mapa->mlx_ptr, WIDTH, HEIGHT, mapa->name);
	mapa->img_ptr = mlx_new_image(mapa->mlx_ptr, WIDTH, HEIGHT);
	mapa->pixel_ptr = mlx_get_data_addr(mapa->img_ptr, &mapa->bpp,
			&mapa->line_length, &mapa->endian);
	mlx_put_image_to_window(mapa->mlx_ptr, mapa->win_ptr, mapa->img_ptr, 0, 0);
}
Responsibilities:
  • Argument validation
  • Map file parsing
  • MLX window and image creation
  • Texture loading
  • Event hook registration
  • Main render loop

raycasting.c - DDA Algorithm

Implements the Digital Differential Analyzer (DDA) algorithm for raycasting.
raycasting.c:102-127
void	raycasting(t_map *mapa, int i)
{
	double	camera_x;
	double	side_x;
	double	side_y;
	double	dist;
	int		start_y;

	while (++i < WIDTH)
	{
		camera_x = 2 * i / (double)WIDTH - 1;
		mapa->ray_dir_x = mapa->dir_x + mapa->plane_x * camera_x;
		mapa->ray_dir_y = mapa->dir_y + mapa->plane_y * camera_x;
		init_dda(mapa, &side_x, &side_y);
		perform_dda(mapa, &side_x, &side_y);
		calculate_wall_data(mapa, &dist);
		select_texture(mapa);
		start_y = (HEIGHT - mapa->line_height) / 2;
		mapa->end = start_y + mapa->line_height;
		if (start_y < 0)
			start_y = 0;
		if (mapa->end > HEIGHT)
			mapa->end = HEIGHT;
		draw_3d_dda(i, start_y, mapa->end, mapa);
	}
}
Key Functions:
  • raycasting() - Main loop casting rays for each screen column
  • init_dda() - Initialize ray direction and step values
  • perform_dda() - Walk the ray until wall hit
  • calculate_wall_data() - Compute wall distance and height
  • select_texture() - Choose appropriate wall texture

drawing.c - Rendering

Handles pixel-level rendering and texture mapping.
drawing.c:52-66
void	draw_3d_dda(int x, int start_y, int end_y, t_map *mapa)
{
	int	y;

	y = -1;
	while (++y < start_y)
		put_pixel(x, y, 0x87CEEB, mapa);
	draw_wall (x, start_y, end_y, mapa);
	y = end_y;
	while (y < HEIGHT)
	{
		put_pixel(x, y, 0x444444, mapa);
		y++;
	}
}
Responsibilities:
  • Ceiling rendering (sky blue)
  • Wall texture mapping
  • Floor rendering (dark gray)
  • Pixel buffer manipulation

freemem.c - Memory Management

Centralized memory cleanup to prevent leaks.
freemem.c:51-56
void	freeall(t_map *map)
{
	freetextures(map);
	freeme(map->toflood_map);
	freeme(map->final_map);
}
Functions:
  • ft_memdel() - Safe single pointer deallocation
  • freeme() - Free 2D arrays
  • freetextures() - Free all texture paths and strings
  • freeall() - Complete cleanup wrapper

utils*.c - Helper Modules

Seven utility files containing various helper functions:
1

utils.c

Map file validation and attribute parsing entry points
2

utils2.c

String manipulation and map copying utilities
3

utils3.c

Flood fill algorithm and map closure validation
4

utils4.c

RGB color validation and parsing
5

utils5.c

Window management and player angle initialization
6

utils6.c

Player movement, rotation, and rendering loop
utils6.c:74-91
int	cubed_render(t_map *mapa)
{
	mapa->new_x = mapa->xx;
	mapa->new_y = mapa->yy;
	move_cubed(mapa, mapa->dir_x, mapa->dir_y);
	rotate_cubed (mapa);
	set_player_dir_and_plane(mapa);
	clear_image(mapa);
	raycasting (mapa, -1);
	mlx_put_image_to_window(mapa->mlx_ptr, mapa->win_ptr, mapa->img_ptr, 0, 0);
	return (0);
}
7

utils7.c

Keyboard event handling and color parsing

parselineutils.c - Map Parsing

Handles .cub file parsing and attribute extraction.
parselineutils.c:64-82
void	parsefloorceilingdirections(t_map *map, int i, int fd)
{
	char	*line;

	while (i)
	{
		line = get_next_line(fd);
		if (i == 1 && line == NULL)
			return (msg(2, MAPERROR3), freetextures(map), exit(1));
		if (line && ft_strcmp(line, "\n"))
			checkatributes(map, line);
		if (all_atributes_asigned(map))
			return (ft_memdel(line));
		if (line == NULL)
			break ;
		ft_memdel(line);
		i++;
	}
}

Header File Structure

cub3.h

Centralized header containing all type definitions and function declarations. Includes:
cub3.h:16-22
# include "minilibx-linux/mlx.h"
# include "libft/libft.h"
# include <X11/X.h>
# include <X11/keysym.h>
# include <stdio.h>
# include <math.h>
# include <string.h>
Constants:
  • Screen dimensions: WIDTH (1732) and HEIGHT (1000)
  • Math constants: PI, field of view POV (1.17)
  • Map tile size: SIZE (30)
  • Color definitions: RED, GREEN, WHITE, BLACK, YELLOW
  • Key codes: W, A, S, D
  • Error message macros
Data Structures:
cub3.h:68-77
typedef struct s_texture
{
    void	*img_ptr;
    char	*data;
    int		width;
    int		height;
    int		bpp;
    int		line_len;
    int		endian;
}	t_texture;
Stores texture image data and metadata for wall rendering.

Dependencies

libft

Custom C library providing standard functions:
  • String manipulation (ft_strdup, ft_strlen, ft_substr, ft_split)
  • Memory operations
  • File I/O (get_next_line)
  • Type checking

MiniLibX

Graphics library providing:
  • Window management (mlx_new_window)
  • Image creation (mlx_new_image)
  • Pixel manipulation (mlx_get_data_addr)
  • Event hooks (mlx_hook, mlx_loop_hook)
  • XPM texture loading (mlx_xpm_file_to_image)

Function Naming Conventions

All functions follow the 42 school norm:
  • Snake_case naming
  • Descriptive names indicating purpose
  • Module-specific prefixes (e.g., ft_ for libft functions)
  • Maximum 25 lines per function (with some exceptions)
Examples:
  • init_dda() - Initialize DDA algorithm
  • perform_dda() - Execute DDA algorithm
  • load_texture() - Load single texture
  • parse_color() - Parse RGB color string
  • is_wall() - Wall collision detection

Module Interactions

1

Initialization

main.c validates arguments and calls ismap() from utils.c
2

Parsing

parselineutils.c reads .cub file and asignatributes.c stores configuration
3

Validation

utils3.c validates map closure using flood fill algorithm
4

Setup

main.c initializes MLX and loads textures
5

Game Loop

utils6.c:cubed_render()raycasting.cdrawing.c → pixel buffer
6

Cleanup

freemem.c handles all memory deallocation on exit

Build System

The Makefile defines the complete build process:
Makefile:13-29
CC			 = cc
LIBFT_DIR    = libft/
LIBFT        = $(LIBFT_DIR)libft.a
MINILIBX_DIR = minilibx-linux/
MINILIBX     = $(MINILIBX_DIR)libmlx.a
HEADERS      = -I $(LIBFT_DIR)
CFLAGS       = -Wall -Wextra -Werror -O3 -g -g3

NAME         = cub3d
RM           = rm -rf
SRC = main.c  utils.c utils2.c utils3.c utils4.c utils5.c utils6.c utils7.c freemem.c parselineutils.c asignatributes.c raycasting.c drawing.c

OBJS = $(SRC:.c=.o)

all: $(MINILIBX) $(LIBFT) $(NAME)
$(NAME): $(MINILIBX) $(OBJS)
	$(CC) $(CFLAGS) -o $(NAME) $(OBJS) -L$(MINILIBX_DIR) -lmlx -L$(LIBFT_DIR) -lft -lm -lX11 -lXext
Compiler Flags:
  • -Wall -Wextra -Werror - Maximum warnings as errors
  • -O3 - Optimization level 3 for performance
  • -g -g3 - Full debugging symbols
Linked Libraries:
  • -lmlx - MiniLibX graphics
  • -lft - Custom libft
  • -lm - Math library
  • -lX11 -lXext - X Window System
The project must compile without warnings. The Makefile uses -Werror which treats all warnings as compilation errors.