mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 09:58:09 +01:00
Added map copy
This commit is contained in:
parent
0627a90bc4
commit
8bce796bcf
4 changed files with 29 additions and 43 deletions
|
|
@ -25,6 +25,7 @@ typedef struct s_mapdata
|
|||
int mapheight;
|
||||
char **map;
|
||||
char **mapflood;
|
||||
int skipped_lines;
|
||||
bool isvalid;
|
||||
char error[1024];
|
||||
} t_mapdata;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
if (!tab)
|
||||
return ;
|
||||
i = 0;
|
||||
while (tab[i])
|
||||
{
|
||||
|
|
@ -150,59 +152,45 @@ void free_tab(char **tab)
|
|||
free(tab);
|
||||
}
|
||||
|
||||
void get_map_height(t_mapdata *map)
|
||||
void add_map_line(const char *line, t_mapdata *map)
|
||||
{
|
||||
int fd;
|
||||
char **splitted;
|
||||
char *line;
|
||||
int i;
|
||||
int count;
|
||||
static int length = 0;
|
||||
char **newmap;
|
||||
char **newmapflood;
|
||||
int i;
|
||||
|
||||
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')
|
||||
newmap = malloc(sizeof(char *) * (length + 1));
|
||||
if (!newmap)
|
||||
return ;
|
||||
newmapflood = malloc(sizeof(char *) * (length + 1));
|
||||
if (!newmapflood)
|
||||
return ;
|
||||
while (map->map && map->map[i] && i < length)
|
||||
{
|
||||
free_tab(splitted);
|
||||
free(line);
|
||||
line = get_next_line(fd);
|
||||
newmap[i] = ft_strdup(map->map[i]);
|
||||
newmapflood[i] = ft_strdup(map->mapflood[i]);
|
||||
i++;
|
||||
splitted = ft_split(line, ' ');
|
||||
}
|
||||
free_tab(splitted);
|
||||
while (line)
|
||||
{
|
||||
free(line);
|
||||
count++;
|
||||
line = get_next_line(fd);
|
||||
}
|
||||
map->mapheight = count + 1;
|
||||
close(fd);
|
||||
newmap[i] = ft_strdup(line);
|
||||
newmapflood[i] = ft_strdup(line);
|
||||
length++;
|
||||
free_tab(map->map);
|
||||
free_tab(map->mapflood);
|
||||
map->map = newmap;
|
||||
map->mapflood = newmapflood;
|
||||
}
|
||||
|
||||
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++;
|
||||
ft_printf("%s", line);
|
||||
if (line[0] != '\n')
|
||||
add_map_line(line, map);
|
||||
free(line);
|
||||
line = get_next_line(fd);
|
||||
}
|
||||
|
|
@ -223,7 +211,7 @@ void print_map(char **map)
|
|||
i = 0;
|
||||
while (map && map[i])
|
||||
{
|
||||
ft_printf("%s", map[i]);
|
||||
ft_printf("line2: %s", map[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
|
@ -242,12 +230,9 @@ bool check_cubfile(char *file, t_mapdata *map)
|
|||
"Map started before all the textures", 37), false);
|
||||
if (map->error[0])
|
||||
return (close(fd), 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);
|
||||
}
|
||||
|
|
|
|||
BIN
vgcore.11143
BIN
vgcore.11143
Binary file not shown.
BIN
vgcore.83143
BIN
vgcore.83143
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue