From 3e837f1144c16be785bbc6fc62b398c8b7933636 Mon Sep 17 00:00:00 2001 From: aortigos Date: Thu, 4 Dec 2025 10:32:50 +0100 Subject: [PATCH] read map files --- includes/cub3d.h | 10 +++- src/parsing/read_map.c | 93 ++++++++++++++++++++++++++++++++++++ src/parsing/read_map_utils.c | 89 ++++++++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 src/parsing/read_map.c create mode 100644 src/parsing/read_map_utils.c diff --git a/includes/cub3d.h b/includes/cub3d.h index c906a3d..667a16f 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: aortigos map = ft_strdup(""); + while (map->line) + { + if (map->line[0] == '\n') + return (ft_putstr_fd(ERR_MAP_EMPTY, 2), freetl(map->map, map->line, + -1), NULL); + tmp = ft_strjoin(map->map, map->line); + ft_memfree(map->map); + ft_memfree(map->line); + map->map = ft_strdup(tmp); + ft_memfree(tmp); + map->line = get_next_line(map->fd); + } + return (map->map); +} + +int read_map_(t_data *map, int count) +{ + map->map = getmap(map); + if (!map->map) + return (0); + map->map2d = ft_split(map->map, '\n'); + if (!map->map2d) + return (ft_memfree(map->map), 0); + ft_memfree(map->map); + if (!check_tures_space_tab(map->ture2d, count) || !parse_rgb(map->ture2d) + || !check_dup(map) || !check_first_last_line(map->map2d) + || !surrounded_by_one(map->map2d)) + return (free2d(map->map2d), 0); + return (1); +} + +void process_map(t_data *map, int *count) +{ + char *tmp; + + while (map->line && map->line[0] != '1' && map->line[0] != 32) + { + if (check_color_textures(map->line)) + { + tmp = ft_strjoin(map->ture, map->line); + ft_memfree(map->ture); + map->ture = ft_strdup(tmp); + ft_memfree(tmp); + (*count)++; + } + ft_memfree(map->line); + map->line = get_next_line(map->fd); + } +} diff --git a/src/parsing/read_map_utils.c b/src/parsing/read_map_utils.c new file mode 100644 index 0000000..c5a9f9c --- /dev/null +++ b/src/parsing/read_map_utils.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read_map_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos ture2d[i]) + { + j = i + 1; + while (m->ture2d[j]) + { + if (!ft_strncmp(m->ture2d[i], m->ture2d[j], 2)) + return (ft_putstr_fd(ERR_MAP_DUP, 2), 0); + j++; + } + i++; + } + return (1); +} + +int check_first_last_line(char **map) +{ + if (!map[0]) + return (ft_putstr_fd(ERR_MAP_INV, 2), 0); + if (!line_around_one(map[0]) || !line_around_one(map[ft_arraylen(map) - 1])) + return (ft_putstr_fd("one Y\n", 2), 0); + return (1); +} + +int surrounded_by_one(char **map) +{ + int i; + int flag; + + flag = 0; + i = -1; + while (map[++i]) + if (!is_surrounded(map[i]) || !is_validmap(map[i], &flag) || flag > 1) + return (ft_putstr_fd("one X\n", 2), 0); + if (flag == 0) + return (ft_putstr_fd("no position map\n", 2), 0); + return (1); +} \ No newline at end of file