diff --git a/includes/maputils.h b/includes/maputils.h index 81d7ece..9d9a4ff 100644 --- a/includes/maputils.h +++ b/includes/maputils.h @@ -20,6 +20,10 @@ typedef struct s_mapdata char *SO_texture; char *WE_texture; char *EA_texture; + int f_color; + int c_color; + char **map; + char **mapflood; bool isvalid; char error[1024]; } t_mapdata; diff --git a/ressources/wrongmap_invalid_colors.cub b/ressources/wrongmap_invalid_colors.cub new file mode 100644 index 0000000..6cf7a29 --- /dev/null +++ b/ressources/wrongmap_invalid_colors.cub @@ -0,0 +1,22 @@ +NO ./path_to_the_north_texture +SO ./path_to_the_south_texture +WE ./path_to_the_west_texture +EA ./path_to_the_east_texture + +F 256,100,0 +C 225,30,0 + +1111111111111111111111111 +1000000000110000000000001 +1011000001110000000000001 +1001000000000000000000001 +111111111011000001110000000000001 +100000000011000001110111111111111 +11110111111111011100000010001 +11110111111111011101010010001 +11000000110101011100000010001 +10000000000000001100000010001 +10000000000000001101010010001 +11000001110101011111011110N0111 +11110111 1110101 101111010001 +11111111 1111111 111111111111 diff --git a/src/main.c b/src/main.c index 78a0122..b4a4e53 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: tchampio SO_texture); ft_printf(BOLD "WE Texture: " RESET "%s\n", data->WE_texture); ft_printf(BOLD "EA Texture: " RESET "%s\n", data->EA_texture); + ft_printf(BOLD "F color: " RESET "%x\n", data->f_color); + ft_printf(BOLD "C color: " RESET "%x\n", data->c_color); ft_printf(BOLD "Validity: " RESET); if (data->isvalid) ft_printf(GREEN "VALID\n" RESET); @@ -35,6 +37,33 @@ void print_mapdata(const t_mapdata *data) ft_printf(CYAN "=================\n" RESET); } +unsigned long set_color(const char *s, t_mapdata *map) +{ + int i; + unsigned long finalcolor; + char **tab; + int rgb[100]; + + i = 0; + tab = ft_split(s, ','); + while (tab[i]) + { + if (i > 2) + ft_strlcpy(map->error, "too many colors", 16); + rgb[i] = ft_atoi(tab[i]); + free(tab[i]); + i++; + } + free(tab); + if (rgb[0] < 0 || rgb[0] > 255 + || rgb[1] < 0 || rgb[1] > 255 + || rgb[2] < 0 || rgb[2] > 255) + return (ft_strlcpy(map->error, "Invalid color", 14), 0); + finalcolor = ((rgb[0] & 0xff) << 16) + + ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff); + return (finalcolor); +} + bool set_textures(char *line, t_mapdata *map) { char **tab; @@ -54,21 +83,24 @@ bool set_textures(char *line, t_mapdata *map) map->WE_texture = ft_strdup(tab[1]); if (!ft_strncmp(tab[0], "EA", 3) || !ft_strncmp(tab[0], "ea", 3)) map->EA_texture = ft_strdup(tab[1]); + if (!ft_strncmp(tab[0], "F", 2) || !ft_strncmp(tab[0], "f", 2)) + map->f_color = set_color(tab[1], map); + if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2)) + map->c_color = set_color(tab[1], map); } while (tab[i]) free(tab[i++]); - free(tab); - return (true); + return (free(tab), true); } bool add_textures(int fd, t_mapdata *map) { char *line; - int set_lines; + int set_lines; line = get_next_line(fd); set_lines = 0; - while (line && set_lines < 4) + while (line && set_lines < 6) { if (!set_textures(line, map)) return (free(line), false); @@ -77,6 +109,7 @@ bool add_textures(int fd, t_mapdata *map) free(line); line = get_next_line(fd); } + free(line); print_mapdata(map); return (true); } @@ -104,6 +137,27 @@ bool check_filename(char *file) return (true); } +void populate_maps(t_mapdata *map, int fd) +{ + char *line; + + line = get_next_line(fd); + while (line) + { + free(line); + line = get_next_line(fd); + } + free(line); +} + +bool check_walls(int fd, t_mapdata *map) +{ + bool last_first; + + populate_maps(map, fd); + return (true); +} + bool check_cubfile(char *file, t_mapdata *map) { int fd; @@ -116,6 +170,11 @@ bool check_cubfile(char *file, t_mapdata *map) if (!add_textures(fd, map)) return (close(fd), ft_strlcpy(map->error, "Map started before all the textures", 37), false); + if (map->error[0]) + return (close(fd), false); + if (!check_walls(fd, map)) + return (close(fd), ft_strlcpy(map->error, + "Map is malformed (invalid chars or missing walls)", 51), false); close(fd); return (true); }