Skip to main content

Overview

Cub3D uses .cub files to define game maps. These files contain texture paths, colors, and the map layout in a specific format that the parser reads and validates.

File Structure

A .cub file consists of three main sections:
  1. Texture definitions (NO, SO, WE, EA)
  2. Color definitions (F, C)
  3. Map grid

Complete Example

Here’s a real map from the Cub3D project:
maps/Engel.cub
SO ./doom2.xpm
NO                      ./doom.xpm      
WE ./doom3.xpm
EA ./doom4.xpm

F 220,100,0
C 225,30,0

1111111111111111111111111
1000000000110000000000001
10110000N1110000000000001
10010000000000000000011111111111
111111111011000001110000000000001
1000000000110000011100000000111111
11110111111111011100000010001
11110000000000011101010010001111
11000000100101011100000010001111
10000000000000001100000010001
10000001110000011101010010001
11000001 1010101 111011110111
1000000111000001110000000000111
1111111111011111111111111111110

Texture Definitions

Cardinal Direction Textures

Four texture paths must be defined, one for each cardinal direction:
NO ./doom.xpm
The texture identifiers can appear in any order and can have whitespace before or after the path. All four textures are required.

Available Textures

The Cub3D project includes several texture files:
  • doom.xpm, doom2.xpm, doom3.xpm, doom4.xpm - DOOM-style wall textures
  • eagle.xpm - Eagle texture
  • pizza.xpm - Pizza texture
  • greystone.xpm - Grey stone texture
  • purplestone.xpm - Purple stone texture
  • gang.xpm - Custom texture
  • blen_textura.xpm - Blended texture
All textures are in XPM (X PixMap) format, which is required by the MiniLibX graphics library.

Color Definitions

Floor Color

Defined with F followed by RGB values (0-255):
F 220,100,0
This creates an orange floor color.

Ceiling Color

Defined with C followed by RGB values (0-255):
C 225,30,0
This creates a red ceiling color.
RGB values must be in the range 0-255. Values outside this range will cause a parsing error (RGBERROR2).

Color Validation

The parser validates colors using these functions:
cub3.h:198-202
int		isvalidnum(char **arr2);
int		isrgbable(char **arr2);
Colors are converted to integer format:
main.c:63-64
mapa->floor_color = parse_color(mapa->floorcolor);
mapa->ceiling_color = parse_color(mapa->ceilingcolor);

Map Grid Format

Valid Characters

The map grid uses the following characters:
CharacterMeaning
0Empty space (walkable)
1Wall
NPlayer starting position facing North
SPlayer starting position facing South
EPlayer starting position facing East
WPlayer starting position facing West
(space)Empty space outside the map

Player Position

1

Single player spawn

The map must contain exactly one player position marker (N, S, E, or W).
2

Starting angle

The character determines the initial viewing direction:
  • N - Facing north (angle = 3π/2)
  • S - Facing south (angle = π/2)
  • E - Facing east (angle = 0)
  • W - Facing west (angle = π)
3

Position initialization

The player is placed at the center of the tile:
main.c:53-54
mapa->xx = mapa->y * SIZE + (SIZE / 2);
mapa->yy = mapa->x * SIZE + (SIZE / 2);

Map Examples

111
1N1
111

Validation Rules

Map Closure

The map must be completely enclosed by walls. The parser uses a flood fill algorithm to verify closure:
cub3.h:203
void		flood_fill(t_map *mapa, char **map, int x, int y);
If the map is not properly enclosed, the parser will exit with error: MAPERROR4 "Map is not closed"

Required Elements

The parser checks for all required elements:
cub3.h:179
int			check3atributtes(t_map *map);
This ensures:
  • All four texture paths are defined (NO, SO, WE, EA)
  • Floor color is defined (F)
  • Ceiling color is defined (C)
  • Exactly one player position exists
  • Map contains only valid characters

Error Messages

Common parsing errors defined in cub3.h:
Error CodeMessageMeaning
MAPERROR3Empty mapNo map data found
MAPERROR4Map is not closedWalls don’t enclose the map
MAPERROR10Attribute not foundMissing required texture/color
MAP_ARG_DUPAttribute found more than onceDuplicate definition
SPOTERRORShould have only one playerMultiple or no player positions
RGBERROR2Invalid RGB valueColor value outside 0-255 range

File Format Requirements

1

File extension

Must end with .cub extension
cub3.h:44
#define ARGERROR3 "El mapa tiene que acabar en .cub\n"
2

Printable names

Filename must contain only printable characters
3

Whitespace handling

The parser handles arbitrary whitespace around texture paths and color definitions
4

Empty lines

Empty lines between sections are allowed and ignored

Map Storage

Internally, maps are stored in the t_map structure:
cub3.h:79-96
typedef struct s_map
{
    char		*northtexture;
    char		*southtexture;
    char		*westtexture;
    char		*easttexture;
    char		*floorcolor;
    char		*ceilingcolor;
    int			floor_color;
    int			ceiling_color;
    char		map[500][500];
    char		*path;
    char		**final_map;
    char		**toflood_map;
    char		**spaceless_map;
    int			map_height;
    int			map_width;
    // ...
}		t_map;
The maximum map size is 500x500 tiles as defined by the map[500][500] array.

Next Steps

Textures

Learn about texture loading and XPM format

Raycasting

Understand how the map is rendered in 3D