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:
-
Validation
- Checks if the
datapointer is valid. - If
dataisNULL, triggersexit_errorand returnsfalse.
- Checks if the
-
Texture Loading Loop
- Iterates through
data->map.bufferlines until all textures (and optionally floor/ceiling colors) are loaded usingcheck_load_textures. - If a buffer line is
NULL, the function callsexit_errorwith a texture-reading error. - Empty lines (
'\0') are skipped. - Each non-empty line is processed through
replace_tabsto normalize spacing. - Then it calls
set_textureto load texture paths or color values. - If texture setup fails,
exit_erroris called.
- Iterates through
-
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_errorand returnsfalse.
- Once textures are loaded, the function initializes the actual map using
-
Map Height Calculation
- Calculates the total number of lines (map height) using
ft_stralenand assigns it todata->map.height.
- Calculates the total number of lines (map height) using
-
Success Return
- If all steps succeed, returns
true.
- If all steps succeed, returns
💡 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);