From 7321db26d90330564c3d3f807f9eb7fc75ba6d1a Mon Sep 17 00:00:00 2001 From: "B.Goulard" <84p71573@gmail.com> Date: Wed, 13 Nov 2024 07:24:32 +0100 Subject: [PATCH 1/3] feat: moved parsing to new 'bgoulard' branch, parsing on texteures added now parses textures using the ol'optional chain functions. --- includes/cub3d_parsing.h | 77 +++++++++++++++++++++++++++++++++ includes/cub3d_struct.h | 12 +++++- parsing/map.c | 93 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 174 insertions(+), 8 deletions(-) create mode 100644 includes/cub3d_parsing.h diff --git a/includes/cub3d_parsing.h b/includes/cub3d_parsing.h new file mode 100644 index 0000000..38c94e9 --- /dev/null +++ b/includes/cub3d_parsing.h @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cub3d_parsing.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/11 14:41:07 by bgoulard #+# #+# */ +/* Updated: 2024/11/11 17:43:12 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CUB3D_PARSING_H +# define CUB3D_PARSING_H + +#include +# define C3D_PRS_PLS "SO" +# define C3D_PRS_PLW "WE" +# define C3D_PRS_PLN "NO" +# define C3D_PRS_PLE "EA" + +# define C3D_PRS_WLL_TRAIL ' ' +# define C3D_PRS_WLL '1' +# define C3D_PRS_EMP '0' + +enum e_tile_m { + EMPTY, + DOOR_OPEN, + DOOR_ANIM, + DOOR_CLOSE, + WALL_TRAIL, + WALL, + PLAYER_N, + PLAYER_S, + PLAYER_E, + PLAYER_W +}; + +typedef struct s_map_truth { + enum e_tile_m tile; + char chr[2]; +} t_map_truth; + +typedef struct s_tile { + enum e_tile_m tile; + bool visited; +} t_tile; + +extern const t_map_truth g_map_table[]; +/* += { +{EMPTY, C3D_PRS_EMP}, +{WALL_TRAIL, C3D_PRS_WLL_TRAIL}, +{WALL, C3D_PRS_WLL}, +{PLAYER_N, C3D_PRS_PLN}, +{PLAYER_S, C3D_PRS_PLS}, +{PLAYER_E, C3D_PRS_PLE}, +{PLAYER_W, C3D_PRS_PLW}, +}; +*/ + +enum e_identifiers { + UNKNOWN = 0, + TEXTURE_N, + TEXTURE_S, + TEXTURE_E, + TEXTURE_W, + FLOOR, + CEILING, +}; + +typedef struct s_identifiers { + enum e_identifiers id; + char *path; +} t_identifiers; + +#endif /* CUB3D_PARSING_H */ diff --git a/includes/cub3d_struct.h b/includes/cub3d_struct.h index ad3cda3..ae4be11 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/12 11:05:49 by bgoulard ### ########.fr */ +/* Updated: 2024/11/13 07:15:14 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,14 @@ typedef struct s_color }; } t_color; +typedef struct s_texture +{ + t_img *img; + int width; + int height; + char *path; +} t_texture; + typedef struct s_point { int x; @@ -64,7 +72,7 @@ typedef struct s_map t_ipoint size; t_tile *map; char **fraw; - t_img texture[4]; + t_texture texture[4]; t_color bg_colors[2]; } t_map; diff --git a/parsing/map.c b/parsing/map.c index bea8e7b..bd752f4 100644 --- a/parsing/map.c +++ b/parsing/map.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */ -/* Updated: 2024/11/12 11:29:13 by bgoulard ### ########.fr */ +/* Updated: 2024/11/13 07:20:15 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,22 +15,100 @@ #include "ft_string.h" #include "ft_optional.h" #include "ft_optional_types.h" +#include "mlx_functions.h" #include #include // steps: -// 1. load_map into a buffer +// 1. load_file into a buffer // 2. store the map while deleting trailing newlines -void *load_map(void *data) +void *load_file(void *data) { t_info *info = (t_info *)data; char *file = NULL; file = ft_fd_to_buff(info->map.fd); + if (file == NULL) + return (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); ft_free((void **)&file); - return (info->last_error = ERROR_IMPLEM, NULL); + info->last_error = NO_ERROR; + return (info); +} + +bool is_identifier(const char *str, const char *id_str[4]) +{ + size_t i; + + i = 0; + while (i < 4) + { + if (ft_strstart_with(str, id_str[i])) + return (true); + i++; + } + return (false); +} + +bool load_texture(t_info *info, const char *str) +{ + const char *id_str[4] = {"NO", "SO", "WE", "EA"}; + size_t i; + t_texture texture; + + + i = 0; + while (i < 4) + { + if (ft_strstart_with(str, id_str[i])) + { + texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " "); + if (texture.path == NULL) + return (info->last_error = ERROR_MALLOC, 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); + info->map.texture[i] = texture; + return (true); + } + i++; + } + return (false); +} + +void *load_textures(void *data) +{ + const char *id_str[4] = {"NO", "SO", "WE", "EA"}; + t_info *info = (t_info *)data; + size_t i; + + while (info->map.fraw[i]) + { + if (is_identifier(info->map.fraw[i], id_str) && \ + (load_texture(info, info->map.fraw[i]) == false)) + return (NULL); + i++; + } + return (info); +} + +void *load_bgs(void *data) +{ + t_info *info = (t_info *)data; + + info->last_error = ERROR_IMPLEM; + return (NULL); +} + +void *load_tiles(void *data) +{ + t_info *info = (t_info *)data; + + info->last_error = ERROR_IMPLEM; + return (NULL); } void *traverse_map(void *data) @@ -45,7 +123,10 @@ void parse_map(t_info *info) { t_optional opt = {OPT_NONE, NULL}; const t_data_tr_i function_list[] = { - load_map, + load_file, + load_textures, + load_bgs, + load_tiles, traverse_map, NULL, }; From d586acba8ff4afb18a0f58e28a5404219490a114 Mon Sep 17 00:00:00 2001 From: "B.Goulard" <84p71573@gmail.com> Date: Mon, 18 Nov 2024 17:25:27 +0100 Subject: [PATCH 2/3] feat: advancement on map parse --- includes/cub3d_parsing.h | 46 ++----- includes/cub3d_struct.h | 5 +- libft/src/ft_vector/ft_vec_at.c | 4 +- mlx_layer/mlx_init.c | 11 +- parsing/map.c | 222 +++++++++++++++++++++++++------- raycast/frame_update.c | 4 +- sources/cleanups.c | 21 ++- sources/error.c | 5 +- sources/main.c | 21 ++- sources/options.c | 16 +-- sources/options_impl.c | 25 ++-- 11 files changed, 257 insertions(+), 123 deletions(-) diff --git a/includes/cub3d_parsing.h b/includes/cub3d_parsing.h index 38c94e9..9e424b4 100644 --- a/includes/cub3d_parsing.h +++ b/includes/cub3d_parsing.h @@ -6,14 +6,15 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/11 14:41:07 by bgoulard #+# #+# */ -/* Updated: 2024/11/11 17:43:12 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 11:32:00 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CUB3D_PARSING_H # define CUB3D_PARSING_H -#include +# include + # define C3D_PRS_PLS "SO" # define C3D_PRS_PLW "WE" # define C3D_PRS_PLN "NO" @@ -23,7 +24,8 @@ # define C3D_PRS_WLL '1' # define C3D_PRS_EMP '0' -enum e_tile_m { +enum e_tile_m +{ EMPTY, DOOR_OPEN, DOOR_ANIM, @@ -36,42 +38,16 @@ enum e_tile_m { PLAYER_W }; -typedef struct s_map_truth { +typedef struct s_map_truth +{ enum e_tile_m tile; char chr[2]; -} t_map_truth; +} t_map_truth; -typedef struct s_tile { +typedef struct s_tile +{ enum e_tile_m tile; bool visited; -} t_tile; - -extern const t_map_truth g_map_table[]; -/* -= { -{EMPTY, C3D_PRS_EMP}, -{WALL_TRAIL, C3D_PRS_WLL_TRAIL}, -{WALL, C3D_PRS_WLL}, -{PLAYER_N, C3D_PRS_PLN}, -{PLAYER_S, C3D_PRS_PLS}, -{PLAYER_E, C3D_PRS_PLE}, -{PLAYER_W, C3D_PRS_PLW}, -}; -*/ - -enum e_identifiers { - UNKNOWN = 0, - TEXTURE_N, - TEXTURE_S, - TEXTURE_E, - TEXTURE_W, - FLOOR, - CEILING, -}; - -typedef struct s_identifiers { - enum e_identifiers id; - char *path; -} t_identifiers; +} t_tile; #endif /* CUB3D_PARSING_H */ diff --git a/includes/cub3d_struct.h b/includes/cub3d_struct.h index ae4be11..969abf8 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/13 07:15:14 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 17:07:15 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,8 @@ # define FILE_EXTENSION ".cub" # define FILE_EXTENSION_LEN 4 +# define BG_CLG 0 +# define BG_FLR 1 // -- graphic utils @@ -109,6 +111,7 @@ typedef enum e_error ERROR_PARSE, ERROR_CLI, ERROR_MLX, + ERROR_TEXTURE_FORMAT, ERROR_IMPLEM, } t_error; diff --git a/libft/src/ft_vector/ft_vec_at.c b/libft/src/ft_vector/ft_vec_at.c index e078789..67f647f 100644 --- a/libft/src/ft_vector/ft_vec_at.c +++ b/libft/src/ft_vector/ft_vec_at.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/27 16:03:56 by bgoulard #+# #+# */ -/* Updated: 2023/12/04 10:38:27 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 16:38:56 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,5 +15,7 @@ // return elem n void *ft_vec_at(t_vector *vec, size_t n) { + if (n >= vec->count) + return (NULL); return (vec->datas[n]); } diff --git a/mlx_layer/mlx_init.c b/mlx_layer/mlx_init.c index 91022d7..57a5a0b 100644 --- a/mlx_layer/mlx_init.c +++ b/mlx_layer/mlx_init.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/11 19:53:42 by rparodi #+# #+# */ -/* Updated: 2024/11/12 06:19:04 by bgoulard ### ########.fr */ +/* Updated: 2024/11/15 09:10:15 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,11 +20,11 @@ int c3_frame_update(void *inf_ptr); +/* move player w keys and call to redraw screen */ int c3_keyhook(int keycode, t_info *info) { if (keycode == XK_Escape || keycode == 65307) return (mlx_loop_end(info->mlx_ptr), EXIT_SUCCESS); - /* move player w keys and call to redraw screen */ return (EXIT_SUCCESS); } @@ -35,8 +35,8 @@ int c3_redcross(t_info *info) t_win_list *c3_init_mlx_window(t_info *info) { - int x; - int y; + int x; + int y; x = 0; y = 0; @@ -53,7 +53,8 @@ int init_mlx_env(t_info *info) if (!info->win_ptr) return (ERROR_MLX); mlx_hook(info->win_ptr, KeyPress, KeyPressMask, c3_keyhook, info); - mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, c3_redcross, info); + mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, c3_redcross, \ + info); mlx_loop_hook(info->mlx_ptr, c3_frame_update, info); return (NO_ERROR); } diff --git a/parsing/map.c b/parsing/map.c index bd752f4..04d829f 100644 --- a/parsing/map.c +++ b/parsing/map.c @@ -6,27 +6,32 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */ -/* Updated: 2024/11/13 07:20:15 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 17:21:09 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" +#include "cub3d_struct.h" +#include "mlx_functions.h" #include "ft_string.h" #include "ft_optional.h" #include "ft_optional_types.h" -#include "mlx_functions.h" +#include "ft_vector.h" +#include "ft_vector_types.h" +#include "ft_math.h" + +#include #include #include -// steps: -// 1. load_file into a buffer -// 2. store the map while deleting trailing newlines -void *load_file(void *data) +void *load_file(void *data) { - t_info *info = (t_info *)data; - char *file = NULL; + t_info *info; + char *file; + file = NULL; + info = (t_info *)data; file = ft_fd_to_buff(info->map.fd); if (file == NULL) return (info->last_error = ERROR_READ_FILE, NULL); @@ -38,12 +43,12 @@ void *load_file(void *data) return (info); } -bool is_identifier(const char *str, const char *id_str[4]) +bool is_identifier(const char *str, const char **id_str) { size_t i; i = 0; - while (i < 4) + while (id_str[i]) { if (ft_strstart_with(str, id_str[i])) return (true); @@ -52,13 +57,11 @@ bool is_identifier(const char *str, const char *id_str[4]) return (false); } -bool load_texture(t_info *info, const char *str) +bool load_texture(t_info *info, const char *str, const char **id_str) { - const char *id_str[4] = {"NO", "SO", "WE", "EA"}; size_t i; t_texture texture; - i = 0; while (i < 4) { @@ -67,62 +70,192 @@ bool load_texture(t_info *info, const char *str) texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " "); if (texture.path == NULL) return (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); - info->map.texture[i] = texture; - return (true); + return (info->last_error = ERROR_MLX, false); + return (info->map.texture[i] = texture, true); } i++; } return (false); } -void *load_textures(void *data) +bool color_from_str(const char *str, t_color *color) { - const char *id_str[4] = {"NO", "SO", "WE", "EA"}; - t_info *info = (t_info *)data; - size_t i; + int col[4]; + char **split; + split = ft_split(str, ','); + if (split == NULL) + return (false); + col[0] = ft_atoi(split[0]); + col[1] = ft_atoi(split[1]); + col[2] = ft_atoi(split[2]); + if (split[3]) + col[3] = ft_atoi(split[3]); + else + col[3] = 255; + if (col[0] < 0 || col[0] > 255 || col[1] < 0 || col[1] > 255 || \ + col[2] < 0 || col[2] > 255 || col[3] < 0 || col[3] > 255) + return (false); + *color = (t_color){.r = col[0], .g = col[1], .b = col[2], .a = col[3]}; + ft_free_2d((void **)split); + return (true); +} + +bool load_bg(t_info *info, const char *line, const char **id_str) +{ + t_color color; + size_t i; + + i = 0; + ft_bzero(&color, sizeof(t_color)); + while (id_str[i]) + { + if (ft_strstart_with(line, id_str[i]) && \ + (color_from_str(line + ft_strlen(id_str[i]), &color) == false || + color.a < 255)) + return (info->last_error = ERROR_PARSE, false); + if (ft_strstart_with(id_str[i], "F ")) + info->map.bg_colors[BG_FLR] = color; + else + info->map.bg_colors[BG_CLG] = color; + i++; + } + if (color.color == 0) + return (info->last_error = ERROR_PARSE, false); + return (true); +} + +void *load_textures(void *data) +{ + const char *id_str[] = {"NO ", "SO ", "WE ", "EA ", NULL}; + t_info *info; + size_t i; + + i = 0; + info = (t_info *)data; while (info->map.fraw[i]) { if (is_identifier(info->map.fraw[i], id_str) && \ - (load_texture(info, info->map.fraw[i]) == false)) - return (NULL); + load_texture(info, info->map.fraw[i], id_str) == false) + return (NULL); i++; } return (info); } -void *load_bgs(void *data) +void *load_bgs(void *data) { - t_info *info = (t_info *)data; + const char *id_str[] = {"F ", "C ", NULL}; + t_info *info; + size_t i; + i = 0; + info = (t_info *)data; + while (info->map.fraw[i]) + { + if (is_identifier(info->map.fraw[i], id_str) && \ + load_bg(info, info->map.fraw[i], id_str) == false) + return (NULL); + i++; + } + return (info); +} + +void *ft_strchrs(const char *str, const char *chrs) +{ + while (*str) + { + if (ft_strchr(chrs, *str)) + return ((void *)str); + str++; + } + return (NULL); +} + +void str_to_tile(const char *str, t_tile *tile, size_t size) +{ + size_t i; + + i = 0; + while (str[i]) + { + if (str[i] == '1' || i[str] == ' ') + tile[i] = WALL; + else + tile[i] = EMPTY; + i++; + } + while (i < size) + tile[i++] = WALL; +} + +t_vector *load_vector(t_map *map) +{ + const char *id_str__all[] = {"NO ", "SO ", "WE ", "EA ", "F ", "C ", NULL}; + t_vector *str_map; + size_t i; + + str_map = ft_vec_new(); + if (str_map == NULL) + return (NULL); + i = 0; + while (map->fraw[i] && + is_identifier(map->fraw[i], id_str__all)) + i++; + while (map->fraw[i + map->size.y]) + { + map->size.x = ft_max(map->size.x, ft_strlen(map->fraw[i])); + ft_vec_add(&str_map, map->fraw[i + map->size.y++]); + } + return (str_map); +} + +void *load_tiles(void *data) +{ + t_info *info; + t_vector *str_map; + size_t i; + + info = (t_info *)data; + str_map = load_vector(&info->map); + if (!str_map) + return (info->last_error = ERROR_MALLOC, NULL); + info->map.map = ft_calloc(sizeof(t_tile), \ + (info->map.size.y * info->map.size.x)); + if (!info->map.map) + return (ft_vec_destroy(&str_map), \ + info->last_error = ERROR_MALLOC, NULL); + i = 0; + 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); + i++; + } + return (ft_vec_destroy(&str_map), info); +} + +void *traverse_map(void *data) +{ + t_info *info; + + 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); } -void *load_tiles(void *data) +void parse_map(t_info *info) { - t_info *info = (t_info *)data; - - info->last_error = ERROR_IMPLEM; - return (NULL); -} - -void *traverse_map(void *data) -{ - t_info *info = (t_info *)data; - - info->last_error = ERROR_IMPLEM; - return (NULL); -} - -void parse_map(t_info *info) -{ - t_optional opt = {OPT_NONE, NULL}; - const t_data_tr_i function_list[] = { + t_optional opt; + const t_data_tr_i function_list[] = { load_file, load_textures, load_bgs, @@ -131,8 +264,7 @@ void parse_map(t_info *info) NULL, }; - opt.val = info; - opt.pres = OPT_SOME; + opt = (t_optional){.val = info, .pres = OPT_SOME}; info->map.path = info->cli_ctx.file; if (ft_optional_chain(&opt, function_list) == false) return (c3_perror(info), (void)0); diff --git a/raycast/frame_update.c b/raycast/frame_update.c index d0f9541..2540ffb 100644 --- a/raycast/frame_update.c +++ b/raycast/frame_update.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/12 06:02:54 by bgoulard #+# #+# */ -/* Updated: 2024/11/12 06:21:09 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 12:12:29 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,6 @@ int c3_frame_update(void *inf_ptr) info = inf_ptr; mlx_clear_window(info->mlx_ptr, info->win_ptr); - ft_putendl_fd("update called\n", STDOUT_FILENO); +// ft_putendl_fd("update called\n", STDOUT_FILENO); return (EXIT_SUCCESS); } diff --git a/sources/cleanups.c b/sources/cleanups.c index a5ff789..1e10f6e 100644 --- a/sources/cleanups.c +++ b/sources/cleanups.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:11:01 by bgoulard #+# #+# */ -/* Updated: 2024/11/12 11:06:44 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 17:14:06 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,16 +26,25 @@ static void cleanup_map(t_map *map) (close(map->fd), map->fd = 0); if (map->fraw) (ft_free_2d((void **)map->fraw), map->fraw = NULL); + if (map->map) + ft_free((void **)&map->map); } static void cleanup_mlx(t_info *info) { - if (info->mlx_ptr && info->win_ptr) + if (!info->mlx_ptr) + return ; + if (info->win_ptr) mlx_destroy_window(info->mlx_ptr, info->win_ptr); - if (info->mlx_ptr) - mlx_destroy_display(info->mlx_ptr); - if (info->mlx_ptr) - ft_free((void **)&info->mlx_ptr); + for (int i = 0; i < 4; i++) + { + if (info->map.texture[i].img) + mlx_destroy_image(info->mlx_ptr, info->map.texture[i].img); + if (info->map.texture[i].path) + ft_free((void **)&info->map.texture[i].path); + } + mlx_destroy_display(info->mlx_ptr); + ft_free((void **)&info->mlx_ptr); } void cleanup_info(t_info *info) diff --git a/sources/error.c b/sources/error.c index 1fe0942..0532121 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/12 08:45:03 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 11:31:05 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ #include -const char *g_error_message[] = { +const char *const g_error_message[] = { "no error", "unknown error", "could not open file", @@ -29,6 +29,7 @@ const char *g_error_message[] = { "parse error", "cli error", "mlx error", + "texture format error", "not implemented", }; diff --git a/sources/main.c b/sources/main.c index 0a5fdf6..6d3ebe7 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/13 06:56:50 by bgoulard ### ########.fr */ +/* Updated: 2024/11/18 14:17:27 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,13 +18,16 @@ #include #include #include +#include // not normed but we'll take care of this as a niceties at the last // possible moment :) void dump_info(t_info *info) { - const char *bool_str[2] = { "True", "False"}; + const char *bool_str[2] = {"True", "False"}; + size_t i; + i = 0; printf("t_info:\n"); printf("\tcli_ctx:\n"); printf("\t\tfile: %s\n", info->cli_ctx.file); @@ -35,9 +38,13 @@ void dump_info(t_info *info) printf("\t\tpath:%s\n", info->map.path); printf("\t\tfd:%d\n", info->map.fd); printf("\t\tsize:\t(x:%d, y:%d)\n", info->map.size.x, info->map.size.y); - printf("\t\tplayer_pos:\t(x:%lf, y:%lf)\n", info->map.player_pos.x, info->map.player_pos.y); - for (size_t i = 0; info->map.fraw[i]; i++) + printf("\t\tplayer_pos:\t(x:%lf, y:%lf)\n", info->map.player_pos.x, \ + info->map.player_pos.y); + while (info->map.fraw[i]) + { printf("\t\tmap.fraw[%zu]: %s\n", i, info->map.fraw[i]); + i++; + } } void check_err(t_info *info) @@ -55,6 +62,8 @@ void check_err(t_info *info) void run_cub3d(t_info *info) { + if (init_mlx_env(info) != NO_ERROR) + return ; parse_map(info); if (info->cli_ctx.debug) dump_info(info); @@ -62,7 +71,7 @@ void run_cub3d(t_info *info) return ; // todo: here // - validity check - init_mlx_env(info); + 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 @@ -75,7 +84,7 @@ void run_cub3d(t_info *info) /// @param file_arg the file path to the .cub file /// @param info the info structure /// @return false (0) if no error, true (1) if an error -int main_cub3d(char *file_arg, t_info *info) +int main_cub3d(char *file_arg, t_info *info) { if (info->cli_ctx.help) return (cleanup_info(info), EXIT_SUCCESS); diff --git a/sources/options.c b/sources/options.c index ca4808c..68caa7d 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/09 01:37:41 by bgoulard ### ########.fr */ +/* Updated: 2024/11/15 09:03:03 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,21 +19,21 @@ int c3_options(t_info *info, int argc, char *argv[]) { - int parsed_args; + int parsed_args; const t_opt opts[] = { - {"file", 'f', c3_set_file, OPT_ARG | OPT_EQSIGN | OPT_OTHER}, - {"debug", 'd', c3_set_debug, 0}, - {"save", 's', c3_set_save, 0}, - {"help", 'h', c3_print_help, 0}, + {"file", 'f', c3_set_file, OPT_ARG | OPT_EQSIGN | OPT_OTHER}, + {"debug", 'd', c3_set_debug, 0}, + {"save", 's', c3_set_save, 0}, + {"help", 'h', c3_print_help, 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. - {NULL, 0, NULL, 0} + {NULL, 0, NULL, 0} }; (void)argc; ft_bzero(info, sizeof(t_info)); - ft_setup_prog((const char * const *)argv); + ft_setup_prog((const char *const *)argv); ft_set_opt_list(opts); parsed_args = ft_parse_args((const char **)argv, &(info->cli_ctx)); return (parsed_args); diff --git a/sources/options_impl.c b/sources/options_impl.c index a01c3ef..3714df1 100644 --- a/sources/options_impl.c +++ b/sources/options_impl.c @@ -6,14 +6,14 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:12:16 by bgoulard #+# #+# */ -/* Updated: 2024/11/09 01:36:19 by bgoulard ### ########.fr */ +/* Updated: 2024/11/15 09:08:34 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d_struct.h" #include "ft_string.h" -void c3_set_file(void *usr_control_struct, const char *arg) +void c3_set_file(void *usr_control_struct, const char *arg) { t_cli *cli_ctx; @@ -21,7 +21,7 @@ void c3_set_file(void *usr_control_struct, const char *arg) cli_ctx->file = ft_strdup(arg); } -void c3_set_debug(void *usr_control_struct) +void c3_set_debug(void *usr_control_struct) { t_cli *cli_ctx; @@ -29,7 +29,7 @@ void c3_set_debug(void *usr_control_struct) cli_ctx->debug = true; } -void c3_set_save(void *usr_control_struct) +void c3_set_save(void *usr_control_struct) { t_cli *cli_ctx; @@ -37,16 +37,17 @@ void c3_set_save(void *usr_control_struct) cli_ctx->save = true; } -void c3_print_help(void *usr_control_struct) +void c3_print_help(void *usr_control_struct) { - t_cli *cli_ctx; + t_cli *cli_ctx; + const char *help_str = \ + "Usage: cub3d [options] \nOptions:\n" \ + "\t-f, --file : specify the file to load\n" \ + "\t-d, --debug : enable debug mode\n" \ + "\t-s, --save : save the state of the 'game' when closing\n" \ + "\t-h, --help : print this help\n"; cli_ctx = (t_cli *)usr_control_struct; cli_ctx->help = true; - ft_putstr_fd("Usage: cub3d [options] \n", STDOUT_FILENO); - ft_putstr_fd("Options:\n", STDOUT_FILENO); - ft_putstr_fd("\t-f, --file : specify the file to load\n", STDOUT_FILENO); - ft_putstr_fd("\t-d, --debug : enable debug mode\n", STDOUT_FILENO); - ft_putstr_fd("\t-s, --save : save the state of the 'game' when closing\n", STDOUT_FILENO); - ft_putstr_fd("\t-h, --help : print this help\n", STDOUT_FILENO); + ft_putstr_fd(help_str, STDOUT_FILENO); } 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 3/3] 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;