set_fc_rgb - 1Fr3aK2/Cub3d GitHub Wiki

📝 set_fc_rgb

Parses and sets the RGB color values for the floor and ceiling, converting their string representations into integer RGB values and storing them inside the main data structure.


⚙️ Parameters

Parameter Type Description
floor char * String containing the RGB color for the floor in the format "R,G,B".
ceiling char * String containing the RGB color for the ceiling in the format "R,G,B".
data t_data * Pointer to the main game data structure where the parsed RGB values will be stored.

🔁 Returns

Return value Description
bool Returns true if both ceiling and floor RGB values are successfully parsed and set, otherwise false.

📖 Description

The set_fc_rgb function processes the color data for both the ceiling and floor of the game map.
It converts color strings (e.g., "220,100,0") into integer RGB representations using the helper function set_rgb.

The function performs the following steps:

  1. Validation: Checks that all input pointers (floor, ceiling, and data) are valid. If any are NULL, returns false.
  2. Ceiling color:
    • Calls set_rgb(ceiling, data) to convert the ceiling string to an RGB integer.
    • Stores the result in data->map.rgb_ceiling.
    • Frees the original ceiling string to avoid memory leaks.
  3. Floor color:
    • Calls set_rgb(floor, data) to convert the floor string to an RGB integer.
    • Stores the result in data->map.rgb_floor.
    • Frees the original floor string.
  4. If any conversion fails, the function immediately returns false.
  5. Returns true only if both ceiling and floor RGB values are correctly assigned.

This function ensures that all string-based color data are converted into usable RGB integers for rendering and memory is managed properly by freeing temporary strings.


💡 Example Usage

t_data data;
char *ceiling = ft_strdup("200,150,100");
char *floor   = ft_strdup("120,80,40");

if (set_fc_rgb(floor, ceiling, &data))
    printf("Ceiling and floor RGB values successfully set!\n");
else
    printf("Failed to set RGB color values.\n");