Continued parser

This commit is contained in:
Theo Champion 2025-06-06 18:38:21 +02:00
parent 5bc41d1a1f
commit 1d16d66d89
5 changed files with 78 additions and 18 deletions

View file

@ -1,5 +1,7 @@
CC=cc CC=cc
CFLAGS=-Wall -Wextra -Werror -g -fsanitize=address -fno-omit-frame-pointer -I mlx SANITIZERS=
#-fsanitize-address -fno-omit-frame-pointer
CFLAGS=-Wall -Wextra -Werror -g $(SANITIZERS) -I mlx
SOURCEFILES=src/main.c \ SOURCEFILES=src/main.c \
src/map/map_checker.c src/map/map_checker.c
OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES)) OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES))

20
includes/cub3d_consts.h Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cub3d_consts.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/06 17:42:23 by tchampio #+# #+# */
/* Updated: 2025/06/06 17:43:19 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CUB3D_CONSTS_H
# define CUB3D_CONSTS_H
# define HEIGHT 800
# define WIDTH 800
# define RESSOURCE_DIR "ressources"
#endif

View file

@ -0,0 +1,24 @@
NO ./path_to_the_north_texture
SO ./path_to_the_south_texture
111111111111111111111111
1000000000110000000000001
1011000001110000000000001
1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 101111010001
11111111 1111111 111111111111
F 220,100,0
C 225,30,0
WE ./path_to_the_west_texture
EA ./path_to_the_east_texture

View file

@ -6,11 +6,12 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/06 13:16:11 by tchampio #+# #+# */ /* Created: 2025/05/06 13:16:11 by tchampio #+# #+# */
/* Updated: 2025/06/06 13:12:28 by tchampio ### ########.fr */ /* Updated: 2025/06/06 17:43:26 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../includes/libft.h" #include "../includes/libft.h"
#include "../includes/cub3d_consts.h"
#include "../mlx/mlx.h" #include "../mlx/mlx.h"
#include "../includes/maputils.h" #include "../includes/maputils.h"
#include <stdbool.h> #include <stdbool.h>
@ -61,7 +62,7 @@ int main(int argc, char **argv)
t_mapdata map; t_mapdata map;
if (argc < 2) if (argc < 2)
return (ft_printf("Error: Missing cub3d filename\n"), 1); return (ft_printf("Error: Missing cub3d file\n"), 1);
if (!check_cubfile(argv[1], &map)) if (!check_cubfile(argv[1], &map))
return (ft_printf("Error: Wrong map file. Reason: %s\n", map.error), 1); return (ft_printf("Error: Wrong map file. Reason: %s\n", map.error), 1);
mlx = mlx_init(); mlx = mlx_init();

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
/* Updated: 2025/06/06 17:55:56 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/maputils.h" #include "../../includes/maputils.h"
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@ -6,33 +18,32 @@
void print_mapdata(const t_mapdata *data) void print_mapdata(const t_mapdata *data)
{ {
ft_printf(BOLD CYAN "=== Map Data ===\n" RESET); ft_printf(BOLD CYAN "=== Map Data ===\n" RESET);
ft_printf(BOLD "Filename: " RESET "%s\n", data->filename ? data->filename : "(null)"); ft_printf(BOLD "Filename: " RESET "%s\n", data->filename);
ft_printf(BOLD "NO Texture: " RESET "%s\n", data->NO_texture ? data->NO_texture : "(null)"); ft_printf(BOLD "NO Texture: " RESET "%s\n", data->NO_texture);
ft_printf(BOLD "SO Texture: " RESET "%s\n", data->SO_texture ? data->SO_texture : "(null)"); ft_printf(BOLD "SO Texture: " RESET "%s\n", data->SO_texture);
ft_printf(BOLD "WE Texture: " RESET "%s\n", data->WE_texture ? data->WE_texture : "(null)"); ft_printf(BOLD "WE Texture: " RESET "%s\n", data->WE_texture);
ft_printf(BOLD "EA Texture: " RESET "%s\n", data->EA_texture ? data->EA_texture : "(null)"); ft_printf(BOLD "EA Texture: " RESET "%s\n", data->EA_texture);
ft_printf(BOLD "Validity: " RESET); ft_printf(BOLD "Validity: " RESET);
if (data->isvalid) if (data->isvalid)
ft_printf(GREEN "VALID\n" RESET); ft_printf(GREEN "VALID\n" RESET);
else else
ft_printf(RED "INVALID\n" RESET); ft_printf(RED "INVALID\n" RESET);
if (!data->isvalid && data->error[0] != '\0') if (!data->isvalid && data->error[0] != '\0')
{ {
ft_printf(BOLD RED "Error: " RESET "%s\n", data->error); ft_printf(BOLD RED "Error: " RESET "%s\n", data->error);
} }
ft_printf(CYAN "=================\n" RESET); ft_printf(CYAN "=================\n" RESET);
} }
bool set_textures(char *line, t_mapdata *map) bool set_textures(char *line, t_mapdata *map)
{ {
char **tab; char **tab;
int i; int i;
tab = ft_split(line, ' '); tab = ft_split(line, ' ');
i = 0; i = 0;
if (tab[0][0] == '1')
return (false);
if (tab[0] && tab[1]) if (tab[0] && tab[1])
{ {
if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3)) if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3))
@ -57,8 +68,8 @@ bool add_textures(int fd, t_mapdata *map)
line = get_next_line(fd); line = get_next_line(fd);
while (line) while (line)
{ {
//ft_printf("%s", line); if (!set_textures(line, map))
set_textures(line, map); return (free(line), false);
free(line); free(line);
line = get_next_line(fd); line = get_next_line(fd);
} }
@ -68,9 +79,9 @@ bool add_textures(int fd, t_mapdata *map)
bool check_filename(char *file) bool check_filename(char *file)
{ {
int filename_size; int filename_size;
int i; int i;
int j; int j;
char end[5]; char end[5];
filename_size = ft_strlen(file); filename_size = ft_strlen(file);
@ -98,7 +109,9 @@ bool check_cubfile(char *file, t_mapdata *map)
fd = open(file, O_RDONLY); fd = open(file, O_RDONLY);
if (fd < 0) if (fd < 0)
return (ft_strlcpy(map->error, "Can't open file", 16), false); return (ft_strlcpy(map->error, "Can't open file", 16), false);
add_textures(fd, map); if (!add_textures(fd, map))
return (close(fd), ft_strlcpy(map->error,
"Map started before all the textures", 37), false);
close(fd); close(fd);
return (true); return (true);
} }