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:
- Texture definitions (NO, SO, WE, EA)
- Color definitions (F, C)
- Map grid
Complete Example
Here’s a real map from the Cub3D project:maps/Engel.cub
Texture Definitions
Cardinal Direction Textures
Four texture paths must be defined, one for each cardinal direction: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 textureseagle.xpm- Eagle texturepizza.xpm- Pizza texturegreystone.xpm- Grey stone texturepurplestone.xpm- Purple stone texturegang.xpm- Custom textureblen_textura.xpm- Blended texture
Color Definitions
Floor Color
Defined withF followed by RGB values (0-255):
Ceiling Color
Defined withC followed by RGB values (0-255):
Color Validation
The parser validates colors using these functions:cub3.h:198-202
main.c:63-64
Map Grid Format
Valid Characters
The map grid uses the following characters:| Character | Meaning |
|---|---|
0 | Empty space (walkable) |
1 | Wall |
N | Player starting position facing North |
S | Player starting position facing South |
E | Player starting position facing East |
W | Player starting position facing West |
| (space) | Empty space outside the map |
Player Position
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 = π)
Map Examples
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
Required Elements
The parser checks for all required elements:cub3.h:179
- 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 Code | Message | Meaning |
|---|---|---|
MAPERROR3 | Empty map | No map data found |
MAPERROR4 | Map is not closed | Walls don’t enclose the map |
MAPERROR10 | Attribute not found | Missing required texture/color |
MAP_ARG_DUP | Attribute found more than once | Duplicate definition |
SPOTERROR | Should have only one player | Multiple or no player positions |
RGBERROR2 | Invalid RGB value | Color value outside 0-255 range |
File Format Requirements
Whitespace handling
The parser handles arbitrary whitespace around texture paths and color definitions
Map Storage
Internally, maps are stored in thet_map structure:
cub3.h:79-96
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