From 5da1a4b1c9a1ee99bf1f58a9ac8163cd6558d69f Mon Sep 17 00:00:00 2001 From: kinou-p Date: Wed, 15 Jun 2022 22:10:56 +0200 Subject: [PATCH] fix empty line in map --- includes/Cub3D.h | 7 ++- srcs/cast_ray/cast_ray.c | 44 +---------------- srcs/cast_ray/get_texture_array.c | 4 +- srcs/cast_ray/utils.c | 80 +++++++++++++++++++----------- srcs/parsing/parse_map.c | 81 +++++++++++++------------------ srcs/utils/cub_utils.c | 42 +++++++++++++++- 6 files changed, 133 insertions(+), 125 deletions(-) diff --git a/includes/Cub3D.h b/includes/Cub3D.h index 361f382..780c206 100644 --- a/includes/Cub3D.h +++ b/includes/Cub3D.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/13 16:30:59 by apommier #+# #+# */ -/* Updated: 2022/06/15 18:42:10 by apommier ### ########.fr */ +/* Updated: 2022/06/15 21:39:57 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -113,9 +113,14 @@ char *transform_map(char **double_map, t_data *img); int check_texture_color(char **tab, t_data *img); int check_map(char **av, t_data *img); void ft_exit(char *str, t_data *img); + void set_back(t_data *img); unsigned char *get_texture(char type, char *path, t_data *img); int get_color(char one, char two, char three); +void set_pixel(t_data *img, int color, int x, int y); +void get_file_size(char *path, t_data *img, int *count, int fd); +void get_map_size(char *path, t_data *img, int *count, int fd); + double reset_angle(double angle); double deg_to_rad(double angle); void draw_ray(t_data *img); diff --git a/srcs/cast_ray/cast_ray.c b/srcs/cast_ray/cast_ray.c index 85309d3..153254f 100644 --- a/srcs/cast_ray/cast_ray.c +++ b/srcs/cast_ray/cast_ray.c @@ -12,43 +12,6 @@ #include "../../includes/Cub3D.h" -int get_color(char R, char G, char B) -{ - int color; - - color = 0; - color = color << 8; - color += R; - color = color << 8; - color += G; - color = color << 8; - color += B; - return (color); -} - -void set_pixel(t_data *img, int color, int x, int y) -{ - int pixel; - - if (y < 0 || y > 520 || x < 0 || x > 960) - return ; - pixel = (y * img->size_line) + (x * 4); - if (img->endian == 1) - { - img->buffer[pixel + 0] = 0; - img->buffer[pixel + 1] = (color >> 16) & 0xFF; - img->buffer[pixel + 2] = (color >> 8) & 0xFF; - img->buffer[pixel + 3] = (color) & 0xFF; - } - else if (img->endian == 0) - { - img->buffer[pixel + 0] = (color) & 0xFF; - img->buffer[pixel + 1] = (color >> 8) & 0xFF; - img->buffer[pixel + 2] = (color >> 16) & 0xFF; - img->buffer[pixel + 3] = 0; - } -} - void draw_ray3d(t_data *img, t_ray ray) { double line_height; @@ -61,14 +24,9 @@ void draw_ray3d(t_data *img, t_ray ray) double gap; double myy; - x = 0; - y = 0; - mx = 0; - my = 0; line_height = 64 * 960 / ray.dist; line_offset = 256 - ((int)line_height) / 2; - gap = 1; - myy = 0; + x = 0; y = 0; my = 0; myy = 0; diff --git a/srcs/cast_ray/get_texture_array.c b/srcs/cast_ray/get_texture_array.c index 3801d7e..fd42db0 100644 --- a/srcs/cast_ray/get_texture_array.c +++ b/srcs/cast_ray/get_texture_array.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/06 16:10:11 by apommier #+# #+# */ -/* Updated: 2022/06/15 19:15:40 by apommier ### ########.fr */ +/* Updated: 2022/06/15 21:38:53 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,7 +76,7 @@ void get_file_size(char *path, t_data *img, int *count, int fd) close(fd); img->to_be_free.fd = -1; if (*count != 12291) - ft_exit("Error\nBad texture file (too much line)\n", img); + ft_exit("Error\nBad texture file (too much or not enough line)\n", img); } void fill_ret(int count, t_data *img, unsigned char **ret, char *swap) diff --git a/srcs/cast_ray/utils.c b/srcs/cast_ray/utils.c index fb10862..1414ced 100644 --- a/srcs/cast_ray/utils.c +++ b/srcs/cast_ray/utils.c @@ -6,46 +6,68 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/13 23:37:02 by apommier #+# #+# */ -/* Updated: 2022/06/15 19:15:23 by apommier ### ########.fr */ +/* Updated: 2022/06/15 21:39:46 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../includes/Cub3D.h" -double deg_to_rad(double angle) +int get_color(char R, char G, char B) { - return (angle * 3.14159265358979323846 / 180.0); + int color; + + color = 0; + color = color << 8; + color += R; + color = color << 8; + color += G; + color = color << 8; + color += B; + return (color); } -double reset_angle(double angle) +void set_pixel(t_data *img, int color, int x, int y) { - if (angle > 359) - angle -= 360.0; - if (angle < 0) - angle += 360.0; - return (angle); + int pixel; + + if (y < 0 || y > 520 || x < 0 || x > 960) + return ; + pixel = (y * img->size_line) + (x * 4); + if (img->endian == 1) + { + img->buffer[pixel + 0] = 0; + img->buffer[pixel + 1] = (color >> 16) & 0xFF; + img->buffer[pixel + 2] = (color >> 8) & 0xFF; + img->buffer[pixel + 3] = (color) & 0xFF; + } + else if (img->endian == 0) + { + img->buffer[pixel + 0] = (color) & 0xFF; + img->buffer[pixel + 1] = (color >> 8) & 0xFF; + img->buffer[pixel + 2] = (color >> 16) & 0xFF; + img->buffer[pixel + 3] = 0; + } } -void ft_error(char *error_msg) +void get_map_size(char *path, t_data *img, int *count, int fd) { - ft_putendl_fd(error_msg, 2); - exit(1); -} + char *swap; -void ft_exit(char *str, t_data *img) -{ - ft_putstr_fd(str, 2); - quit_game(img); -} - -void check_dir(char *path, t_data *img) -{ - int fd; - - fd = open(path, O_DIRECTORY); - if (fd >= 0) - { - close(fd); - ft_exit("Error\nPath is a directory and not a file\n", img); - } + swap = 0; + if (!path) + ft_exit("Error\nNo path for map\n", img); + check_dir(path, img); + fd = open(path, O_RDONLY); + if (fd == -1) + ft_exit("Error\nBad path for map\n", img); + img->to_be_free.fd = fd; + while (swap || !(*count)) + { + if (swap) + free(swap); + (*count)++; + swap = get_next_line(fd); + } + close(fd); + img->to_be_free.fd = -1; } diff --git a/srcs/parsing/parse_map.c b/srcs/parsing/parse_map.c index 62b1296..945aa22 100644 --- a/srcs/parsing/parse_map.c +++ b/srcs/parsing/parse_map.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/10 17:54:25 by sadjigui #+# #+# */ -/* Updated: 2022/06/15 18:52:15 by apommier ### ########.fr */ +/* Updated: 2022/06/15 22:03:10 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -214,6 +214,8 @@ void check_zero_one(char **split, t_data *img) i = 0; while (split[i]) { + if (!ft_strchr(split[i], '1')) + ft_exit("Error\nInvalid line in map file\n", img); size_line(split[i], img); i++; } @@ -271,58 +273,41 @@ int detect_map_line(char *line) return (1); } +char **fill_tab(char *swap, int count, char **ret, int fd) +{ + while (swap || !count) + { + swap = get_next_line(fd); + if (ft_strlen(swap) >= 1 && swap[ft_strlen(swap) - 1] == '\n') + swap[ft_strlen(swap) - 1] = 0; + ret[count] = swap; + count++; + } + return (ret); +} + char **isafile(char **av, t_data *img) { - int fd; - char *line; - char *str; - char *tmp; - char **split; - - check_dir(av[1], img); - img->err = 0; + char **ret; + int count; + int pass; + + count = 0; + get_map_size(av[1], img, &count, 0); + ret = ft_calloc(sizeof(char*), count); + if (!ret) + quit_game(img); + img->to_be_free.tab = ret; fd = open(av[1], O_RDONLY); - img->to_be_free.fd = fd; - str = NULL; - tmp = NULL; - if (fd == - 1) - ft_exit("Error\nFile doesn't exist\n", img); - while ((line = get_next_line(fd)) != NULL) - { - if (line[0] != '\n' && img->err == 0) - { - if (detect_map_line(line)) - img->err = 1; - if (line[0] == '\n' && img->err == 1) - img->err = 2; - } - tmp = ft_strjoin(str, line); - free(line); - line = NULL; - if (str != NULL) - free(str); - if (!tmp) - quit_game(img); - str = tmp; - } - if (img->err == 2) - { - free(str); - ft_exit("Error\nBad texture file\n", img); - } - split = ft_split(str, '\n'); - img->to_be_free.tab = split; - free(str); - free(line); + if (fd == -1) + ft_exit("Error\nBad texture file", img); + ret = fill_tab(0, 0, ret, fd); close(fd); - img->to_be_free.fd = -1; - int pass = 0; - pass = check_texture_color(split, img); - check_zero_one(split + pass, img); - //leaks here -- normalement c bon - transform_map(split + pass, img); - free_double(split); + pass = check_texture_color(ret, img); + check_zero_one(ret + pass, img); + transform_map(ret + pass, img); + free_double(ret); img->to_be_free.tab = 0; return (0); } diff --git a/srcs/utils/cub_utils.c b/srcs/utils/cub_utils.c index 82b4b76..a1c6984 100644 --- a/srcs/utils/cub_utils.c +++ b/srcs/utils/cub_utils.c @@ -6,8 +6,46 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/15 18:48:45 by apommier #+# #+# */ -/* Updated: 2022/06/15 18:48:47 by apommier ### ########.fr */ +/* Updated: 2022/06/15 19:23:03 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../../includes/Cub3D.h" \ No newline at end of file +#include "../../includes/Cub3D.h" + +double deg_to_rad(double angle) +{ + return (angle * 3.14159265358979323846 / 180.0); +} + +double reset_angle(double angle) +{ + if (angle > 359) + angle -= 360.0; + if (angle < 0) + angle += 360.0; + return (angle); +} + +void ft_error(char *error_msg) +{ + ft_putendl_fd(error_msg, 2); + exit(1); +} + +void ft_exit(char *str, t_data *img) +{ + ft_putstr_fd(str, 2); + quit_game(img); +} + +void check_dir(char *path, t_data *img) +{ + int fd; + + fd = open(path, O_DIRECTORY); + if (fd >= 0) + { + close(fd); + ft_exit("Error\nPath is a directory and not a file\n", img); + } +} \ No newline at end of file