diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | inc/cub3d.h | 4 | ||||
-rw-r--r-- | inc/cub3d_defines.h | 38 | ||||
-rw-r--r-- | inc/cub3d_structs.h | 5 | ||||
-rw-r--r-- | map/map_one.cub | 42 | ||||
-rw-r--r-- | map/map_vim.cub | 25 | ||||
-rw-r--r-- | src/ft_check_ext.c | 25 | ||||
-rw-r--r-- | src/ft_check_map_line.c | 66 | ||||
-rw-r--r-- | src/ft_draw_verline.c | 4 | ||||
-rw-r--r-- | src/ft_exit.c | 2 | ||||
-rw-r--r-- | src/ft_get_colors.c | 86 | ||||
-rw-r--r-- | src/ft_get_map.c | 6 | ||||
-rw-r--r-- | src/ft_get_res.c | 26 | ||||
-rw-r--r-- | src/ft_get_screen_size.c | 4 | ||||
-rw-r--r-- | src/ft_get_sprite.c | 14 | ||||
-rw-r--r-- | src/ft_get_tex.c | 52 | ||||
-rw-r--r-- | src/ft_init_lists.c | 1 | ||||
-rw-r--r-- | src/ft_init_winlx.c | 2 | ||||
-rw-r--r-- | src/ft_map_error.c | 6 | ||||
-rw-r--r-- | src/ft_parse_map.c | 62 | ||||
-rw-r--r-- | src/ft_raycasting.c | 2 | ||||
-rw-r--r-- | src/ft_select_get.c | 2 |
22 files changed, 343 insertions, 136 deletions
@@ -1,4 +1,5 @@ default: all + #==================================================================================================# #-------------------------------------------- Shell -----------------------------------------------# #==================================================================================================# @@ -51,7 +52,11 @@ SRCS_NAME += ft_init_s_ray.c SRCS_NAME += ft_init_map.c SRCS_NAME += ft_tex_init.c SRCS_NAME += ft_detect.c +<<<<<<< HEAD +SRCS_NAME += ft_check_ext.c +======= SRCS_NAME += ft_draw_textures.c +>>>>>>> 8a89388d397b50f2c70cef05f8fa6cfa10cc213c #--------------------------------------------------------------------------------------------------# SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME}) #--------------------------------------------------------------------------------------------------# diff --git a/inc/cub3d.h b/inc/cub3d.h index 04033b0..a691023 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -25,7 +25,6 @@ ** 3: failed mlx init ** 4: map error ** 5: no map -** 6: not a .cub */ int8_t ft_init_cub3d(t_cub **clist); @@ -57,7 +56,7 @@ int8_t ft_check_map_line(char *line, uint8_t l, t_cub *clist); size_t ft_get_line_len(char *line); int ft_missing_error(const char *err, t_cub *clist); uint8_t ft_free_words(char **words); -int ft_map_error(t_cub *clist); +int ft_map_error(const char *errmsg, t_cub *clist); int ft_init_winlx(t_cub *clist); void ft_draw_scene(t_cub *clist); void ft_print_list(t_cub *clist); @@ -75,5 +74,6 @@ int ft_d_key(t_cub *clist); int ft_f1_key(t_cub *clist); int ft_left_key(t_cub *clist); int ft_right_key(t_cub *clist); +int8_t ft_check_ext(const char *filep, const char *ext); # endif diff --git a/inc/cub3d_defines.h b/inc/cub3d_defines.h index e6c98c5..0c9a294 100644 --- a/inc/cub3d_defines.h +++ b/inc/cub3d_defines.h @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cub3d_defines.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/15 15:40:25 by rbousset #+# #+# */ +/* Updated: 2020/02/15 15:40:27 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + # ifndef CUB3D_DEFINES_H # define CUB3D_DEFINES_H @@ -42,7 +54,29 @@ # endif /* -** ====== ERROR MSG ====== +** ====== MAP ERROR MSG ====== +*/ + +# define FT_ERR_ALLOCATE "allocation error" +# define FT_ERR_READ "read error" +# define FT_ERR_NOT_A_CUB "given map is not a .cub" +# define FT_ERR_ARGS "too many or to few arguments" +# define FT_ERR_RES_SMALL "resolution is too small" +# define FT_ERR_RES_ALPHA "resolution should be digits only" +# define FT_ERR_NOT_A_XPM "given texture is not a .xpm" +# define FT_ERR_COLOR_ALPHA "colors should be digits only" +# define FT_ERR_COLOR_MAX "colors should be maximum 255" +# define FT_ERR_COLOR_ARGS "colors three numbers separated by commas" +# define FT_ERR_UNFINISHED "no map" +# define FT_ERR_MAP_LEN "map length inconsistency" +# define FT_ERR_ILL_ENTRY "illegal map entry" +# define FT_ERR_ALR_SET "duplicate entry" +# define FT_ERR_ILL_MAP "map contains illegal char" +# define FT_ERR_MULT_SPAWN "multiple spawn points" +# define FT_ERR_MAP_L_L "last line is invalid" + +/* +** ====== MISSING ERROR MSG ====== */ # define FT_ERR_MISS_ELEMENT "Missing element:" @@ -53,7 +87,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 07b1be1..38472a6 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -23,8 +23,8 @@ typedef struct s_win uint8_t inited; uint16_t x_max_size; uint16_t y_max_size; - uint16_t x_size; - uint16_t y_size; + uint32_t x_size; + uint32_t y_size; } t_win; typedef struct s_img @@ -110,6 +110,7 @@ typedef struct s_cub { uint8_t w_side; uint8_t minimap; + 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 637fef5..c3b5bdf 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -1,25 +1,33 @@ -R 1200 900 +R 1200 800 +<<<<<<< HEAD +NO ./path_to_the_north_texture.xpm +SO ./path_to_the_south_texture.xpm +EA ./path_to_the_east_texture.xpm +WE ./path_to_the_west_texture.xpm +S ./path_to_the_sprite_texture.xpm +======= NO ./map/img/crapaud.xpm SO ./map/img/linus.xpm EA ./map/img/linuz.xpm WE ./map/img/segfault2.xpm S ./map/img/segfot.xpm +>>>>>>> 8a89388d397b50f2c70cef05f8fa6cfa10cc213c -C 30,130,175 -F 150,150,145 +C 50,100,200 +F 150,150,124 -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 -1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 2 0 0 0 0 0 1 0 0 0 1 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 -1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1 -1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 -1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 0 1 0 0 0 1 -1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 -1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 W 0 1 -1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1 -1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 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 1 +11111111111111111 1 +1 00000000011000001 +1N0100000000000001 +101100000111000001 +111111111111111111 +100000000011000001 +111101111111110111 +111101111111110111 +11000 0001101010111 +100020000000000011 +100000000000000011 +1100000011010101 11 +111101111111010111 +111111111111111111 diff --git a/map/map_vim.cub b/map/map_vim.cub deleted file mode 100644 index face56c..0000000 --- a/map/map_vim.cub +++ /dev/null @@ -1,25 +0,0 @@ -R 1200 600 - -NO ./path_to_the_north_texture -SO ./path_to_the_south_texture -EA ./path_to_the_east_texture -WE ./path_to_the_west_texture -S ./path_to_the_sprite_texture - -F 220,100,0 -C 225,30,0 - -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 -1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 2 0 0 0 0 0 1 0 0 0 1 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 -1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1 -1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 -1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 0 1 0 0 0 1 -1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 -1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 1 -1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 N 0 1 -1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 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 1 diff --git a/src/ft_check_ext.c b/src/ft_check_ext.c new file mode 100644 index 0000000..9a953d0 --- /dev/null +++ b/src/ft_check_ext.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_ext.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/17 15:33:28 by rbousset #+# #+# */ +/* Updated: 2020/02/17 15:33:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdint.h> + +int8_t + ft_check_ext(const char *filep, const char *ext) +{ + const uint8_t extlen = ft_strlen(ext); + + if (ft_strncmp(filep + (ft_strlen(filep) - extlen), ext, extlen)) + return (-1); + else + return (0); +} diff --git a/src/ft_check_map_line.c b/src/ft_check_map_line.c index 9e4e484..1009aa3 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) { @@ -32,32 +64,44 @@ size_t return (i - j); } +static int8_t + ft_check_side_walls(char *line, size_t i, t_cub *clist) +{ + if (line[0] != '1' || line[i - 1] != '1') + { + ft_strlcpy(clist->errmsg, FT_ERR_ILL_ENTRY, + ft_strlen(FT_ERR_ILL_ENTRY) + 1); + return (-1); + } + return (0); +} + int8_t ft_check_map_line(char *line, uint8_t l, t_cub *clist) { size_t i; - i = 0; - while (line[i]) + i = -1; + while (line[++i]) { 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++; } - if (line[0] != '1' || line[i - 1] != '1' - || ft_get_line_len(line) != clist->mlist->map_w) + if (ft_check_side_walls(line, i, clist) < 0) return (-1); + if (ft_get_line_len(line) != clist->mlist->map_w) + { + ft_strlcpy(clist->errmsg, FT_ERR_MAP_LEN, + ft_strlen(FT_ERR_MAP_LEN) + 1); + return (-1); + } return (0); } diff --git a/src/ft_draw_verline.c b/src/ft_draw_verline.c index 8156473..bcddbc7 100644 --- a/src/ft_draw_verline.c +++ b/src/ft_draw_verline.c @@ -15,7 +15,7 @@ static void ft_draw_floor(t_cub *cl, int32_t y, int32_t x) { - while (y < cl->wlist->y_size) + while ((uint32_t)y < cl->wlist->y_size) { *(int*)(cl->img.ptr + (x * 4 + (y * cl->img.sizeline))) = ft_rgb_to_hex(cl->f_rgb); @@ -47,7 +47,7 @@ int8_t y1 = 0; if (y2 < 0) y2 = 0; - if (y2 >= cl->wlist->y_size) + if ((uint32_t)y2 >= cl->wlist->y_size) y2 = cl->wlist->x_size - 1; if (y1 > y2) { diff --git a/src/ft_exit.c b/src/ft_exit.c index d4b69ba..f0a94a3 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -43,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_colors.c b/src/ft_get_colors.c index 869dd71..f54fabc 100644 --- a/src/ft_get_colors.c +++ b/src/ft_get_colors.c @@ -13,17 +13,57 @@ #include <libft.h> #include <cub3d.h> #include <stddef.h> +#include <stdint.h> -static int - ft_check_digits(const char *word) +static int8_t + ft_check_color_digits(char **num, t_cub *clist) { size_t i; + uint8_t j; i = 0; - while (ft_isdigit(word[i])) - i++; - if (i != ft_strlen(word)) + j = 0; + while (j < 3) + { + while (ft_isdigit(num[j][i])) + i++; + if (i != ft_strlen(num[j])) + { + ft_free_words(num); + ft_strlcpy(clist->errmsg, FT_ERR_COLOR_ALPHA, + ft_strlen(FT_ERR_COLOR_ALPHA) + 1); + return (-1); + } + i = 0; + j++; + } + return (0); +} + +static int8_t + ft_check_max_int(char **num, t_cub *clist) +{ + if (ft_atoi(num[0]) > 255 || ft_atoi(num[1]) > 255 + || ft_atoi(num[2]) > 255) + { + ft_strlcpy(clist->errmsg, FT_ERR_COLOR_MAX, + ft_strlen(FT_ERR_COLOR_MAX) + 1); + ft_free_words(num); + return (-1); + } + return (0); +} + +static int8_t + ft_check_nums_amount(char **num, t_cub *clist) +{ + if (!num[0] || !num[1] || !num[2] || num[3]) + { + ft_strlcpy(clist->errmsg, FT_ERR_COLOR_ARGS, + ft_strlen(FT_ERR_COLOR_ARGS) + 1); + ft_free_words(num); return (-1); + } return (0); } @@ -33,17 +73,22 @@ int8_t char **num; if (!(*words) || !words[1] || words[2]) + { + ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); return (-1); + } if (!(num = ft_split(words[1], ','))) - return (-1); - if (!num[0] || !num[1] || !num[2] || num[3] || - ft_check_digits(num[0]) || ft_check_digits(num[1]) || - ft_check_digits(num[2]) || ft_atoi(num[0]) > 255 || - ft_atoi(num[1]) > 255 || ft_atoi(num[2]) > 255) { - ft_free_words(num); + ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, + ft_strlen(FT_ERR_ALLOCATE) + 1); return (-1); } + if (ft_check_nums_amount(num, clist) < 0) + return (-1); + if (ft_check_color_digits(num, clist) < 0) + return (-1); + if (ft_check_max_int(num, clist) < 0) + return (-1); clist->f_rgb.r = ft_atoi(num[0]); clist->f_rgb.g = ft_atoi(num[1]); clist->f_rgb.b = ft_atoi(num[2]); @@ -56,18 +101,23 @@ int8_t { char **num; - if (!(*words) || ft_strcmp(*words, "C") || !words[1] || words[2]) + if (!(*words) || !words[1] || words[2]) + { + ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); return (-1); + } if (!(num = ft_split(words[1], ','))) - return (-1); - if (!num[0] || !num[1] || !num[2] || num[3] || - ft_check_digits(num[0]) || ft_check_digits(num[1]) || - ft_check_digits(num[2]) || ft_atoi(num[0]) > 255 || - ft_atoi(num[1]) > 255 || ft_atoi(num[2]) > 255) { - ft_free_words(num); + ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, + ft_strlen(FT_ERR_ALLOCATE) + 1); return (-1); } + if (ft_check_nums_amount(num, clist) < 0) + return (-1); + if (ft_check_color_digits(num, clist) < 0) + return (-1); + if (ft_check_max_int(num, clist) < 0) + return (-1); clist->c_rgb.r = ft_atoi(num[0]); clist->c_rgb.g = ft_atoi(num[1]); clist->c_rgb.b = ft_atoi(num[2]); 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..8607f62 100644 --- a/src/ft_get_res.c +++ b/src/ft_get_res.c @@ -12,9 +12,10 @@ #include <libft.h> #include <cub3d.h> +#include <stdint.h> static int8_t - ft_checkdigit(const char *word) + ft_checkdigit(const char *word, t_cub *clist) { size_t i; @@ -22,7 +23,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 +39,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_get_sprite.c b/src/ft_get_sprite.c index cdea85c..791f51f 100644 --- a/src/ft_get_sprite.c +++ b/src/ft_get_sprite.c @@ -12,14 +12,28 @@ #include <libft.h> #include <cub3d.h> +#include <stdint.h> int8_t ft_get_sprite(char **words, t_cub *clist) { if (!(*words) || !words[1] || words[2]) + { + ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); return (-1); + } + if (ft_check_ext(*(words + 1), ".xpm") < 0) + { + ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM, + ft_strlen(FT_ERR_NOT_A_XPM) + 1); + return (-1); + } ft_memdel((void**)&clist->mlist->sprite_path); if (!(clist->mlist->sprite_path = ft_strdup(*(words + 1)))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, + ft_strlen(FT_ERR_ALLOCATE) + 1); return (-1); + } return (0); } diff --git a/src/ft_get_tex.c b/src/ft_get_tex.c index 1965e72..ad093ed 100644 --- a/src/ft_get_tex.c +++ b/src/ft_get_tex.c @@ -18,10 +18,23 @@ int8_t ft_get_tex_no(char **words, t_cub *clist) { if (!(*words) || !(*(words + 1)) || (*(words + 2))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); return (-1); + } + if (ft_check_ext(*(words + 1), ".xpm") < 0) + { + ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM, + ft_strlen(FT_ERR_NOT_A_XPM) + 1); + return (-1); + } ft_memdel((void**)&clist->mlist->no_tex_path); if (!(clist->mlist->no_tex_path = ft_strdup(*(words + 1)))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, + ft_strlen(FT_ERR_ALLOCATE) + 1); return (-1); + } return (0); } @@ -29,10 +42,23 @@ int8_t ft_get_tex_so(char **words, t_cub *clist) { if (!(*words) || !(*(words + 1)) || (*(words + 2))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); + return (-1); + } + if (ft_check_ext(*(words + 1), ".xpm") < 0) + { + ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM, + ft_strlen(FT_ERR_NOT_A_XPM) + 1); return (-1); + } ft_memdel((void**)&clist->mlist->so_tex_path); if (!(clist->mlist->so_tex_path = ft_strdup(*(words + 1)))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, + ft_strlen(FT_ERR_ALLOCATE) + 1); return (-1); + } return (0); } @@ -40,10 +66,23 @@ int8_t ft_get_tex_ea(char **words, t_cub *clist) { if (!(*words) || !(*(words + 1)) || (*(words + 2))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); return (-1); + } + if (ft_check_ext(*(words + 1), ".xpm") < 0) + { + ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM, + ft_strlen(FT_ERR_NOT_A_XPM) + 1); + return (-1); + } ft_memdel((void**)&clist->mlist->ea_tex_path); if (!(clist->mlist->ea_tex_path = ft_strdup(*(words + 1)))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, + ft_strlen(FT_ERR_ALLOCATE) + 1); return (-1); + } return (0); } @@ -51,9 +90,22 @@ int8_t ft_get_tex_we(char **words, t_cub *clist) { if (!(*words) || !(*(words + 1)) || (*(words + 2))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); + return (-1); + } + if (ft_check_ext(*(words + 1), ".xpm") < 0) + { + ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM, + ft_strlen(FT_ERR_NOT_A_XPM) + 1); return (-1); + } ft_memdel((void**)&clist->mlist->we_tex_path); if (!(clist->mlist->we_tex_path = ft_strdup(*(words + 1)))) + { + ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, + ft_strlen(FT_ERR_ALLOCATE) + 1); return (-1); + } return (0); } diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c index dc50231..95f0488 100644 --- a/src/ft_init_lists.c +++ b/src/ft_init_lists.c @@ -73,6 +73,7 @@ static t_cub if (!(clist->plist = ft_init_player()) || !(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_init_winlx.c b/src/ft_init_winlx.c index 1b60477..6657408 100644 --- a/src/ft_init_winlx.c +++ b/src/ft_init_winlx.c @@ -26,7 +26,7 @@ int clist->wlist->x_size, clist->wlist->y_size, "Cub3D"))) return (-1); clist->wlist->inited = 1; - ft_printf("Created window of size %dx%d\n", + ft_printf("Created window of size %ux%u\n", clist->wlist->x_size, clist->wlist->y_size); return (0); } diff --git a/src/ft_map_error.c b/src/ft_map_error.c index 263eee8..5d5ab68 100644 --- a/src/ft_map_error.c +++ b/src/ft_map_error.c @@ -15,10 +15,12 @@ #include <unistd.h> int - ft_map_error(t_cub *clist) + ft_map_error(const char *errmsg, t_cub *clist) { ft_dprintf(STDERR_FILENO, "Error\n"); ft_dprintf(STDERR_FILENO, - "\033[1;31mMap error: line %zu\033[0m\n", clist->mlist->line_chk); + "\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 0e1a1e7..4fd870b 100644 --- a/src/ft_parse_map.c +++ b/src/ft_parse_map.c @@ -18,32 +18,6 @@ #include <stdint.h> static void - ft_check_cub(const char *map_path, t_cub *clist) -{ - char **words; - size_t i; - - if (!(words = ft_split(map_path, '.'))) - { - ft_dprintf(STDERR_FILENO, "Error\n"); - ft_dprintf(STDERR_FILENO, "\033[31;1mMap is not a .cub\033[0m\n"); - ft_free_words(words); - ft_exit(6, clist); - } - i = 0; - while (words[i]) - i++; - if (ft_strncmp(words[i - 1], "cub", 3)) - { - ft_dprintf(STDERR_FILENO, "Error\n"); - ft_dprintf(STDERR_FILENO, "\033[31;1mMap is not a .cub\033[0m\n"); - ft_free_words(words); - ft_exit(6, clist); - } - ft_free_words(words); -} - -static void ft_check_map_last_line(t_cub *clist) { t_map *ml; @@ -58,16 +32,16 @@ static void while (ml->map[i - 1][j]) { if (ml->map[i - 1][j] != '1' && ml->map[i - 1][j] != '\0') - ft_map_error(clist); + ft_map_error(FT_ERR_MAP_L_L, clist); j++; } } static int8_t - ft_error_here(char *line, t_cub *clist) + ft_error_here(const char *errmsg, char *line, t_cub *clist) { ft_memdel((void**)&line); - return (ft_map_error(clist)); + return (ft_map_error(errmsg, clist)); } static int8_t @@ -79,9 +53,9 @@ static int8_t clist->mlist->line_chk += 1; if ((ret = get_next_line(fd, &line)) < 0) - return (ft_map_error(clist)); + return (ft_map_error(FT_ERR_READ, clist)); if (ret == 0) - return (ft_error_here(line, clist)); + return (ft_error_here(FT_ERR_UNFINISHED, line, clist)); if (!line[0]) { ft_memdel((void**)&line); @@ -89,33 +63,39 @@ static int8_t } if (!ft_ischarset("RNSEWFC1\0", line[0]) || !(words = ft_split(line, ' '))) - return (ft_error_here(line, clist)); + return (ft_error_here(FT_ERR_ILL_ENTRY, line, clist)); if ((ret = ft_select_get(words, clist)) == 12) return ((ft_get_map_first_line(line, clist) < 0) ? (-1) : (12)); ft_memdel((void**)&line); return (ret); } +static void + ft_no_map_error(t_cub *clist) +{ + ft_dprintf(STDERR_FILENO, "Error\n"); + ft_dprintf(STDERR_FILENO, "\033[31;1mNo map\033[0m\n"); + ft_exit(5, clist); +} + void ft_parse_map(const char *map_path, t_cub *clist) { int fd; int8_t ret; - ft_check_cub(map_path, clist); + if (ft_check_ext(map_path, ".cub") < 0) + ft_map_error(FT_ERR_NOT_A_CUB, clist); fd = open(map_path, O_RDONLY); if (fd < 0) - { - ft_dprintf(STDERR_FILENO, "Error\n"); - ft_dprintf(STDERR_FILENO, "\033[31;1mNo map\033[0m\n"); - ft_exit(5, clist); - } + ft_no_map_error(clist); ret = 1; - while (ret != 12 && ret != -1) + while (ret != 12 && ret >= 0) ret = ft_parse_it(fd, clist); - (ret == -1) ? (ft_map_error(clist)) : 0; + (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); diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index 7b03b84..90f6d88 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -65,7 +65,7 @@ void if (cl->rlist.wall_t < 0) cl->rlist.wall_t = 0; cl->rlist.wall_b = cl->rlist.line_h / 2 + wl->y_size / 2; - if (cl->rlist.wall_b >= wl->y_size) + if (cl->rlist.wall_b >= (float)wl->y_size) cl->rlist.wall_b = wl->y_size - 1; ft_choose_tex(cl); ft_calc_tex(cl); diff --git a/src/ft_select_get.c b/src/ft_select_get.c index f550d98..4b82430 100644 --- a/src/ft_select_get.c +++ b/src/ft_select_get.c @@ -86,7 +86,7 @@ int8_t if (id < 0 || (*fun_ptr[id])(words, clist) < 0) { ft_free_words(words); - return (-1); + return ((id < 0) ? (-2) : (-1)); } ft_free_words(words); return (id); |