From 820b5f3bcca8868aee7cc57628b72e6e468ab203 Mon Sep 17 00:00:00 2001 From: "B.Goulard" <84p71573@gmail.com> Date: Thu, 19 Dec 2024 22:35:37 +0100 Subject: [PATCH] Fix: check on background + check for metainfo in the map + fix tilemap size calculation --- includes/cub3d_struct.h | 5 ++++- maps/bad_open_map.cub | 25 ++++++++++++--------- parsing/load_bgs.c | 12 +++++++--- parsing/load_tiles.c | 25 ++++++++++++++------- sources/error.c | 50 +++++++++++++++++++++++------------------ 5 files changed, 72 insertions(+), 45 deletions(-) diff --git a/includes/cub3d_struct.h b/includes/cub3d_struct.h index d97bcf3..0de80e4 100644 --- a/includes/cub3d_struct.h +++ b/includes/cub3d_struct.h @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/28 13:53:54 by bgoulard #+# #+# */ -/* Updated: 2024/12/17 17:21:45 by bgoulard ### ########.fr */ +/* Updated: 2024/12/19 22:29:11 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -120,6 +120,7 @@ typedef struct s_cli // -- error utils +// keep ERROR_IMPLEM as last (used to get enum size) typedef enum e_error { NO_ERROR = 0, @@ -133,7 +134,9 @@ typedef enum e_error ERROR_PARSE, ERROR_MAP_OPEN, ERROR_PARSE_BG_COLOR_FORMAT, + ERROR_PARSE_NO_BG_COLOR, ERROR_PARSE_ALREADY_SET, + ERROR_PARSE_META_IN_MAP, ERROR_CLI, ERROR_MLX, ERROR_TEXTURE_FORMAT, diff --git a/maps/bad_open_map.cub b/maps/bad_open_map.cub index 6d730a2..5a6c30e 100644 --- a/maps/bad_open_map.cub +++ b/maps/bad_open_map.cub @@ -1,20 +1,23 @@ -SO ./textures/b.xpm -NO ./textures/wasteland_32.xpm -WE ./textures/sandy_32.xpm -EA ./textures/cobblestone_32.xpm +SO ./textures/chiseled_tuff.xpm +NO ./textures/chiseled_tuff_bricks.xpm +WE ./textures/chiseled_stone_bricks.xpm +EA ./textures/chiseled_tuff_bricks_top.xpm + + F 0,255,0 -C 0,123,255 - - +C 0,255,255 + 11111111111111111 + 10000000000000001 + 11111111111111111 11111111111111111111 10000011000001010101 10000011000000000001 10111111111111000101 -1011 1000000 -10111111 11000100 -10000011111110000000 -100000000000000N0000 +1011 1000001 +10111111 11000101 +10000011111110000001 +100000000000000N0001 11111111111111111111 diff --git a/parsing/load_bgs.c b/parsing/load_bgs.c index a02069c..6f93b1a 100644 --- a/parsing/load_bgs.c +++ b/parsing/load_bgs.c @@ -6,11 +6,12 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/01 17:47:37 by bgoulard #+# #+# */ -/* Updated: 2024/12/16 09:36:28 by bgoulard ### ########.fr */ +/* Updated: 2024/12/19 22:10:37 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_addons.h" +#include "cub3d.h" #include "cub3d_struct.h" #include "cub3d_parsing.h" #include "ft_string.h" @@ -43,7 +44,7 @@ bool color_from_str(const char *str, t_color *color) return (true); } -bool load_bg(t_info *info, const char *line, const char **id_str) +bool load_bg(t_info *info, const char *line, const char **id_str, bool *tpl) { t_color color; size_t id_idx; @@ -60,6 +61,7 @@ bool load_bg(t_info *info, const char *line, const char **id_str) info->map.bg_colors[BG_SKY] = color; else return (info->last_error = ERROR_PARSE_ALREADY_SET, false); + tpl[id_idx] = true; return (true); } @@ -68,15 +70,19 @@ void *load_bgs(void *data) const char *id_str[] = {[BG_FLR] = "F ", [BG_SKY] = "C ", [2] = NULL}; t_info *info; size_t i; + bool tuple[2]; i = 0; info = (t_info *)data; + ft_bzero(tuple, sizeof(bool) * 2); while (info->map.fraw[i]) { if (is_identifier(info->map.fraw[i], id_str) && \ - load_bg(info, info->map.fraw[i], id_str) == false) + load_bg(info, info->map.fraw[i], id_str, tuple) == false) return (NULL); i++; } + if (tuple[BG_FLR] == false || tuple[BG_SKY] == false) + return (sv_errno(info, ERROR_PARSE_NO_BG_COLOR), NULL); return (info); } diff --git a/parsing/load_tiles.c b/parsing/load_tiles.c index c87fe00..418692c 100644 --- a/parsing/load_tiles.c +++ b/parsing/load_tiles.c @@ -6,10 +6,11 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/01 17:47:15 by bgoulard #+# #+# */ -/* Updated: 2024/12/16 09:33:53 by bgoulard ### ########.fr */ +/* Updated: 2024/12/19 22:32:16 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ +#include "cub3d.h" #include "cub3d_struct.h" #include "cub3d_parsing.h" @@ -30,7 +31,7 @@ void *ft_strchrs(const char *str, const char *chrs) return (NULL); } -void str_to_tile(const char *str, t_tile *tile, size_t size) +int str_to_tile(const char *str, t_tile *tile, size_t size) { size_t i; @@ -39,12 +40,15 @@ void str_to_tile(const char *str, t_tile *tile, size_t size) { if (str[i] == '1' || str[i] == ' ') tile[i].tile_type = WALL; + else if (!ft_strchr("0NSWE", str[i])) + return (-1); else tile[i].tile_type = EMPTY; i++; } while (i < size) tile[i++].tile_type = WALL; + return (0); } t_vector *load_vector(t_map *map) @@ -52,8 +56,10 @@ 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; + size_t max; str_map = ft_vec_new(); + max = 0; if (str_map == NULL) return (NULL); i = 0; @@ -61,9 +67,10 @@ t_vector *load_vector(t_map *map) i++; while (map->fraw[i + map->size.y]) { - map->size.x = ft_max(map->size.x, ft_strlen(map->fraw[i])); + max = ft_max(max, ft_strlen(map->fraw[i + map->size.y])); ft_vec_add(&str_map, map->fraw[i + map->size.y++]); } + map->size.x = max; return (str_map); } @@ -95,6 +102,8 @@ int set_player(t_info *info, int i, t_vector *str_map) return (EXIT_SUCCESS); } +#define EPMIM ERROR_PARSE_META_IN_MAP + void *load_tiles(void *data) { t_info *info; @@ -104,17 +113,17 @@ void *load_tiles(void *data) info = (t_info *)data; str_map = load_vector(&info->map); if (!str_map) - return (info->last_error = ERROR_MALLOC, NULL); + return (sv_errno(info, 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); + return (ft_vec_destroy(&str_map), sv_errno(info, 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); + if (str_to_tile(ft_vec_at(str_map, i), info->map.map + (i \ + * info->map.size.x), info->map.size.x) == -1) + return (ft_vec_destroy(&str_map), sv_errno(info, EPMIM), NULL); if (ft_strchrs(ft_vec_at(str_map, i), "SNWE") && \ set_player(info, i, str_map) != EXIT_SUCCESS) return (NULL); diff --git a/sources/error.c b/sources/error.c index 5d48182..6e5cc21 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/12/17 17:06:42 by bgoulard ### ########.fr */ +/* Updated: 2024/12/19 22:29:32 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,36 +20,42 @@ const char **get_error_message(void) { - static const char *error_messages[] = { - [NO_ERROR] = "no error", - [ERROR_UNKNOWN] = "unknown error", - [ERROR_OPEN_FILE] = "could not open file", - [ERROR_READ_FILE] = "could not read file", - [ERROR_EXTENSION_FILE] = "bad file extension", - [ERROR_NAME_FILE] = "invalid file name", - [ERROR_MISSING_FILE] = "missing file", - [ERROR_MALLOC] = "malloc error", - [ERROR_PARSE] = "parse error", - [ERROR_MAP_OPEN] = "map open error", - [ERROR_PARSE_BG_COLOR_FORMAT] = "bad format for background color", - [ERROR_PARSE_ALREADY_SET] = "variable was set multiple times", - [ERROR_CLI] = "cli error", - [ERROR_MLX] = "mlx error", - [ERROR_TEXTURE_FORMAT] = "texture format error", - [ERROR_IMPLEM] = "not implemented", - [ERROR_TEXTURE_MISSING] = "texture missing", - }; + static const char *err_msg[ERROR_IMPLEM + 1] = { NULL }; - return (error_messages); + err_msg[NO_ERROR] = "no error"; + err_msg[ERROR_UNKNOWN] = "unknown error"; + err_msg[ERROR_OPEN_FILE] = "could not open file"; + err_msg[ERROR_READ_FILE] = "could not read file"; + err_msg[ERROR_EXTENSION_FILE] = "bad file extension"; + err_msg[ERROR_NAME_FILE] = "invalid file name"; + err_msg[ERROR_MISSING_FILE] = "missing file"; + err_msg[ERROR_MALLOC] = "malloc error"; + err_msg[ERROR_PARSE] = "parse error"; + err_msg[ERROR_MAP_OPEN] = "map open error"; + err_msg[ERROR_PARSE_BG_COLOR_FORMAT] = "bad format for background color"; + err_msg[ERROR_PARSE_ALREADY_SET] = "variable was set multiple times"; + err_msg[ERROR_CLI] = "cli error"; + err_msg[ERROR_MLX] = "mlx error"; + err_msg[ERROR_TEXTURE_FORMAT] = "texture format error"; + err_msg[ERROR_IMPLEM] = "not implemented"; + err_msg[ERROR_TEXTURE_MISSING] = "texture missing"; + err_msg[ERROR_PARSE_NO_BG_COLOR] = "no background color provided"; + err_msg[ERROR_PARSE_META_IN_MAP] = "meta data in map (should be above)"; + + return (err_msg); } void c3_perror(t_info *info) { const char **errs_msg = get_error_message(); + const char *msg; if (info->last_error == NO_ERROR) return ; - print_error(errs_msg[info->last_error], info->errno_state); + msg = errs_msg[info->last_error]; + if (!msg) + msg = errs_msg[ERROR_UNKNOWN]; + print_error(msg, info->errno_state); } void print_error(const char *msg, int state)