norm get_texture

This commit is contained in:
kinou-p 2022-06-15 19:16:11 +02:00
parent e530c856e8
commit 2812949b09
5 changed files with 44 additions and 40 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
}