From 3e28da1fcf8378ee62c2f22d36d7436fc21a3a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 21 Jul 2025 15:22:41 +0200 Subject: [PATCH] refactor: put all populate_map related code in that file --- src/map/checkers.h | 3 +-- src/map/map_checker.c | 4 ++-- src/map/populate_map.c | 43 ++++++++++++++++++++++++++++++++++++++++-- src/map/populate_map.h | 20 ++++++++++++++++++++ src/map/setters.c | 41 +--------------------------------------- src/map/setters.h | 3 +-- 6 files changed, 66 insertions(+), 48 deletions(-) create mode 100644 src/map/populate_map.h diff --git a/src/map/checkers.h b/src/map/checkers.h index 3db8c10..b2bd998 100644 --- a/src/map/checkers.h +++ b/src/map/checkers.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:39:41 by kcolin #+# #+# */ -/* Updated: 2025/07/17 15:55:09 by kcolin ### ########.fr */ +/* Updated: 2025/07/21 15:24:14 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ # include "mapdata.h" bool check_filename(t_mapdata *map, char *file); -void populate_maps(t_mapdata *map, int fd); bool check_walls(t_mapdata *map); bool check_bare_minimum(t_mapdata *map); diff --git a/src/map/map_checker.c b/src/map/map_checker.c index f5b5e53..2963bc6 100644 --- a/src/map/map_checker.c +++ b/src/map/map_checker.c @@ -6,16 +6,16 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */ -/* Updated: 2025/07/17 15:53:51 by kcolin ### ########.fr */ +/* Updated: 2025/07/21 15:25:16 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../libft/includes/libft.h" #include "../utils/colors.h" +#include "populate_map.h" #include "checkers.h" #include "setters.h" #include -#include #include void print_mapdata(const t_mapdata *data) diff --git a/src/map/populate_map.c b/src/map/populate_map.c index 7cae3c2..d2f8f1c 100644 --- a/src/map/populate_map.c +++ b/src/map/populate_map.c @@ -6,12 +6,51 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:44:02 by kcolin #+# #+# */ -/* Updated: 2025/07/17 15:53:38 by kcolin ### ########.fr */ +/* Updated: 2025/07/21 15:23:47 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../libft/includes/libft.h" -#include "setters.h" +#include "../utils/frees.h" + +int copy_old_map(t_mapdata *map, char **newmap, char **newmapflood, int length) +{ + int i; + + i = 0; + if (!map->map) + return (0); + while (i < length) + { + newmap[i] = ft_strdup(map->map[i]); + newmapflood[i] = ft_strdup(map->mapflood[i]); + i++; + } + return (i); +} + +void add_map_line(const char *line, t_mapdata *map) +{ + static int length = 0; + char **newmap; + char **newmapflood; + int i; + + newmap = ft_calloc(sizeof(char *), (length + 2)); + if (!newmap) + return ; + newmapflood = ft_calloc(sizeof(char *), (length + 2)); + if (!newmapflood) + return ; + i = copy_old_map(map, newmap, newmapflood, length); + newmap[i] = ft_strdup(line); + newmapflood[i] = ft_strdup(line); + free_tab_length(map->map, length); + free_tab_length(map->mapflood, length); + map->mapheight = ++length; + map->map = newmap; + map->mapflood = newmapflood; +} void populate_maps(t_mapdata *map, int fd) { diff --git a/src/map/populate_map.h b/src/map/populate_map.h new file mode 100644 index 0000000..828090c --- /dev/null +++ b/src/map/populate_map.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* populate_map.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/21 15:24:31 by kcolin #+# #+# */ +/* Updated: 2025/07/21 15:25:02 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef POPULATE_MAP_H +# define POPULATE_MAP_H + +# include "mapdata.h" + +void populate_maps(t_mapdata *map, int fd); + +#endif // POPULATE_MAP_H diff --git a/src/map/setters.c b/src/map/setters.c index d4b77b0..d8d3eda 100644 --- a/src/map/setters.c +++ b/src/map/setters.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/06/21 19:35:43 by tchampio #+# #+# */ -/* Updated: 2025/07/21 12:48:50 by kcolin ### ########.fr */ +/* Updated: 2025/07/21 15:22:41 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -90,42 +90,3 @@ bool add_textures(int fd, t_mapdata *map) free(line); return (true); } - -int copy_old_map(t_mapdata *map, char **newmap, char **newmapflood, int length) -{ - int i; - - i = 0; - if (!map->map) - return (0); - while (i < length) - { - newmap[i] = ft_strdup(map->map[i]); - newmapflood[i] = ft_strdup(map->mapflood[i]); - i++; - } - return (i); -} - -void add_map_line(const char *line, t_mapdata *map) -{ - static int length = 0; - char **newmap; - char **newmapflood; - int i; - - newmap = ft_calloc(sizeof(char *), (length + 2)); - if (!newmap) - return ; - newmapflood = ft_calloc(sizeof(char *), (length + 2)); - if (!newmapflood) - return ; - i = copy_old_map(map, newmap, newmapflood, length); - newmap[i] = ft_strdup(line); - newmapflood[i] = ft_strdup(line); - free_tab_length(map->map, length); - free_tab_length(map->mapflood, length); - map->mapheight = ++length; - map->map = newmap; - map->mapflood = newmapflood; -} diff --git a/src/map/setters.h b/src/map/setters.h index e4d5e81..885d732 100644 --- a/src/map/setters.h +++ b/src/map/setters.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:40:32 by kcolin #+# #+# */ -/* Updated: 2025/07/17 15:54:11 by kcolin ### ########.fr */ +/* Updated: 2025/07/21 15:22:57 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,5 @@ # include "mapdata.h" bool add_textures(int fd, t_mapdata *map); -void add_map_line(const char *line, t_mapdata *map); #endif // SETTERS_H