set_texture - 1Fr3aK2/Cub3d GitHub Wiki
📝 set_texture
This function sets the texture paths for the map — including the wall textures (north, south, west, east) and the color or texture values for the floor and ceiling — based on a given input line.
⚙️ Parameters
| Parameter | Type | Description |
|---|---|---|
line |
char * |
The input line containing the texture identifier and its corresponding file path. |
floor |
char ** |
Pointer to the string that will store the floor texture or RGB value. |
ceiling |
char ** |
Pointer to the string that will store the ceiling texture or RGB value. |
map |
t_map * |
Pointer to the map structure where the texture paths for walls are stored. |
🔁 Returns
| Return value | Description |
|---|---|
bool |
Returns true if the texture or color was successfully set, or false in case of an error (invalid format, duplication, or allocation failure). |
📖 Description
The set_texture function processes an input line to extract a key-value pair that represents a texture or color definition.
It supports the following identifiers:
"NO"→ North wall texture"SO"→ South wall texture"WE"→ West wall texture"EA"→ East wall texture"F"→ Floor texture or color (RGB)"C"→ Ceiling texture or color (RGB)
The function splits the input line by spaces using ft_split, expecting exactly two tokens:
- The texture key (
NO,SO,WE,EA,F, orC) - The file path or color string
If the format is invalid, if the key is unrecognized, or if the corresponding texture has already been set, the function frees any allocated memory and returns false.
Otherwise, it duplicates the texture path (or color string) and assigns it to the corresponding field in the t_map structure or the floor/ceiling pointers.
Internal Logic Summary
- Uses
ft_splitto divide the line into parts. - Validates that there are exactly two arguments (key + value).
- Compares the key using
ft_strncmp. - Uses
ft_strdupto assign the texture path to the appropriate field. - Frees temporary memory
free_arrbefore returning.
If any step fails, the function returns false.
💡 Example Usage
t_map map = {0};
char *floor = NULL;
char *ceiling = NULL;
char *line1 = "NO textures/north.png";
char *line2 = "F 220,100,0";
if (set_texture(line1, &floor, &ceiling, &map))
printf("North texture set successfully!\n");
else
printf("Failed to set north texture.\n");
if (set_texture(line2, &floor, &ceiling, &map))
printf("Floor color set successfully!\n");
else
printf("Failed to set floor color.\n");