init_map - 1Fr3aK2/Cub3d GitHub Wiki

📝 init_map

Initializes the game map by reading, validating, and processing data from a .cub file.
This function prepares all essential map elements such as floor and ceiling colors, textures, and the player’s initial position.


⚙️ Parameters

Parameter Type Description
file char * Path to the map file to be loaded. Must be a valid string ending with the .cub extension.
data t_data * Pointer to the main data structure containing the map, textures, and player information. Must not be NULL.

🔁 Returns

Return value Description
1 Map successfully initialized and ready for use.
-1 Invalid input parameters (file or data is NULL).

⚠️ Note: If any of the internal validation or parsing steps fail, the function calls exit_error() and terminates the program immediately.


📖 Description

The init_map function is responsible for setting up the entire game map environment.
It ensures the provided .cub file is valid, loads its contents, parses the map data, validates integrity, sets color information, and initializes the player’s starting position.

Step-by-step process:

  1. Parameter validation – Checks if both file and data are non-NULL; if not, returns -1.
  2. Initialize variables – Sets floor and ceiling pointers to NULL.
  3. Filename validation – Calls check_map_name to ensure the provided file name has a valid .cub extension.
  4. Read file lines – Loads the map file content into memory using get_lines.
  5. Parse map data – Extracts and processes the map layout and color definitions via get_map.
  6. Validate map structure – Verifies that the map is properly closed and valid through check_map.
    • If validation fails, the program terminates with exit_error().
  7. Set floor and ceiling colors – Converts RGB string data into usable color values using set_fc_rgb.
    • If this process fails, the function terminates with exit_error().
  8. Initialize player – Configures the player’s initial position and direction via init_player.

Once all steps are successfully executed, the map data is ready for use by the rendering and gameplay systems.


💡 Example Usage

t_data data;
int result;

result = init_map("maps/level1.cub", &data);
if (result == -1)
{
    printf("Error: invalid input parameters.\n");
}
else
{
    printf("Map initialized successfully!\n");
}