42_cube3D/map/parsing_header2.c

99 lines
2.8 KiB
C
Raw Permalink Normal View History

2023-05-16 13:12:54 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_header2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 17:28:58 by cchauvet #+# #+# */
2023-06-16 12:33:53 -04:00
/* Updated: 2023/06/16 16:31:06 by cchauvet ### ########.fr */
2023-05-16 13:12:54 -04:00
/* */
/* ************************************************************************** */
2023-05-03 07:09:06 -04:00
#include "./parsing_private.h"
2023-05-16 13:12:54 -04:00
int set_texture(t_map *map, int token, const char *key, const char *value)
2023-05-03 07:09:06 -04:00
{
2023-05-05 10:51:37 -04:00
if (map->img_path[token - 1] != NULL)
2023-05-03 07:09:06 -04:00
{
ft_eprintf("redefinition of %s", key);
return (1);
}
2023-05-05 10:49:04 -04:00
map->img_path[token - 1] = ft_strdup(value);
if (map->img_path[token - 1] == NULL)
return (1);
2023-05-03 07:09:06 -04:00
return (0);
}
2023-05-16 13:12:54 -04:00
static unsigned int get_rgb(unsigned char red, unsigned char green,
2023-05-05 07:54:25 -04:00
unsigned char blue)
{
return (red << 24 | green << 16 | blue << 8 | 255);
}
2023-05-16 13:12:54 -04:00
static long long get_color(const char **tab)
2023-05-05 07:54:25 -04:00
{
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++;
}
2023-05-16 13:12:54 -04:00
return (get_rgb((unsigned char) rgb[0],
(unsigned char) rgb[1], (unsigned char) rgb[2]));
2023-05-05 07:54:25 -04:00
}
2023-05-16 13:12:54 -04:00
int set_color(long long *color, const char *key, const char *value)
2023-05-03 07:09:06 -04:00
{
2023-05-05 07:54:25 -04:00
char **tab;
2023-05-03 07:09:06 -04:00
if (*color != -1)
{
2023-05-05 07:54:25 -04:00
ft_eprintf("map: %s redefinition", key);
2023-05-03 07:09:06 -04:00
return (1);
}
2023-06-16 12:33:53 -04:00
if (ft_count(value, ',') != 2)
2023-05-03 07:09:06 -04:00
{
2023-05-05 07:54:25 -04:00
ft_eprintf("map: invalid format %s %s", key, value);
2023-05-03 07:09:06 -04:00
return (1);
}
2023-06-16 12:33:53 -04:00
tab = ft_split(value, ',');
if (tab == NULL)
return (1);
2023-05-05 07:54:25 -04:00
*color = get_color((const char **) tab);
2023-05-05 10:49:04 -04:00
ft_freer_tab_ultimate(1, tab);
2023-05-05 07:54:25 -04:00
return (*color == -1);
2023-05-03 07:09:06 -04:00
}
2023-05-16 08:47:04 -04:00
int check_header_is_complete(t_map *map)
{
if (map->img_path[0] == NULL)
ft_eprintf("map: incomplete: Nord texture is missing");
2023-05-16 13:12:54 -04:00
else if (map->img_path[1] == NULL)
2023-05-16 08:47:04 -04:00
ft_eprintf("map: incomplete: WEST texture is missing");
2023-05-16 13:12:54 -04:00
else if (map->img_path[2] == NULL)
2023-05-16 08:47:04 -04:00
ft_eprintf("map: incomplete: SOUTH texture is missing");
2023-05-16 13:12:54 -04:00
else if (map->img_path[3] == NULL)
2023-05-16 08:47:04 -04:00
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);
}