From 8211ff19ea40d8f495c912cf9c89db7d7861e992 Mon Sep 17 00:00:00 2001 From: "B.Goulard" <84p71573@gmail.com> Date: Wed, 27 Nov 2024 12:01:31 +0100 Subject: [PATCH] v1 of parsing finished --- includes/cub3d.h | 4 +- includes/cub3d_options.h | 3 +- includes/cub3d_struct.h | 20 +++++++-- includes/message_error.h | 4 +- parsing/map.c | 90 +++++++++++++++++++++++++++++++--------- sources/error.c | 14 +++++-- sources/main.c | 10 ++--- sources/options.c | 3 +- sources/options_impl.c | 10 ++++- 9 files changed, 121 insertions(+), 37 deletions(-) diff --git a/includes/cub3d.h b/includes/cub3d.h index fb034ff..a9e446d 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:26 by rparodi #+# #+# */ -/* Updated: 2024/11/13 06:56:05 by bgoulard ### ########.fr */ +/* Updated: 2024/11/27 11:56:07 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ void cleanup_info(t_info *info); int c3_options(t_info *info, int argc, char *argv[]); void c3_perror(t_info *info); -void print_error(const char *msg); +void print_error(const char *msg, int errno_state); void parse_map(t_info *info); void parse_args(char *arg, t_info *inf); diff --git a/includes/cub3d_options.h b/includes/cub3d_options.h index 1d582e9..3f38d9e 100644 --- a/includes/cub3d_options.h +++ b/includes/cub3d_options.h @@ -6,13 +6,14 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:15:15 by bgoulard #+# #+# */ -/* Updated: 2024/11/11 21:34:02 by rparodi ### ########.fr */ +/* Updated: 2024/11/27 11:35:39 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CUB3D_OPTIONS_H # define CUB3D_OPTIONS_H +void c3_set_parse_excl(void *usr_control_struct); void c3_set_file(void *usr_control_struct, const char *arg); void c3_set_debug(void *usr_control_struct); void c3_set_save(void *usr_control_struct); diff --git a/includes/cub3d_struct.h b/includes/cub3d_struct.h index 969abf8..508fedb 100644 --- a/includes/cub3d_struct.h +++ b/includes/cub3d_struct.h @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/08 23:55:29 by bgoulard #+# #+# */ -/* Updated: 2024/11/18 17:07:15 by bgoulard ### ########.fr */ +/* Updated: 2024/11/27 11:50:14 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,9 +62,19 @@ typedef struct s_dpoint typedef enum e_tile { - EMPTY, - WALL -} t_tile; + EMPTY = 0, + WALL = 1, +} t_tile_type; + +typedef union u_tile +{ + int raw_tile; + struct { + unsigned int tile_visited: 1; // parsing + unsigned int other: 27; // disponible + unsigned int tile_type: 4; // 16 tile types possible + }; +} t_tile; typedef struct s_map { @@ -94,6 +104,7 @@ typedef struct s_cli char *file; bool save; bool help; + bool no_graphics; } t_cli; // -- error utils @@ -120,6 +131,7 @@ typedef enum e_error typedef struct s_info { t_error last_error; + int errno_state; t_xvar *mlx_ptr; t_win_list *win_ptr; diff --git a/includes/message_error.h b/includes/message_error.h index bd41e2e..1b70796 100644 --- a/includes/message_error.h +++ b/includes/message_error.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:43:20 by rparodi #+# #+# */ -/* Updated: 2024/11/09 00:42:02 by bgoulard ### ########.fr */ +/* Updated: 2024/11/27 11:57:28 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,5 +19,7 @@ # define RED "\x1b[31m" # define BOLD_RED "\033[1;31m" +# define YELLOW "\x1b[33m" +# define BOLD_YELLOW "\033[1;33m" # define RESET "\x1b[K\x1b[0m" #endif diff --git a/parsing/map.c b/parsing/map.c index 04d829f..80f5702 100644 --- a/parsing/map.c +++ b/parsing/map.c @@ -6,21 +6,25 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */ -/* Updated: 2024/11/18 17:21:09 by bgoulard ### ########.fr */ +/* Updated: 2024/11/27 11:53:06 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ +// proj #include "cub3d.h" #include "cub3d_struct.h" +// mlx #include "mlx_functions.h" - -#include "ft_string.h" -#include "ft_optional.h" -#include "ft_optional_types.h" -#include "ft_vector.h" +// libft - types #include "ft_vector_types.h" +#include "ft_optional_types.h" +// libft #include "ft_math.h" +#include "ft_string.h" +#include "ft_vector.h" +#include "ft_optional.h" +// sys std #include #include #include @@ -34,10 +38,12 @@ void *load_file(void *data) info = (t_info *)data; file = ft_fd_to_buff(info->map.fd); if (file == NULL) - return (info->last_error = ERROR_READ_FILE, NULL); + return (info->errno_state = errno, + info->last_error = ERROR_READ_FILE, NULL); info->map.fraw = ft_split(file, '\n'); if (info->map.fraw == NULL) - return (info->last_error = ERROR_MALLOC, NULL); + return (info->errno_state = errno, +info->last_error = ERROR_MALLOC, NULL); ft_free((void **)&file); info->last_error = NO_ERROR; return (info); @@ -69,13 +75,15 @@ bool load_texture(t_info *info, const char *str, const char **id_str) { texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " "); if (texture.path == NULL) - return (info->last_error = ERROR_MALLOC, false); + return (info->errno_state = errno, +info->last_error = ERROR_MALLOC, false); if (ft_strend_with(texture.path, ".xpm") == false) return (info->last_error = ERROR_TEXTURE_FORMAT, false); texture.img = mlx_xpm_file_to_image(info->mlx_ptr, texture.path, \ &texture.width, &texture.height); if (texture.img == NULL) - return (info->last_error = ERROR_MLX, false); + return (info->errno_state = errno, +info->last_error = ERROR_MLX, false); return (info->map.texture[i] = texture, true); } i++; @@ -185,13 +193,13 @@ void str_to_tile(const char *str, t_tile *tile, size_t size) while (str[i]) { if (str[i] == '1' || i[str] == ' ') - tile[i] = WALL; + tile[i].raw_tile = WALL; else - tile[i] = EMPTY; + tile[i].raw_tile = EMPTY; i++; } while (i < size) - tile[i++] = WALL; + tile[i++].raw_tile = WALL; } t_vector *load_vector(t_map *map) @@ -220,6 +228,7 @@ void *load_tiles(void *data) t_info *info; t_vector *str_map; size_t i; + t_dpoint pos; info = (t_info *)data; str_map = load_vector(&info->map); @@ -231,25 +240,68 @@ void *load_tiles(void *data) return (ft_vec_destroy(&str_map), \ info->last_error = ERROR_MALLOC, NULL); i = 0; + ft_bzero(&pos, sizeof(t_dpoint)); while (ft_vec_at(str_map, i)) { str_to_tile(ft_vec_at(str_map, i), info->map.map + \ (i * info->map.size.x), info->map.size.x); + if (ft_strchrs(ft_vec_at(str_map, i), "SNWE")) + { + if (pos.x != 0 || pos.y != 0 || i == 0) + return (ft_vec_destroy(&str_map), \ + info->last_error = ERROR_PARSE, NULL); + pos.x = i + .5; + pos.y = ft_strchrs(ft_vec_at(str_map, i), "SNWE") - ft_vec_at(str_map, i) + .5; + info->player.pos = pos; + } i++; } return (ft_vec_destroy(&str_map), info); } +t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos) +{ + return (map + (pos.y * dimensions.x + pos.x)); +} + +bool flood_fill(t_tile *tiles, t_ipoint pos, t_ipoint maxs) +{ + t_tile *current; + size_t i; + const t_ipoint to_check[] = { + (t_ipoint){pos.x + 1, pos.y}, + (t_ipoint){pos.x - 1, pos.y}, + (t_ipoint){pos.x, pos.y + 1}, + (t_ipoint){pos.x, pos.y - 1}, + (t_ipoint){pos.x + 1, pos.y + 1}, + (t_ipoint){pos.x - 1, pos.y + 1}, + (t_ipoint){pos.x + 1, pos.y - 1}, + (t_ipoint){pos.x - 1, pos.y - 1}, + }; + + if (pos.x < 0 || pos.y < 0 || pos.x >= maxs.x || pos.y >= maxs.y) + return (false); + current = c3_get_cell(tiles, maxs, pos); + if (current->tile_visited == true || current->tile_type == WALL) + return (true); + current->tile_visited = true; + i = 0; + while (i != (sizeof(to_check) / sizeof(to_check[0]))) + if (flood_fill(tiles, to_check[i++], maxs) == false) + return (false); + return (true); +} + void *traverse_map(void *data) { - t_info *info; + t_info *info; + t_ipoint pos_start; info = (t_info *)data; - /// TODO: - /// modify tiles to add bit feild to know if visited instead of alloc bool - /// - info->last_error = ERROR_IMPLEM; - return (NULL); + pos_start = (t_ipoint){.x = info->player.pos.x, .y = info->player.pos.y}; + if (flood_fill(info->map.map, pos_start, info->map.size) == false) + return (info->last_error = ERROR_PARSE, NULL); + return (info); } void parse_map(t_info *info) diff --git a/sources/error.c b/sources/error.c index 0532121..549f220 100644 --- a/sources/error.c +++ b/sources/error.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 11:09:00 by rparodi #+# #+# */ -/* Updated: 2024/11/18 11:31:05 by bgoulard ### ########.fr */ +/* Updated: 2024/11/27 12:00:34 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,10 +37,10 @@ void c3_perror(t_info *info) { if (info->last_error == NO_ERROR) return ; - print_error(g_error_message[info->last_error]); + print_error(g_error_message[info->last_error], info->errno_state); } -void print_error(const char *msg) +void print_error(const char *msg, int state) { ft_putstr_fd(BOLD_RED, STDERR_FILENO); ft_putstr_fd("Error:\n", STDERR_FILENO); @@ -48,5 +48,13 @@ void print_error(const char *msg) ft_putstr_fd(RED, STDERR_FILENO); ft_putstr_fd(msg, STDERR_FILENO); ft_putstr_fd(RESET, STDERR_FILENO); + if (state != 0) + { + ft_putstr_fd(YELLOW, STDERR_FILENO); + ft_putstr_fd(" due to:\n", STDERR_FILENO); + ft_putstr_fd(BOLD_YELLOW, STDERR_FILENO); + ft_putstr_fd(ft_strerror(state), STDERR_FILENO); + ft_putstr_fd(RESET, STDERR_FILENO); + } ft_putstr_fd(".\n", STDERR_FILENO); } diff --git a/sources/main.c b/sources/main.c index 6d3ebe7..34a2ba5 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */ -/* Updated: 2024/11/18 14:17:27 by bgoulard ### ########.fr */ +/* Updated: 2024/11/27 11:51:42 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,7 +57,8 @@ void check_err(t_info *info) return (info->last_error = ERROR_EXTENSION_FILE, (void)0); info->map.fd = open(info->cli_ctx.file, O_RDONLY); if (info->map.fd == -1) - return (info->last_error = ERROR_OPEN_FILE, (void)0); + return (info->errno_state = errno, + info->last_error = ERROR_OPEN_FILE, (void)0); } void run_cub3d(t_info *info) @@ -69,15 +70,14 @@ void run_cub3d(t_info *info) dump_info(info); if (info->last_error != NO_ERROR) return ; + if (info->cli_ctx.no_graphics) + printf("no graphics mode\n"); // todo: here // - validity check printf("launching mlx\n"); mlx_loop(info->mlx_ptr); // - game loop : already loops over the mlx_ptr // -> get events if key pressed move player + run math to re-draw screen -// - mlx cleanup : already called in parent function deprecated to call here -// -> previous 'segfault' were due to someone calling cleaup instead of -// mlx_loop_end } /// @brief main function of the cub3d executable diff --git a/sources/options.c b/sources/options.c index 68caa7d..d3690c9 100644 --- a/sources/options.c +++ b/sources/options.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:14:09 by bgoulard #+# #+# */ -/* Updated: 2024/11/15 09:03:03 by bgoulard ### ########.fr */ +/* Updated: 2024/11/27 11:35:08 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ int c3_options(t_info *info, int argc, char *argv[]) {"debug", 'd', c3_set_debug, 0}, {"save", 's', c3_set_save, 0}, {"help", 'h', c3_print_help, 0}, + {"parse-only", 'p', c3_set_parse_excl, 0}, // add more options here put the implementation in options_impl.c // if you want custom option see ft_args_types.h for more info // code example in it. diff --git a/sources/options_impl.c b/sources/options_impl.c index 3714df1..174d292 100644 --- a/sources/options_impl.c +++ b/sources/options_impl.c @@ -6,13 +6,21 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:12:16 by bgoulard #+# #+# */ -/* Updated: 2024/11/15 09:08:34 by bgoulard ### ########.fr */ +/* Updated: 2024/11/27 11:35:14 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d_struct.h" #include "ft_string.h" +void c3_set_parse_excl(void *usr_control_struct) +{ + t_cli *cli_ctx; + + cli_ctx = (t_cli *)usr_control_struct; + cli_ctx->no_graphics = true; +} + void c3_set_file(void *usr_control_struct, const char *arg) { t_cli *cli_ctx;