From 2812949b09094ff812600ebeff089af8b9a7bec9 Mon Sep 17 00:00:00 2001 From: kinou-p Date: Wed, 15 Jun 2022 19:16:11 +0200 Subject: [PATCH] norm get_texture --- srcs/cast_ray/get_texture_array.c | 67 +++++++++++++++++-------------- srcs/cast_ray/set_back.c | 4 +- srcs/cast_ray/utils.c | 5 +-- srcs/key_event/key.c | 4 +- srcs/utils/quit_game.c | 4 +- 5 files changed, 44 insertions(+), 40 deletions(-) diff --git a/srcs/cast_ray/get_texture_array.c b/srcs/cast_ray/get_texture_array.c index c2f4045..3801d7e 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 18:41:04 by apommier ### ########.fr */ +/* Updated: 2022/06/15 19:15:40 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,63 +54,48 @@ int is_nbr(char *str) return (1); } -unsigned char *get_texture(char type, char *path, t_data *img) +void get_file_size(char *path, t_data *img, int *count, int fd) { - int fd; - unsigned char *ret; - int count; - char *swap; + char *swap; swap = 0; - (void)type; - count = 0; if (!path) - return (0); - fd = open(path, O_DIRECTORY); - if (fd >= 0) - { - close(fd); - ft_exit("Error\nTexture path is a directory\n", img); - } + ft_exit("Error\nNo path for texture\n", img); + check_dir(path, img); fd = open(path, O_RDONLY); if (fd == -1) - quit_game(img); + ft_exit("Error\nBad path for texture\n", img); img->to_be_free.fd = fd; - while (swap || !count) + while (swap || !(*count)) { if (swap) free(swap); - count++; + (*count)++; swap = get_next_line(fd); } close(fd); img->to_be_free.fd = -1; - if (count != 12291) + if (*count != 12291) ft_exit("Error\nBad texture file (too much line)\n", img); - ret = ft_calloc(sizeof(char), count); - if (!ret) - quit_game(img); - img->to_be_free.str = ret; - fd = open(path, O_RDONLY); - if (fd == -1) - ft_exit("Error\nBad texture file", img); - img->to_be_free.fd = fd; - count = 0; +} + +void fill_ret(int count, t_data *img, unsigned char **ret, char *swap) +{ while (swap || !count) { if (swap) free(swap); - swap = get_next_line(fd); + swap = get_next_line(img->to_be_free.fd); if (!count) { free(swap); - swap = get_next_line(fd); + swap = get_next_line(img->to_be_free.fd); } if (swap) swap[ft_strlen(swap) - 1] = 0; if ((swap && is_nbr(swap) && ft_strlen(swap) <= 3 && ft_atoi(swap) <= 255 && ft_atoi(swap) >= 0) || !count) - ret[count] = (unsigned char)ft_atoi(swap); + (*ret)[count] = (unsigned char)ft_atoi(swap); else if (swap) { get_next_line(-1); @@ -119,6 +104,26 @@ unsigned char *get_texture(char type, char *path, t_data *img) } count++; } +} + +unsigned char *get_texture(char type, char *path, t_data *img) +{ + int fd; + unsigned char *ret; + int count; + + (void)type; + count = 0; + get_file_size(path, img, &count, 0); + ret = ft_calloc(sizeof(char), count); + if (!ret) + quit_game(img); + img->to_be_free.str = ret; + fd = open(path, O_RDONLY); + if (fd == -1) + ft_exit("Error\nBad texture file", img); + img->to_be_free.fd = fd; + fill_ret(0, img, &ret, 0); close(fd); img->to_be_free.fd = -1; put_texture_in_struct(type, ret, img); diff --git a/srcs/cast_ray/set_back.c b/srcs/cast_ray/set_back.c index 8d953e1..2ab8acc 100644 --- a/srcs/cast_ray/set_back.c +++ b/srcs/cast_ray/set_back.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/15 18:46:05 by apommier #+# #+# */ -/* Updated: 2022/06/15 18:46:55 by apommier ### ########.fr */ +/* Updated: 2022/06/15 18:55:47 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,4 +38,4 @@ void set_back(t_data *img) } x += 4; } -} \ No newline at end of file +} diff --git a/srcs/cast_ray/utils.c b/srcs/cast_ray/utils.c index a7188bb..fb10862 100644 --- a/srcs/cast_ray/utils.c +++ b/srcs/cast_ray/utils.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/13 23:37:02 by apommier #+# #+# */ -/* Updated: 2022/06/15 18:52:18 by apommier ### ########.fr */ +/* Updated: 2022/06/15 19:15:23 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,7 +46,6 @@ void check_dir(char *path, t_data *img) if (fd >= 0) { close(fd); - ft_exit("Error\nTexture path is a directory\n", img); + ft_exit("Error\nPath is a directory and not a file\n", img); } - close(fd); } diff --git a/srcs/key_event/key.c b/srcs/key_event/key.c index 1b67ae1..e36645e 100644 --- a/srcs/key_event/key.c +++ b/srcs/key_event/key.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/15 18:40:33 by apommier #+# #+# */ -/* Updated: 2022/06/15 18:45:35 by apommier ### ########.fr */ +/* Updated: 2022/06/15 18:54:06 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -105,4 +105,4 @@ int update_pos(t_data *img) update_angle_side(img); update_side(img); return (0); -} \ No newline at end of file +} diff --git a/srcs/utils/quit_game.c b/srcs/utils/quit_game.c index a09ebf4..7762465 100644 --- a/srcs/utils/quit_game.c +++ b/srcs/utils/quit_game.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/15 18:48:35 by apommier #+# #+# */ -/* Updated: 2022/06/15 18:49:09 by apommier ### ########.fr */ +/* Updated: 2022/06/15 18:54:20 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,4 +45,4 @@ int quit_game(t_data *img) if (img->to_be_free.fd != -1) close(img->to_be_free.fd); exit(1); -} \ No newline at end of file +}