Skip to main content

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.
t_map *parse_map_e(char *path);
path
char *
required
File path to the .cub map file to parse
return
t_map *
Pointer to the parsed map structure, or NULL on error

Parsing Process

The function performs parsing in four stages:
  1. Allocation & Validation - Allocates map structure and validates .cub extension
  2. Raw Reading - Reads all lines from the file into map->raw
  3. Processing - Processes raw lines to extract map grid and metadata
  4. Identifier Parsing - Parses entity type definitions and configurations
  5. Size Calculation - Determines map dimensions
If any stage fails, the function cleans up partial state and returns NULL.

File Format Requirements

  • File must have .cub extension
  • File must be readable
  • Must contain valid map grid and configuration

Usage Example

// From loop.c - Loading a new map
t_map *map = parse_map_e("maps/level1.cub");
if (!map) {
    // Handle parsing error
    return;
}

// Use map to create game
t_game *game = game_new_e(window, map);
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.
void clear_map(void *map);
map
void *
required
Pointer to the map instance to clear (cast from t_map *)

What Gets Cleared

  • raw - Array of raw file lines
  • types - Hashmap of entity type configurations
  • identifiers - Hashmap of entity identifier mappings
  • path - Stored file path string
The 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.
void free_map(t_map *map);
map
t_map *
required
Pointer to the map instance to free

Usage Example

// From loop.c - Cleaning up before loading new map
free_map(cub3d()->curr_map);
cub3d()->curr_map = NULL;

Map Structure

t_map

Contains all parsed map data and configuration.
struct s_map
{
    char        *path;          // Path to the .cub file
    t_hashmap   *types;         // Entity type configurations
    char        **raw;          // Raw file lines
    char        **map;          // Processed 2D map grid
    t_size      size;           // Map dimensions (width, height)
    t_hashmap   *identifiers;   // Entity identifier -> creator mappings
};
path
char *
The file path to the original .cub file
types
t_hashmap *
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 string
  • CONTROLLER: Controller type name
raw
char **
Null-terminated array of strings containing the raw file contents, one line per element
map
char **
Null-terminated array of strings representing the processed 2D map grid. Each character represents a tile type or entity.
size
t_size
The dimensions of the map grid (width and height in tiles)
identifiers
t_hashmap *
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:
#define DEFAULT_MAP_PATH "maps/hub.cub"
#define DEFAULT_AIR_TYPES "0 \t\n\v\f\r"
#define DEFAULT_WALL_TYPES "1"
#define DEFAULT_PLAYER_TYPES "NSEW"
DEFAULT_AIR_TYPES
string
Characters that represent walkable/empty space
DEFAULT_WALL_TYPES
string
Characters that represent solid walls
DEFAULT_PLAYER_TYPES
string
Characters that represent player spawn points with initial facing direction:
  • N: North
  • S: South
  • E: East
  • W: West

Usage Patterns

Loading and Switching Maps

// Parse new map
t_map *new_map = parse_map_e("maps/level2.cub");
if (!new_map) {
    fprintf(stderr, "Failed to load map\n");
    return;
}

// Clean up old game and map
free_game(current_game);
free_map(current_map);

// Create new game with new map
current_game = game_new_e(window, new_map);
current_map = new_map;

Accessing Map Data

// Get map dimensions
int width = map->size.width;
int height = map->size.height;

// Access tile at position (x, y)
if (y < height && x < ft_strlen(map->map[y])) {
    char tile = map->map[y][x];
    // Check tile type
    if (ft_strchr(DEFAULT_WALL_TYPES, tile)) {
        // It's a wall
    }
}

// Get entity type configuration
char *is_targetable = hashmap_get_with_identifier(
    game, map->types, identifier, "TARGETABLE");

Error Handling

All map functions use the error flagging system (fte):
t_map *map = parse_map_e(path);
if (!map) {
    // Error details are set via fte_set()
    // Common errors:
    // - "map alloc" - Memory allocation failed
    // - "invalid map" - Invalid file extension or path
    // - "map open" - File couldn't be opened
    // - "map read" - Reading file contents failed
    return;
}

See Also

  • Game - Game instance creation and management
  • Entities - Entity system and spawning

Build docs developers (and LLMs) love