diff options
author | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-02-15 20:29:21 +0100 |
---|---|---|
committer | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-02-15 20:29:21 +0100 |
commit | 030fe8c3316d1e1f5f3dea81c3fffd8e00c16afa (patch) | |
tree | aa661716c5ffdd844e41bccb9be833637eaaa3b8 | |
parent | In progress (diff) | |
download | 42-cub3d-030fe8c3316d1e1f5f3dea81c3fffd8e00c16afa.tar.gz 42-cub3d-030fe8c3316d1e1f5f3dea81c3fffd8e00c16afa.tar.bz2 42-cub3d-030fe8c3316d1e1f5f3dea81c3fffd8e00c16afa.tar.xz 42-cub3d-030fe8c3316d1e1f5f3dea81c3fffd8e00c16afa.tar.zst 42-cub3d-030fe8c3316d1e1f5f3dea81c3fffd8e00c16afa.zip |
Work in progress, res now
-rw-r--r-- | inc/cub3d_defines.h | 19 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 2 | ||||
-rw-r--r-- | map/map_one.cub | 4 | ||||
-rw-r--r-- | src/ft_check_map_line.c | 40 | ||||
-rw-r--r-- | src/ft_exit.c | 3 | ||||
-rw-r--r-- | src/ft_get_map.c | 6 | ||||
-rw-r--r-- | src/ft_get_res.c | 25 | ||||
-rw-r--r-- | src/ft_get_screen_size.c | 4 | ||||
-rw-r--r-- | src/ft_init_lists.c | 4 | ||||
-rw-r--r-- | src/ft_map_error.c | 2 | ||||
-rw-r--r-- | src/ft_parse_map.c | 4 |
11 files changed, 83 insertions, 30 deletions
diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h index 6f8de65..14ec61a 100644 --- a/inc/cub3d_defines.h +++ b/inc/cub3d_defines.h @@ -54,15 +54,26 @@ # endif /* -** ====== ERROR MSG ====== +** ====== MAP ERROR MSG ====== */ # define FT_ERR_NOT_A_CUB "given map is not a .cub" -# define FT_ERR_MAP_L_L "last line is invalid or contains illegal char" -# define FT_ERR_UNFINISHED "unexpected file end" +# define FT_ERR_ARGS "too many arguments" +# define FT_ERR_RES_SMALL "resolution is too small" +# define FT_ERR_RES_ALPHA "resolution should be digits only" +# define FT_ERR_MAP_L_L "last line is invalid" +# define FT_ERR_UNFINISHED "no map" # define FT_ERR_READ "read error" # define FT_ERR_ILL_ENTRY "illegal map entry" # define FT_ERR_ALR_SET "duplicate entry" +# define FT_ERR_ALLOCATE "allocation error" +# define FT_ERR_ILL_MAP "map contains illegal char" +# define FT_ERR_MULT_SPAWN "multiple spawn points" + +/* +** ====== MISSING ERROR MSG ====== +*/ + # define FT_ERR_MISS_ELEMENT "Missing element:" # define FT_ERR_MISS_NORTH "north side texture" # define FT_ERR_MISS_SOUTH "south side texture" @@ -71,7 +82,7 @@ # define FT_ERR_MISS_SPRITE "sprite texture" # define FT_ERR_MISS_RESOLUTION "resolution" # define FT_ERR_MISS_FLOOR_C "floor color" -# define FT_ERR_MISS_CEIL_C "floor color" +# define FT_ERR_MISS_CEIL_C "ceiling color" # define FT_ERR_MISS_PLAYER_SPAWN "player spawn" # endif diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index fce155e..13225af 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -109,7 +109,7 @@ typedef struct s_map typedef struct s_cub { uint8_t minimap; - char *errmsg; + char errmsg[40]; struct s_win *wlist; struct s_player *plist; struct s_map *mlist; diff --git a/map/map_one.cub b/map/map_one.cub index 2c3f8a2..909b797 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -1,4 +1,4 @@ -R 1200 900 +R 1200 1 NO ./path_to_the_north_texture SO ./path_to_the_south_texture @@ -6,7 +6,7 @@ EA ./path_to_the_east_texture WE ./path_to_the_west_texture S ./path_to_the_sprite_texture -C 30,130,175 +C 30,130,255 F 150,150,145 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 diff --git a/src/ft_check_map_line.c b/src/ft_check_map_line.c index 9e4e484..b41d052 100644 --- a/src/ft_check_map_line.c +++ b/src/ft_check_map_line.c @@ -15,6 +15,38 @@ #include <stddef.h> #include <stdint.h> +static int8_t + ft_first_checks(char *line, size_t i, t_cub *clist) +{ + if (!ft_ischarset("012NSEW ", line[i])) + { + ft_strlcpy(clist->errmsg, FT_ERR_ILL_MAP, + ft_strlen(FT_ERR_ILL_MAP) + 1); + return (-1); + } + if (ft_ischarset("NSEW", line[i])) + clist->mlist->isspawn += 1; + if (clist->mlist->isspawn > 1) + { + ft_strlcpy(clist->errmsg, FT_ERR_MULT_SPAWN, + ft_strlen(FT_ERR_MULT_SPAWN) + 1); + return (-1); + } + return (0); +} + +static int8_t + ft_second_checks(char *line, size_t i, t_cub *clist) +{ + if (!ft_ischarset("1 ", line[i])) + { + ft_strlcpy(clist->errmsg, FT_ERR_ILL_MAP, + ft_strlen(FT_ERR_ILL_MAP) + 1); + return (-1); + } + return (0); +} + size_t ft_get_line_len(char *line) { @@ -42,16 +74,12 @@ int8_t { if (l != 1) { - if (!ft_ischarset("012NSEW ", line[i])) - return (-1); - if (ft_ischarset("NSEW", line[i])) - clist->mlist->isspawn += 1; - if (clist->mlist->isspawn > 1) + if (ft_first_checks(line, i, clist) < 0) return (-1); } else { - if (!ft_ischarset("1 ", line[i])) + if (ft_second_checks(line, i, clist) < 0) return (-1); } i++; diff --git a/src/ft_exit.c b/src/ft_exit.c index 53af7f9..f0a94a3 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -33,7 +33,6 @@ static void ft_memdel((void**)&clist->wlist->winptr); ft_memdel((void**)&clist->wlist->wlx); ft_memdel((void**)&clist->wlist); - ft_memdel((void**)&clist->errmsg); ft_memdel((void**)&clist); } @@ -44,8 +43,6 @@ int mlx_destroy_window(clist->wlist->wlx, clist->wlist->winptr); ft_free_lists(clist); ft_printf("Exiting program\n"); - if (exit_code < 0 || exit_code > 0) - ft_printf("Exit code: %hhu\n", exit_code); exit(exit_code); return (0); } diff --git a/src/ft_get_map.c b/src/ft_get_map.c index ee23c7a..cb677da 100644 --- a/src/ft_get_map.c +++ b/src/ft_get_map.c @@ -53,7 +53,11 @@ static int8_t if (!(clist->mlist->mapl = (char *)ft_nrealloc(clist->mlist->mapl, ((clist->mlist->map_w + 1) * i) * sizeof(char), ((clist->mlist->map_w + 1) * (i + 1)) * sizeof(char)))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, + ft_strlen(FT_ERR_ALLOCATE) + 1); return (-1); + } ft_linecpy(line, clist->mlist->mapl, (clist->mlist->map_w + 1) * i); return (0); } @@ -65,12 +69,14 @@ int if (!line[0]) { ft_memdel((void**)&line); + ft_strlcpy(clist->errmsg, FT_ERR_READ, ft_strlen(FT_ERR_READ) + 1); return (-1); } clist->mlist->map_w = ft_get_line_len(line); if (ft_check_map_line(line, 1, clist) < 0) { ft_memdel((void**)&line); + ft_strlcpy(clist->errmsg, FT_ERR_READ, ft_strlen(FT_ERR_READ) + 1); return (-1); } ft_memdel((void**)&clist->mlist->mapl); diff --git a/src/ft_get_res.c b/src/ft_get_res.c index 86a3186..e51a66f 100644 --- a/src/ft_get_res.c +++ b/src/ft_get_res.c @@ -14,7 +14,7 @@ #include <cub3d.h> static int8_t - ft_checkdigit(const char *word) + ft_checkdigit(const char *word, t_cub *clist) { size_t i; @@ -22,7 +22,11 @@ static int8_t while (ft_isdigit(word[i])) i++; if (i != ft_strlen(word)) + { + ft_strlcpy(clist->errmsg, FT_ERR_RES_ALPHA, + ft_strlen(FT_ERR_RES_ALPHA) + 1); return (-1); + } return (0); } @@ -34,20 +38,23 @@ int8_t wlist = clist->wlist; if (!(*words + 0) || !(*(words + 1)) || !(*(words + 2)) || (*(words + 3))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); return (-1); - if ((ft_checkdigit(words[1]) < 0) || - (ft_checkdigit(words[2]) < 0)) + } + if ((ft_checkdigit(words[1], clist) < 0) || + (ft_checkdigit(words[2], clist) < 0)) return (-1); wlist->x_size = ft_atoi(words[1]); wlist->y_size = ft_atoi(words[2]); - if (wlist->x_size < 1 - || wlist->y_size < 1) + if (wlist->x_size <= 1 + || wlist->y_size <= 1) + { + ft_strlcpy(clist->errmsg, FT_ERR_RES_SMALL, + ft_strlen(FT_ERR_RES_SMALL) + 1); return (-1); + } if (ft_get_screen_size(wlist) < 0) return (-1); - if (wlist->x_size > wlist->x_max_size) - wlist->x_size = wlist->x_max_size; - if (wlist->y_size > wlist->y_max_size) - wlist->y_size = wlist->y_max_size; return (0); } diff --git a/src/ft_get_screen_size.c b/src/ft_get_screen_size.c index fe368c4..2b437c3 100644 --- a/src/ft_get_screen_size.c +++ b/src/ft_get_screen_size.c @@ -24,5 +24,9 @@ int8_t return (-1); wlist->x_max_size = ft_atoi(words[0]); wlist->y_max_size = ft_atoi(words[1]); + if (wlist->x_size > wlist->x_max_size) + wlist->x_size = wlist->x_max_size; + if (wlist->y_size > wlist->y_max_size) + wlist->y_size = wlist->y_max_size; return (ft_free_words(words)); } diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c index 459e522..95f0488 100644 --- a/src/ft_init_lists.c +++ b/src/ft_init_lists.c @@ -71,9 +71,9 @@ static t_cub if (!(clist = (t_cub*)malloc(sizeof(t_cub)))) return (NULL); if (!(clist->plist = ft_init_player()) || - !(clist->mlist = ft_init_map()) || - !(clist->errmsg = ft_calloc(1, sizeof(char)))) + !(clist->mlist = ft_init_map())) return (NULL); + ft_bzero(clist->errmsg, 40); clist->minimap = 0; clist->f_rgb = ft_init_rgb(); clist->c_rgb = ft_init_rgb(); diff --git a/src/ft_map_error.c b/src/ft_map_error.c index cb099dc..5d5ab68 100644 --- a/src/ft_map_error.c +++ b/src/ft_map_error.c @@ -19,7 +19,7 @@ int { ft_dprintf(STDERR_FILENO, "Error\n"); ft_dprintf(STDERR_FILENO, - "\033[1;31mMap error: line %3zu: %s\033[0m\n", + "\033[1;31mMap error: line %zu: %s\033[0m\n", clist->mlist->line_chk, errmsg); return (ft_exit(4, clist)); diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c index 0eef871..c4aa011 100644 --- a/src/ft_parse_map.c +++ b/src/ft_parse_map.c @@ -90,12 +90,12 @@ void if (fd < 0) ft_no_map_error(clist); ret = 1; - while (ret != 12 && ret != -1) + while (ret != 12 && ret >= 0) ret = ft_parse_it(fd, clist); (ret == -2) ? (ft_map_error(FT_ERR_ALR_SET, clist)) : 0; (ret == -1) ? (ft_map_error(clist->errmsg, clist)) : 0; if (ft_get_map_core(fd, clist) < 0) - ft_map_error(clist); + ft_map_error(clist->errmsg, clist); ft_check_map_last_line(clist); ft_get_player_spawn(clist->plist, clist); ft_check_missing(clist); |