get_map - 1Fr3aK2/Cub3d GitHub Wiki
📝 get_map
Loads the map from a file into memory, performing all necessary validation, parsing, and setup for floor and ceiling colors.
⚙️ Parameters
| Parameter | Type | Description |
|---|---|---|
file_name |
char * |
Path to the map file to be loaded. Must be a valid string and point to an existing .cub file. |
floor |
char ** |
Pointer to a string where the floor color or RGB data will be stored after parsing. |
ceiling |
char ** |
Pointer to a string where the ceiling color or RGB data will be stored after parsing. |
data |
t_data * |
Pointer to the main data structure that stores file descriptors, map buffers, and texture data. Must be non-NULL and have a valid map.height. |
🔁 Returns
| Return value | Description |
|---|---|
void |
The function does not return. If any error occurs (invalid pointers, file cannot be opened, memory allocation failure, or parsing error), it calls exit_error() and terminates the program. |
📖 Description
The get_map function loads and parses the map data from the specified file.
It validates all input parameters, opens the map file, allocates memory for the buffer, and processes the map lines.
In addition, it parses the floor and ceiling color values for later rendering use.
Detailed Execution Steps:
-
Validate inputs:
Checks thatfile_nameanddataare not NULL and thatdata->map.height > 0.
If any condition fails, callsexit_error("Inv pointer file/struct"). -
Open the map file:
Opens the file in read-only mode (O_RDONLY).
If the file cannot be opened, callsexit_error("Error opening the file"). -
Allocate buffer memory:
Allocates space fordata->map.bufferbased on the number of map lines (data->map.height + 1).
If allocation fails, callsexit_error("Memory allocation error"). -
Initialize the buffer:
Callsstart_buffer(data)to prepare the map buffer for reading. -
Load the map lines:
Callsalloc_buffer(data, &i)to read each line of the map file into the buffer.
If the function does not return1, callsexit_error("GET_MAP ERROR W/ALLOC BUFFER"). -
Parse map and colors:
Callsparse_map(data, floor, ceiling)to validate the map structure and extract floor/ceiling color information.
If parsing fails, callsexit_error("GET_MAP ERROR W/PARSE_MAP"). -
Close the file:
After successful parsing, closes the file descriptor withclose(data->file.fd).
This function ensures the map and color data are correctly loaded into memory and validated before being used by the rendering engine.
💡 Example Usage
t_data data;
char *floor = NULL;
char *ceiling = NULL;
// Assuming data.map.height was set previously
data.map.height = 12;
get_map("maps/level1.cub", &floor, &ceiling, &data);
// After execution:
// - data.map.buffer contains the map lines
// - floor and ceiling contain parsed RGB color strings