Overview
The map module handles parsing and managing .cub map files that define level layouts, entity placements, and configuration. Maps contain both the 2D grid layout and metadata like textures, colors, and entity definitions.Functions
parse_map_e()
Parses a .cub map file and returns a structured map object.File path to the .cub map file to parse
Pointer to the parsed map structure, or NULL on error
Parsing Process
The function performs parsing in four stages:- Allocation & Validation - Allocates map structure and validates .cub extension
- Raw Reading - Reads all lines from the file into
map->raw - Processing - Processes raw lines to extract map grid and metadata
- Identifier Parsing - Parses entity type definitions and configurations
- Size Calculation - Determines map dimensions
File Format Requirements
- File must have
.cubextension - File must be readable
- Must contain valid map grid and configuration
Usage Example
The map path is stored in the map structure for reference and potential reloading.
clear_map()
Clears all dynamically allocated data within a map structure without freeing the structure itself.Pointer to the map instance to clear (cast from t_map *)
What Gets Cleared
raw- Array of raw file linestypes- Hashmap of entity type configurationsidentifiers- Hashmap of entity identifier mappingspath- Stored file path string
map array itself is not freed as it may point into the raw array.
This function is safe to call with NULL. It checks for NULL before proceeding.
free_map()
Completely frees a map instance including the structure itself.Pointer to the map instance to free
Usage Example
Map Structure
t_map
Contains all parsed map data and configuration.The file path to the original .cub file
Hashmap storing entity type configurations. Keys are entity identifiers, values are configuration strings.Example configurations:
TARGETABLE: “TRUE” or “FALSE”TRANSPARENT: “TRUE” or “FALSE”MAX_HEALTH: Numeric stringCONTROLLER: Controller type name
Null-terminated array of strings containing the raw file contents, one line per element
Null-terminated array of strings representing the processed 2D map grid. Each character represents a tile type or entity.
The dimensions of the map grid (width and height in tiles)
Hashmap mapping entity identifier characters to their creator functions (t_type_creator). Used during entity spawning.
Map Configuration
Default Map Types
Defined in cub3d.h:Characters that represent walkable/empty space
Characters that represent solid walls
Characters that represent player spawn points with initial facing direction:
- N: North
- S: South
- E: East
- W: West