diff --git a/includes/maputils.h b/includes/maputils.h index 9d9a4ff..b462ef2 100644 --- a/includes/maputils.h +++ b/includes/maputils.h @@ -22,6 +22,7 @@ typedef struct s_mapdata char *EA_texture; int f_color; int c_color; + int mapheight; char **map; char **mapflood; bool isvalid; diff --git a/ressources/testmap.cub b/ressources/testmap.cub index f83f2a9..4660b9f 100644 --- a/ressources/testmap.cub +++ b/ressources/testmap.cub @@ -6,12 +6,12 @@ EA ./path_to_the_east_texture F 220,100,0 C 225,30,0 -1111111111111111111111111 -1000000000110000000000001 -1011000001110000000000001 -1001000000000000000000001 + 1111111111111111111111111 + 1000000000110000000000001 + 1011000001110000000000001 + 1001000000000000000000001 111111111011000001110000000000001 -100000000011000001110111111111111 +10000000001100000111011111D111111 11110111111111011100000010001 11110111111111011101010010001 11000000110101011100000010001 diff --git a/src/map/map_checker.c b/src/map/map_checker.c index c77b51b..cf74bfb 100644 --- a/src/map/map_checker.c +++ b/src/map/map_checker.c @@ -6,12 +6,13 @@ /* By: tchampio +#include #include #include "../../includes/libft.h" @@ -110,11 +111,10 @@ bool add_textures(int fd, t_mapdata *map) line = get_next_line(fd); } free(line); - print_mapdata(map); return (true); } -bool check_filename(char *file) +bool check_filename(t_mapdata *map, char *file) { int filename_size; int i; @@ -133,17 +133,76 @@ bool check_filename(char *file) } if (ft_strncmp(end, ".cub", 5)) return (false); - else - return (true); + map->filename = ft_strdup(file); + return (true); +} + +void free_tab(char **tab) +{ + int i; + + i = 0; + while (tab[i]) + { + free(tab[i]); + i++; + } + free(tab); +} + +void get_map_height(t_mapdata *map) +{ + int fd; + char **splitted; + char *line; + int i; + int count; + + fd = open(map->filename, O_RDONLY); + line = get_next_line(fd); + splitted = ft_split(line, ' '); + i = 0; + count = 0; + while (splitted[i] && splitted[i][0] != '1') + { + free_tab(splitted); + free(line); + line = get_next_line(fd); + i++; + splitted = ft_split(line, ' '); + } + free_tab(splitted); + while (line) + { + free(line); + count++; + line = get_next_line(fd); + } + map->mapheight = count + 1; + close(fd); } void populate_maps(t_mapdata *map, int fd) { char *line; + int i; + get_map_height(map); + ft_printf("%d\n", map->mapheight); + map->map = malloc(sizeof(char *) * map->mapheight); + if (!map->map) + return ; + map->mapflood = malloc(sizeof(char *) * map->mapheight); + if (!map->mapflood) + return ; line = get_next_line(fd); + ft_printf("%s", line); + i = 0; while (line) { + map->map[i] = ft_strdup(line); + map->mapflood[i] = ft_strdup(line); + i++; free(line); line = get_next_line(fd); } @@ -152,17 +211,28 @@ void populate_maps(t_mapdata *map, int fd) bool check_walls(int fd, t_mapdata *map) { - bool last_first; - - populate_maps(map, fd); + (void)fd; + ft_printf("%d\n", map->mapheight); return (true); } +void print_map(char **map) +{ + int i; + + i = 0; + while (map && map[i]) + { + ft_printf("%s", map[i]); + i++; + } +} + bool check_cubfile(char *file, t_mapdata *map) { int fd; - if (!check_filename(file)) + if (!check_filename(map, file)) return (ft_strlcpy(map->error, "File is not a .cub file", 25), false); fd = open(file, O_RDONLY); if (fd < 0) @@ -172,9 +242,12 @@ bool check_cubfile(char *file, t_mapdata *map) "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); + fd = open(file, O_RDONLY); + populate_maps(map, fd); + //if (!check_walls(fd, map)) + // return (close(fd), ft_strlcpy(map->error, + // "Map is malformed (invalid chars or missing walls)", 51), false); + print_map(map->map); return (true); } diff --git a/vgcore.11143 b/vgcore.11143 new file mode 100644 index 0000000..e8fe37c Binary files /dev/null and b/vgcore.11143 differ diff --git a/vgcore.83143 b/vgcore.83143 new file mode 100644 index 0000000..466ce01 Binary files /dev/null and b/vgcore.83143 differ