Files
cub3d/src/parsing/lst_textures.c
2026-02-10 15:31:37 +01:00

84 lines
2.4 KiB
C
Executable File

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lst_textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/01 08:48:55 by aortigos #+# #+# */
/* Updated: 2026/02/10 12:40:04 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
int get_index(char *line, int i)
{
while (ft_isspace(line[i]))
i++;
return (i);
}
t_texture_list *new_texture(char *line)
{
t_texture_list *list;
list = (t_texture_list *)ft_calloc(sizeof(t_texture_list), 1);
if (!list)
return (NULL);
while (ft_isspace(*line))
line++;
if ((!ft_strncmp(line, "NO", 2) || !ft_strncmp(line, "SO", 2))
|| !ft_strncmp(line, "WE", 2) || !ft_strncmp(line, "EA", 2))
{
// Extract first 2 chars as identifier
list->name = ft_substr(line, 0, 2);
// Extract everything after identifier
list->value = ft_substr(line, get_index(line, 2), ft_strlen(line));
}
else if ((!ft_strncmp(line, "F", 1) || !ft_strncmp(line, "C", 1)))
{
// Extract first char as identifier
list->name = ft_substr(line, 0, 1);
// Extract RGB values after identifier
list->value = ft_substr(line, get_index(line, 1), ft_strlen(line));
}
list->next = NULL;
return (list);
}
void lst_back_ture(t_texture_list **l_ture, t_texture_list *new)
{
t_texture_list *tmp;
// If list is empty, new nodes becomes head
if (!*l_ture)
{
(*l_ture) = new;
return ;
}
tmp = *l_ture;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
int lst_ture(t_data *m, t_texture_list **l_ture)
{
int i;
t_texture_list *tmp;
i = 0;
// Process all 6 texture directives
while (m->texture2d[i])
{
// Create node for each texture line
tmp = new_texture(m->texture2d[i++]);
if (!tmp)
return (0);
// Add to end of list
lst_back_ture(l_ture, tmp);
}
return (1);
}