cube3D/map/parsing_header2.c
2023-05-16 12:47:04 +00:00

124 lines
2.6 KiB
C

#include "./parsing_private.h"
#include "map.h"
static int set_texture(t_map *map, int token, const char *key, char *value)
{
if (map->img_path[token - 1] != NULL)
{
ft_eprintf("redefinition of %s", key);
return (1);
}
map->img_path[token - 1] = ft_strdup(value);
if (map->img_path[token - 1] == NULL)
return (1);
return (0);
}
static unsigned int get_rgb(unsigned char red, unsigned char green,
unsigned char blue)
{
return (red << 24 | green << 16 | blue << 8 | 255);
}
static long long get_color(const char **tab)
{
long long rgb[3];
size_t i;
i = 0;
while (i < 3)
{
if (ft_atoul_check(tab[i]) == 0)
{
ft_eprintf("map: %s color invalid format", tab[i]);
return (-1);
}
rgb[i] = ft_atoul(tab[i]);
if (rgb[i] > 255)
{
ft_eprintf("map: %s is a to high to be a color", tab[i]);
return (-1);
}
i++;
}
return (get_rgb((unsigned char) rgb[0], (unsigned char) rgb[1], (unsigned char) rgb[2]));
}
static int set_color(long long *color, const char *key, char *value)
{
char **tab;
if (*color != -1)
{
ft_eprintf("map: %s redefinition", key);
return (1);
}
tab = ft_split(value, ',');
if (tab == NULL)
return (1);
if (ft_tablen((const void **) tab) != 3)
{
ft_freer_tab_ultimate(1, tab);
ft_eprintf("map: invalid format %s %s", key, value);
return (1);
}
*color = get_color((const char **) tab);
ft_freer_tab_ultimate(1, tab);
return (*color == -1);
}
int check_header_is_complete(t_map *map)
{
if (map->img_path[0] == NULL)
ft_eprintf("map: incomplete: Nord texture is missing");
else if (map->img_path[0] == NULL)
ft_eprintf("map: incomplete: WEST texture is missing");
else if (map->img_path[0] == NULL)
ft_eprintf("map: incomplete: SOUTH texture is missing");
else if (map->img_path[0] == NULL)
ft_eprintf("map: incomplete: EAST texture is missing");
else if (map->color_bot == -1)
ft_eprintf("map: incomplete: floor color is missing");
else if (map->color_top == -1)
ft_eprintf("map: incomplete: roof color is missing");
else
return (0);
return (1);
}
int header_is_valid(char ***header, t_map *map)
{
size_t i;
int token;
if (header == NULL)
return (1);
map->color_bot = -1;
map->color_top = -1;
i = 0;
while (i < ft_tablen((const void **) header))
{
token = get_token(header[i][0]);
if (token > 0 && token < 5)
{
if (set_texture(map, token, header[i][0], header[i][1]))
return (1);
}
else if (token == 5)
{
if (set_color(&map->color_bot,
header[i][0], header[i][1]))
return (1);
}
else if (token == 6)
{
if (set_color(&map->color_top,
header[i][0], header[i][1]))
return (1);
}
i++;
}
return (check_header_is_complete(map));
}