fix empty line in map

This commit is contained in:
kinou-p 2022-06-15 22:10:56 +02:00
parent 1d4dcf811b
commit 5da1a4b1c9
6 changed files with 133 additions and 125 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:30:59 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_texture_color(char **tab, t_data *img);
int check_map(char **av, t_data *img); int check_map(char **av, t_data *img);
void ft_exit(char *str, t_data *img); void ft_exit(char *str, t_data *img);
void set_back(t_data *img); void set_back(t_data *img);
unsigned char *get_texture(char type, char *path, t_data *img); unsigned char *get_texture(char type, char *path, t_data *img);
int get_color(char one, char two, char three); 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 reset_angle(double angle);
double deg_to_rad(double angle); double deg_to_rad(double angle);
void draw_ray(t_data *img); void draw_ray(t_data *img);

View File

@ -12,43 +12,6 @@
#include "../../includes/Cub3D.h" #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) void draw_ray3d(t_data *img, t_ray ray)
{ {
double line_height; double line_height;
@ -61,14 +24,9 @@ void draw_ray3d(t_data *img, t_ray ray)
double gap; double gap;
double myy; double myy;
x = 0;
y = 0;
mx = 0;
my = 0;
line_height = 64 * 960 / ray.dist; line_height = 64 * 960 / ray.dist;
line_offset = 256 - ((int)line_height) / 2; line_offset = 256 - ((int)line_height) / 2;
gap = 1; x = 0;
myy = 0;
y = 0; y = 0;
my = 0; my = 0;
myy = 0; myy = 0;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/06 16:10:11 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); close(fd);
img->to_be_free.fd = -1; img->to_be_free.fd = -1;
if (*count != 12291) 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) void fill_ret(int count, t_data *img, unsigned char **ret, char *swap)

View File

@ -6,46 +6,68 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 23:37:02 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" #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) int pixel;
angle -= 360.0;
if (angle < 0) if (y < 0 || y > 520 || x < 0 || x > 960)
angle += 360.0; return ;
return (angle); 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); char *swap;
exit(1);
}
void ft_exit(char *str, t_data *img) swap = 0;
{ if (!path)
ft_putstr_fd(str, 2); ft_exit("Error\nNo path for map\n", img);
quit_game(img); check_dir(path, img);
} fd = open(path, O_RDONLY);
if (fd == -1)
void check_dir(char *path, t_data *img) ft_exit("Error\nBad path for map\n", img);
{ img->to_be_free.fd = fd;
int fd; while (swap || !(*count))
{
fd = open(path, O_DIRECTORY); if (swap)
if (fd >= 0) free(swap);
{ (*count)++;
close(fd); swap = get_next_line(fd);
ft_exit("Error\nPath is a directory and not a file\n", img); }
} close(fd);
img->to_be_free.fd = -1;
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/10 17:54:25 by sadjigui #+# #+# */ /* 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; i = 0;
while (split[i]) while (split[i])
{ {
if (!ft_strchr(split[i], '1'))
ft_exit("Error\nInvalid line in map file\n", img);
size_line(split[i], img); size_line(split[i], img);
i++; i++;
} }
@ -271,58 +273,41 @@ int detect_map_line(char *line)
return (1); 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) char **isafile(char **av, t_data *img)
{ {
int fd; int fd;
char *line; char **ret;
char *str; int count;
char *tmp; int pass;
char **split;
check_dir(av[1], img); count = 0;
img->err = 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); fd = open(av[1], O_RDONLY);
img->to_be_free.fd = fd; if (fd == -1)
str = NULL; ft_exit("Error\nBad texture file", img);
tmp = NULL; ret = fill_tab(0, 0, ret, fd);
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);
close(fd); close(fd);
img->to_be_free.fd = -1; pass = check_texture_color(ret, img);
int pass = 0; check_zero_one(ret + pass, img);
pass = check_texture_color(split, img); transform_map(ret + pass, img);
check_zero_one(split + pass, img); free_double(ret);
//leaks here -- normalement c bon
transform_map(split + pass, img);
free_double(split);
img->to_be_free.tab = 0; img->to_be_free.tab = 0;
return (0); return (0);
} }

View File

@ -6,8 +6,46 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/15 18:48:45 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" #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);
}
}