Added map copy

This commit is contained in:
Theo Champion 2025-06-20 12:07:03 +02:00
parent 0627a90bc4
commit 8bce796bcf
4 changed files with 29 additions and 43 deletions

View file

@ -25,6 +25,7 @@ typedef struct s_mapdata
int mapheight; int mapheight;
char **map; char **map;
char **mapflood; char **mapflood;
int skipped_lines;
bool isvalid; bool isvalid;
char error[1024]; char error[1024];
} t_mapdata; } t_mapdata;

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */ /* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
/* Updated: 2025/06/17 16:33:43 by tchampio ### ########.fr */ /* Updated: 2025/06/19 17:48:19 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -141,6 +141,8 @@ void free_tab(char **tab)
{ {
int i; int i;
if (!tab)
return ;
i = 0; i = 0;
while (tab[i]) while (tab[i])
{ {
@ -150,59 +152,45 @@ void free_tab(char **tab)
free(tab); free(tab);
} }
void get_map_height(t_mapdata *map) void add_map_line(const char *line, t_mapdata *map)
{ {
int fd; static int length = 0;
char **splitted; char **newmap;
char *line; char **newmapflood;
int i; int i;
int count;
fd = open(map->filename, O_RDONLY);
line = get_next_line(fd);
splitted = ft_split(line, ' ');
i = 0; i = 0;
count = 0; newmap = malloc(sizeof(char *) * (length + 1));
while (splitted[i] && splitted[i][0] != '1') if (!newmap)
return ;
newmapflood = malloc(sizeof(char *) * (length + 1));
if (!newmapflood)
return ;
while (map->map && map->map[i] && i < length)
{ {
free_tab(splitted); newmap[i] = ft_strdup(map->map[i]);
free(line); newmapflood[i] = ft_strdup(map->mapflood[i]);
line = get_next_line(fd);
i++; i++;
splitted = ft_split(line, ' ');
} }
free_tab(splitted); newmap[i] = ft_strdup(line);
while (line) newmapflood[i] = ft_strdup(line);
{ length++;
free(line); free_tab(map->map);
count++; free_tab(map->mapflood);
line = get_next_line(fd); map->map = newmap;
} map->mapflood = newmapflood;
map->mapheight = count + 1;
close(fd);
} }
void populate_maps(t_mapdata *map, int fd) void populate_maps(t_mapdata *map, int fd)
{ {
char *line; 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); line = get_next_line(fd);
ft_printf("%s", line);
i = 0;
while (line) while (line)
{ {
map->map[i] = ft_strdup(line); ft_printf("%s", line);
map->mapflood[i] = ft_strdup(line); if (line[0] != '\n')
i++; add_map_line(line, map);
free(line); free(line);
line = get_next_line(fd); line = get_next_line(fd);
} }
@ -223,7 +211,7 @@ void print_map(char **map)
i = 0; i = 0;
while (map && map[i]) while (map && map[i])
{ {
ft_printf("%s", map[i]); ft_printf("line2: %s", map[i]);
i++; i++;
} }
} }
@ -242,12 +230,9 @@ bool check_cubfile(char *file, t_mapdata *map)
"Map started before all the textures", 37), false); "Map started before all the textures", 37), false);
if (map->error[0]) if (map->error[0])
return (close(fd), false); return (close(fd), false);
close(fd);
fd = open(file, O_RDONLY);
populate_maps(map, fd); populate_maps(map, fd);
//if (!check_walls(fd, map)) //if (!check_walls(fd, map))
// return (close(fd), ft_strlcpy(map->error, // return (close(fd), ft_strlcpy(map->error,
// "Map is malformed (invalid chars or missing walls)", 51), false); // "Map is malformed (invalid chars or missing walls)", 51), false);
print_map(map->map);
return (true); return (true);
} }

Binary file not shown.

Binary file not shown.