parse_map - 1Fr3aK2/Cub3d GitHub Wiki

📝 parse_map

Parses the map data from the buffer, loads textures, initializes the map structure, and defines the map height.
This version of the function also handles parsing of floor and ceiling color data during texture loading.


⚙️ Parameters

Parameter Type Description
data t_data * Pointer to the main data structure containing map and texture information. Must be non-NULL.
floor char ** Pointer to the string that stores the RGB values or color data for the floor.
ceiling char ** Pointer to the string that stores the RGB values or color data for the ceiling.

🔁 Returns

Return value Description
true Map successfully parsed, textures loaded, and map structure properly set up.
false Parsing failed due to invalid data pointer, missing textures, or map setup errors. Calls exit_error with an appropriate message before returning.

📖 Description

The parse_map function performs the following steps:

  1. Validation

    • Checks if the data pointer is valid.
    • If data is NULL, triggers exit_error and returns false.
  2. Texture Loading Loop

    • Iterates through data->map.buffer lines until all textures (and optionally floor/ceiling colors) are loaded using check_load_textures.
    • If a buffer line is NULL, the function calls exit_error with a texture-reading error.
    • Empty lines ('\0') are skipped.
    • Each non-empty line is processed through replace_tabs to normalize spacing.
    • Then it calls set_texture to load texture paths or color values.
    • If texture setup fails, exit_error is called.
  3. Map Setup

    • Once textures are loaded, the function initializes the actual map using set_map.
    • If this process fails, it reports an error via exit_error and returns false.
  4. Map Height Calculation

    • Calculates the total number of lines (map height) using ft_stralen and assigns it to data->map.height.
  5. Success Return

    • If all steps succeed, returns true.

💡 Example Usage

t_data data;
char *floor = NULL;
char *ceiling = NULL;

// Assume data.map.buffer is already filled with map lines read from a .cub file
if (!parse_map(&data, &floor, &ceiling))
{
    printf("❌ Failed to parse map or load textures.\n");
    return 1;
}

// ✅ data.map is now initialized with textures, floor/ceiling colors, and full map data
printf("Map successfully parsed! Height: %d\n", data.map.height);