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:

  1. Validate inputs:
    Checks that file_name and data are not NULL and that data->map.height > 0.
    If any condition fails, calls exit_error("Inv pointer file/struct").

  2. Open the map file:
    Opens the file in read-only mode (O_RDONLY).
    If the file cannot be opened, calls exit_error("Error opening the file").

  3. Allocate buffer memory:
    Allocates space for data->map.buffer based on the number of map lines (data->map.height + 1).
    If allocation fails, calls exit_error("Memory allocation error").

  4. Initialize the buffer:
    Calls start_buffer(data) to prepare the map buffer for reading.

  5. Load the map lines:
    Calls alloc_buffer(data, &i) to read each line of the map file into the buffer.
    If the function does not return 1, calls exit_error("GET_MAP ERROR W/ALLOC BUFFER").

  6. Parse map and colors:
    Calls parse_map(data, floor, ceiling) to validate the map structure and extract floor/ceiling color information.
    If parsing fails, calls exit_error("GET_MAP ERROR W/PARSE_MAP").

  7. Close the file:
    After successful parsing, closes the file descriptor with close(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