mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
started to parse the 'real' map
This commit is contained in:
parent
d336d7bc15
commit
02079dfa87
4 changed files with 92 additions and 6 deletions
|
|
@ -20,6 +20,10 @@ typedef struct s_mapdata
|
|||
char *SO_texture;
|
||||
char *WE_texture;
|
||||
char *EA_texture;
|
||||
int f_color;
|
||||
int c_color;
|
||||
char **map;
|
||||
char **mapflood;
|
||||
bool isvalid;
|
||||
char error[1024];
|
||||
} t_mapdata;
|
||||
|
|
|
|||
22
ressources/wrongmap_invalid_colors.cub
Normal file
22
ressources/wrongmap_invalid_colors.cub
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
NO ./path_to_the_north_texture
|
||||
SO ./path_to_the_south_texture
|
||||
WE ./path_to_the_west_texture
|
||||
EA ./path_to_the_east_texture
|
||||
|
||||
F 256,100,0
|
||||
C 225,30,0
|
||||
|
||||
1111111111111111111111111
|
||||
1000000000110000000000001
|
||||
1011000001110000000000001
|
||||
1001000000000000000000001
|
||||
111111111011000001110000000000001
|
||||
100000000011000001110111111111111
|
||||
11110111111111011100000010001
|
||||
11110111111111011101010010001
|
||||
11000000110101011100000010001
|
||||
10000000000000001100000010001
|
||||
10000000000000001101010010001
|
||||
11000001110101011111011110N0111
|
||||
11110111 1110101 101111010001
|
||||
11111111 1111111 111111111111
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/05/06 13:16:11 by tchampio #+# #+# */
|
||||
/* Updated: 2025/06/06 17:43:26 by tchampio ### ########.fr */
|
||||
/* Updated: 2025/06/16 16:12:50 by tchampio ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -61,6 +61,7 @@ int main(int argc, char **argv)
|
|||
t_mlx_data data;
|
||||
t_mapdata map;
|
||||
|
||||
ft_bzero(&map, sizeof(map));
|
||||
if (argc < 2)
|
||||
return (ft_printf("Error: Missing cub3d file\n"), 1);
|
||||
if (!check_cubfile(argv[1], &map))
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
|
||||
/* Updated: 2025/06/08 20:28:57 by tchampio ### ########.fr */
|
||||
/* Updated: 2025/06/16 17:03:00 by tchampio ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -23,6 +23,8 @@ void print_mapdata(const t_mapdata *data)
|
|||
ft_printf(BOLD "SO Texture: " RESET "%s\n", data->SO_texture);
|
||||
ft_printf(BOLD "WE Texture: " RESET "%s\n", data->WE_texture);
|
||||
ft_printf(BOLD "EA Texture: " RESET "%s\n", data->EA_texture);
|
||||
ft_printf(BOLD "F color: " RESET "%x\n", data->f_color);
|
||||
ft_printf(BOLD "C color: " RESET "%x\n", data->c_color);
|
||||
ft_printf(BOLD "Validity: " RESET);
|
||||
if (data->isvalid)
|
||||
ft_printf(GREEN "VALID\n" RESET);
|
||||
|
|
@ -35,6 +37,33 @@ void print_mapdata(const t_mapdata *data)
|
|||
ft_printf(CYAN "=================\n" RESET);
|
||||
}
|
||||
|
||||
unsigned long set_color(const char *s, t_mapdata *map)
|
||||
{
|
||||
int i;
|
||||
unsigned long finalcolor;
|
||||
char **tab;
|
||||
int rgb[100];
|
||||
|
||||
i = 0;
|
||||
tab = ft_split(s, ',');
|
||||
while (tab[i])
|
||||
{
|
||||
if (i > 2)
|
||||
ft_strlcpy(map->error, "too many colors", 16);
|
||||
rgb[i] = ft_atoi(tab[i]);
|
||||
free(tab[i]);
|
||||
i++;
|
||||
}
|
||||
free(tab);
|
||||
if (rgb[0] < 0 || rgb[0] > 255
|
||||
|| rgb[1] < 0 || rgb[1] > 255
|
||||
|| rgb[2] < 0 || rgb[2] > 255)
|
||||
return (ft_strlcpy(map->error, "Invalid color", 14), 0);
|
||||
finalcolor = ((rgb[0] & 0xff) << 16)
|
||||
+ ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff);
|
||||
return (finalcolor);
|
||||
}
|
||||
|
||||
bool set_textures(char *line, t_mapdata *map)
|
||||
{
|
||||
char **tab;
|
||||
|
|
@ -54,21 +83,24 @@ bool set_textures(char *line, t_mapdata *map)
|
|||
map->WE_texture = ft_strdup(tab[1]);
|
||||
if (!ft_strncmp(tab[0], "EA", 3) || !ft_strncmp(tab[0], "ea", 3))
|
||||
map->EA_texture = ft_strdup(tab[1]);
|
||||
if (!ft_strncmp(tab[0], "F", 2) || !ft_strncmp(tab[0], "f", 2))
|
||||
map->f_color = set_color(tab[1], map);
|
||||
if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2))
|
||||
map->c_color = set_color(tab[1], map);
|
||||
}
|
||||
while (tab[i])
|
||||
free(tab[i++]);
|
||||
free(tab);
|
||||
return (true);
|
||||
return (free(tab), true);
|
||||
}
|
||||
|
||||
bool add_textures(int fd, t_mapdata *map)
|
||||
{
|
||||
char *line;
|
||||
int set_lines;
|
||||
int set_lines;
|
||||
|
||||
line = get_next_line(fd);
|
||||
set_lines = 0;
|
||||
while (line && set_lines < 4)
|
||||
while (line && set_lines < 6)
|
||||
{
|
||||
if (!set_textures(line, map))
|
||||
return (free(line), false);
|
||||
|
|
@ -77,6 +109,7 @@ bool add_textures(int fd, t_mapdata *map)
|
|||
free(line);
|
||||
line = get_next_line(fd);
|
||||
}
|
||||
free(line);
|
||||
print_mapdata(map);
|
||||
return (true);
|
||||
}
|
||||
|
|
@ -104,6 +137,27 @@ bool check_filename(char *file)
|
|||
return (true);
|
||||
}
|
||||
|
||||
void populate_maps(t_mapdata *map, int fd)
|
||||
{
|
||||
char *line;
|
||||
|
||||
line = get_next_line(fd);
|
||||
while (line)
|
||||
{
|
||||
free(line);
|
||||
line = get_next_line(fd);
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
|
||||
bool check_walls(int fd, t_mapdata *map)
|
||||
{
|
||||
bool last_first;
|
||||
|
||||
populate_maps(map, fd);
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool check_cubfile(char *file, t_mapdata *map)
|
||||
{
|
||||
int fd;
|
||||
|
|
@ -116,6 +170,11 @@ bool check_cubfile(char *file, t_mapdata *map)
|
|||
if (!add_textures(fd, map))
|
||||
return (close(fd), ft_strlcpy(map->error,
|
||||
"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);
|
||||
return (true);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue