fix 1 000 000 problem
This commit is contained in:
parent
c7e4b14f33
commit
edba4a3e76
8
map/map
8
map/map
@ -1,8 +0,0 @@
|
|||||||
11111111
|
|
||||||
10100001
|
|
||||||
10100001
|
|
||||||
10000001
|
|
||||||
10000001
|
|
||||||
11000101
|
|
||||||
11000101
|
|
||||||
11111111
|
|
||||||
11
map/map.cub
Normal file
11
map/map.cub
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
NO ./sprite/brick_wall.ppm
|
||||||
|
SO ./sprite/brick_wall.ppm
|
||||||
|
EA ./sprite/brick_wall.ppm
|
||||||
|
WE ./sprite/brick_wall.ppm
|
||||||
|
|
||||||
|
111111111111111111
|
||||||
|
110000000000000001
|
||||||
|
100000000000000011
|
||||||
|
100000000000000001
|
||||||
|
11000000E000000001
|
||||||
|
111111111111111111
|
||||||
12
map/map8.cub
Normal file
12
map/map8.cub
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
NO ./sprite/brick_wall.ppm
|
||||||
|
SO ./sprite/brick_wall.ppm
|
||||||
|
EA ./sprite/brick_wall.ppm
|
||||||
|
WE ./sprite/brick_wall.ppm
|
||||||
|
|
||||||
|
|
||||||
|
111111111
|
||||||
|
110000001
|
||||||
|
1000000011111
|
||||||
|
10E000001
|
||||||
|
110000001
|
||||||
|
111111111
|
||||||
@ -146,23 +146,25 @@ void draw_ray3d(t_data *img, ray ray)
|
|||||||
// int diff = line_height - 512 \ 2;
|
// int diff = line_height - 512 \ 2;
|
||||||
myy += gap;
|
myy += gap;
|
||||||
my = (int)myy;//gap;
|
my = (int)myy;//gap;
|
||||||
ray.pixel = ((my) * texture_size + mx)* 3 + 1;
|
ray.pixel = ((my) * 64 + mx)* 3 + 1;
|
||||||
x = -1;
|
x = -1;
|
||||||
if (ray.pixel > 12186)
|
if (ray.pixel > 12186)
|
||||||
color = 0;
|
color = 0;
|
||||||
else
|
else
|
||||||
color = get_color(img->map.texture.north[ray.pixel], img->map.texture.north[ray.pixel + 1], img->map.texture.north[ray.pixel + 2]);
|
color = get_color(img->map.texture.north[ray.pixel], img->map.texture.north[ray.pixel + 1], img->map.texture.north[ray.pixel + 2]);
|
||||||
while (++x < 4)
|
while (++x < /*img->map.x / 2*/4)
|
||||||
{
|
{
|
||||||
if (ray.wall_type)
|
if (ray.wall_type)
|
||||||
{
|
{
|
||||||
//mlx_pixel_put(img->mlx, img->mlx_win, ray.index * 4 + x, y + line_offset , color);
|
//mlx_pixel_put(img->mlx, img->mlx_win, ray.index * 4 + x, y + line_offset , color);
|
||||||
set_pixel(img, color, ray.index * 4 + x, y + line_offset);
|
set_pixel(img, color, ray.index * /*(img->map.x / 2)*/4 + x, y + line_offset);
|
||||||
|
//set_pixel(img, color, ray.index * (img->map.x / 2) + x, y + line_offset);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//mlx_pixel_put(img->mlx, img->mlx_win, ray.index * 4 + x, y + line_offset , (color >> 1) & 8355711);
|
//mlx_pixel_put(img->mlx, img->mlx_win, ray.index * 4 + x, y + line_offset , (color >> 1) & 8355711);
|
||||||
set_pixel(img, (color >> 1) & 8355711, ray.index * 4 + x, y + line_offset);
|
set_pixel(img, (color >> 1) & 8355711, ray.index * /*(img->map.x / 2)*/4 + x, y + line_offset);
|
||||||
|
//set_pixel(img, (color >> 1) & 8355711, ray.index * (img->map.x / 2) + x, y + line_offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
y++;
|
y++;
|
||||||
@ -241,18 +243,20 @@ void draw_ray(t_data *img)
|
|||||||
{
|
{
|
||||||
ray_x = img->player.x;
|
ray_x = img->player.x;
|
||||||
ray_y = img->player.y;
|
ray_y = img->player.y;
|
||||||
count = 8;
|
count = img->map.max;
|
||||||
}
|
}
|
||||||
while (count < 8)
|
//printf("max= %d\n", img->map.max);
|
||||||
|
while (count < img->map.max)
|
||||||
{
|
{
|
||||||
//printf("count = %d\n", count);
|
//printf("count = %d\n", count);
|
||||||
mx = (int)(ray_x)>>6;
|
mx = (int)(ray_x)>>6;
|
||||||
my = (int)(ray_y)>>6;
|
my = (int)(ray_y)>>6;
|
||||||
mp = my * img->map.x + mx;
|
mp = my * img->map.x + mx;
|
||||||
//printf("mx=%d my=%d mp= %d\n", mx, my, mp);
|
//printf("mx=%d my=%d mp= %d\n", mx, my, mp);
|
||||||
if (mp > 0 && mp < img->map.x * img->map.y && img->map.simple_map[mp] == '1')//hit wall
|
//printf("map= -%s- c= %c\n",img->map.simple_map, img->map.simple_map[mp]);
|
||||||
|
if (mp > 0 && mp < img->map.size && img->map.simple_map[mp] == '1')//hit wall
|
||||||
{
|
{
|
||||||
count = 8;
|
count = img->map.max;
|
||||||
//printf("vertical wall\n");
|
//printf("vertical wall\n");
|
||||||
dist_v = cos(deg_to_rad(ray_angle)) * (ray_x-img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y-img->player.y);
|
dist_v = cos(deg_to_rad(ray_angle)) * (ray_x-img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y-img->player.y);
|
||||||
}
|
}
|
||||||
@ -287,17 +291,17 @@ void draw_ray(t_data *img)
|
|||||||
{
|
{
|
||||||
ray_x = img->player.x;
|
ray_x = img->player.x;
|
||||||
ray_y = img->player.y;
|
ray_y = img->player.y;
|
||||||
count = 8;
|
count = img->map.max;
|
||||||
}//looking straight left or right
|
}//looking straight left or right
|
||||||
|
|
||||||
while (count < 8)
|
while (count < img->map.max)
|
||||||
{
|
{
|
||||||
mx = (int)(ray_x)>>6;
|
mx = (int)(ray_x)>>6;
|
||||||
my = (int)(ray_y)>>6;
|
my = (int)(ray_y)>>6;
|
||||||
mp = my * img->map.x + mx;
|
mp = my * img->map.x + mx;
|
||||||
if (mp > 0 && mp < img->map.x * img->map.y && img->map.simple_map[mp] == '1')//hit
|
if (mp > 0 && mp < img->map.size && img->map.simple_map[mp] == '1')//hit
|
||||||
{
|
{
|
||||||
count = 8;
|
count = img->map.max;
|
||||||
dist_h = cos(deg_to_rad(ray_angle)) * (ray_x - img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y - img->player.y);
|
dist_h = cos(deg_to_rad(ray_angle)) * (ray_x - img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y - img->player.y);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@ -6,22 +6,57 @@
|
|||||||
/* 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/06 21:31:30 by apommier ### ########.fr */
|
/* Updated: 2022/06/11 17:23:14 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/Cub3D.h"
|
#include "../includes/Cub3D.h"
|
||||||
|
|
||||||
unsigned char *get_texture(int type)
|
void put_texture_in_struct(char type, unsigned char *texture, t_data *img)
|
||||||
|
{
|
||||||
|
if (type == 'N')
|
||||||
|
{
|
||||||
|
if (img->map.texture.north)
|
||||||
|
ft_exit("Error\nMultiple declaration of texture\n");
|
||||||
|
img->map.texture.north = texture;
|
||||||
|
}
|
||||||
|
if (type == 'S')
|
||||||
|
{
|
||||||
|
if (img->map.texture.south)
|
||||||
|
ft_exit("Error\nMultiple declaration of texture\n");
|
||||||
|
img->map.texture.south = texture;
|
||||||
|
}
|
||||||
|
if (type == 'W')
|
||||||
|
{
|
||||||
|
if (img->map.texture.west)
|
||||||
|
ft_exit("Error\nMultiple declaration of texture\n");
|
||||||
|
img->map.texture.west = texture;
|
||||||
|
}
|
||||||
|
if (type == 'E')
|
||||||
|
{
|
||||||
|
if (img->map.texture.east)
|
||||||
|
ft_exit("Error\nMultiple declaration of texture\n");
|
||||||
|
img->map.texture.east = texture;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char *get_texture(char type, char *path, t_data *img)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
unsigned char *ret;
|
unsigned char *ret;
|
||||||
int count;
|
int count;
|
||||||
char *swap = 0;
|
char *swap = 0;
|
||||||
|
|
||||||
|
if (!path)
|
||||||
|
{
|
||||||
|
printf("no path\n");
|
||||||
|
path = ft_strjoin("./sprite/brick_wall.ppm", 0);
|
||||||
|
}
|
||||||
(void)type;
|
(void)type;
|
||||||
count = 0;
|
count = 0;
|
||||||
fd = open("./sprite/brick_wall.ppm", O_RDONLY);
|
fd = open(path, O_RDONLY);
|
||||||
|
if (fd == -1)
|
||||||
|
ft_exit("Error\nBad texture file\n");
|
||||||
while (swap || !count)
|
while (swap || !count)
|
||||||
{
|
{
|
||||||
if (swap)
|
if (swap)
|
||||||
@ -32,7 +67,7 @@ unsigned char *get_texture(int type)
|
|||||||
close(fd);
|
close(fd);
|
||||||
//printf("count= %d\n", count);
|
//printf("count= %d\n", count);
|
||||||
ret = ft_calloc(sizeof(char), count);
|
ret = ft_calloc(sizeof(char), count);
|
||||||
fd = open("./sprite/brick_wall.ppm", O_RDONLY);
|
fd = open(path, O_RDONLY);
|
||||||
if (!ret)
|
if (!ret)
|
||||||
return (0);
|
return (0);
|
||||||
//ret[count] = -1;
|
//ret[count] = -1;
|
||||||
@ -55,5 +90,6 @@ unsigned char *get_texture(int type)
|
|||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
|
put_texture_in_struct(type, ret, img);
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
77
srcs/main.c
77
srcs/main.c
@ -6,13 +6,13 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/02/13 16:42:55 by apommier #+# #+# */
|
/* Created: 2022/02/13 16:42:55 by apommier #+# #+# */
|
||||||
/* Updated: 2022/06/01 18:04:29 by apommier ### ########.fr */
|
/* Updated: 2022/06/11 20:32:22 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/Cub3D.h"
|
#include "../includes/Cub3D.h"
|
||||||
|
|
||||||
void print_ray(t_data *img)
|
/*void print_ray(t_data *img)
|
||||||
{
|
{
|
||||||
int i = -1;
|
int i = -1;
|
||||||
while (++i < 15)
|
while (++i < 15)
|
||||||
@ -21,43 +21,17 @@ void print_ray(t_data *img)
|
|||||||
mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (img->player.vx) * i) + 1, (img->player.y + (img->player.vy) * i) , 255 << 8);
|
mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (img->player.vx) * i) + 1, (img->player.y + (img->player.vy) * i) , 255 << 8);
|
||||||
mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (img->player.vx) * i), (img->player.y + (img->player.vy) * i) + 1, 255 << 8);
|
mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (img->player.vx) * i), (img->player.y + (img->player.vy) * i) + 1, 255 << 8);
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
map_info set_map(char **argv)
|
void set_map(t_data *img)
|
||||||
{
|
{
|
||||||
//char **map_tab;
|
img->map.texture.north = 0;
|
||||||
char *map;
|
img->map.texture.east = 0;
|
||||||
char *del;
|
img->map.texture.south = 0;
|
||||||
char *swap;
|
img->map.texture.west = 0;
|
||||||
int fd;
|
|
||||||
map_info ret_map;
|
|
||||||
|
|
||||||
map = 0;
|
|
||||||
fd = open(argv[1], O_RDONLY);
|
|
||||||
if (fd == -1)
|
|
||||||
ft_error("Error: Open call fail");
|
|
||||||
swap = get_next_line(fd);
|
|
||||||
while (swap)
|
|
||||||
{
|
|
||||||
del = map;
|
|
||||||
if (swap[ft_strlen(swap) - 1] == '\n')
|
|
||||||
swap[ft_strlen(swap) - 1] = 0;
|
|
||||||
map = ft_strjoin(map, swap);
|
|
||||||
free(swap);
|
|
||||||
swap = get_next_line(fd);
|
|
||||||
free(del);
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
ret_map.simple_map = map;
|
|
||||||
//ret_map.floor = ;
|
|
||||||
//ret_map.sky = ;
|
|
||||||
ret_map.x = 8;
|
|
||||||
ret_map.y = 8;
|
|
||||||
ret_map.size = ret_map.x * ret_map.y;
|
|
||||||
return (ret_map);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_case(char type, t_data *img, int y, int x)
|
/*void print_case(char type, t_data *img, int y, int x)
|
||||||
{
|
{
|
||||||
int *buffer = 0;
|
int *buffer = 0;
|
||||||
int img_width;
|
int img_width;
|
||||||
@ -76,7 +50,7 @@ void print_case(char type, t_data *img, int y, int x)
|
|||||||
mlx_destroy_image(img->mlx_test, buffer);
|
mlx_destroy_image(img->mlx_test, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_map(map_info map, t_data *img)
|
/*void print_map(map_info map, t_data *img)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int j;
|
int j;
|
||||||
@ -89,7 +63,7 @@ void print_map(map_info map, t_data *img)
|
|||||||
x = 0;
|
x = 0;
|
||||||
while (++j < 8)
|
while (++j < 8)
|
||||||
{
|
{
|
||||||
print_case(map.simple_map[i], img, (y * 64), (x * 64));
|
print_case(map.[i], img, (y * 64), (x * 64));
|
||||||
i++;
|
i++;
|
||||||
x++;
|
x++;
|
||||||
}
|
}
|
||||||
@ -103,9 +77,9 @@ void print_line(t_data *img, double x, double y)
|
|||||||
int j = -1;
|
int j = -1;
|
||||||
while (++i < x && ++j < y)
|
while (++i < x && ++j < y)
|
||||||
mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (img->player.vx / 5) * i) + 2, (img->player.y + (img->player.vy / 5) * i) + 2, 65);
|
mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (img->player.vx / 5) * i) + 2, (img->player.y + (img->player.vy / 5) * i) + 2, 65);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
void print_player(player player, t_data *img)
|
/*void print_player(player player, t_data *img)
|
||||||
{
|
{
|
||||||
int *buffer = 0;
|
int *buffer = 0;
|
||||||
int img_width;
|
int img_width;
|
||||||
@ -115,14 +89,14 @@ void print_player(player player, t_data *img)
|
|||||||
&img_width, &img_height);
|
&img_width, &img_height);
|
||||||
mlx_put_image_to_window(img->mlx_test, img->mlx_win_test, buffer, player.x - 3.5 , player.y -3.5);
|
mlx_put_image_to_window(img->mlx_test, img->mlx_win_test, buffer, player.x - 3.5 , player.y -3.5);
|
||||||
mlx_destroy_image(img->mlx_test, buffer);
|
mlx_destroy_image(img->mlx_test, buffer);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
t_data img;
|
t_data img;
|
||||||
sprite texture;
|
//sprite texture;
|
||||||
|
|
||||||
texture.north = get_texture(1);
|
//texture.north = get_texture(1, 0, img);
|
||||||
//int index= -1;
|
//int index= -1;
|
||||||
/*while (texture.north[++index] != -1)
|
/*while (texture.north[++index] != -1)
|
||||||
{
|
{
|
||||||
@ -131,24 +105,15 @@ int main(int argc, char **argv)
|
|||||||
printf("end element= %d\n", texture.north[index]);*/
|
printf("end element= %d\n", texture.north[index]);*/
|
||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
ft_error("Error: bad number of arguments, only need a map");
|
ft_error("Error: bad number of arguments, only need a map");
|
||||||
|
set_map(&img);
|
||||||
|
if (check_map(argv, &img))
|
||||||
|
return (0);
|
||||||
img.mlx = mlx_init();
|
img.mlx = mlx_init();
|
||||||
|
|
||||||
if (!img.mlx)
|
if (!img.mlx)
|
||||||
ft_error("Error: mlx_init fail");
|
ft_error("Error: mlx_init fail");
|
||||||
|
|
||||||
img.map = set_map(argv);
|
|
||||||
img.mlx_win = mlx_new_window(img.mlx, 960, 512, "Cub3D");
|
img.mlx_win = mlx_new_window(img.mlx, 960, 512, "Cub3D");
|
||||||
|
img.player.x = 64 * 3 + 16;
|
||||||
|
img.player.y = 64 + 16;
|
||||||
//img.mlx_test = mlx_init();
|
|
||||||
//img.mlx_win_test = mlx_new_window(img.mlx, 512, 512, "Cub3D_test");
|
|
||||||
|
|
||||||
|
|
||||||
//img->player.x=150; img->player.y=400; pa=90;
|
|
||||||
//pdx=cos(deg_to_rad(pa)); pdy=-sin(deg_to_rad(pa));
|
|
||||||
img.map.texture = texture;
|
|
||||||
img.player.x = 150;
|
|
||||||
img.player.y = 400;
|
|
||||||
img.player.angle = 90;
|
img.player.angle = 90;
|
||||||
img.player.vx = cos(deg_to_rad(img.player.angle));
|
img.player.vx = cos(deg_to_rad(img.player.angle));
|
||||||
img.player.vy = sin(deg_to_rad(img.player.angle));
|
img.player.vy = sin(deg_to_rad(img.player.angle));
|
||||||
|
|||||||
141
srcs/parsing/check_color_texture.c
Normal file
141
srcs/parsing/check_color_texture.c
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* check_color_texture.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/06/11 01:00:17 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/06/11 17:33:45 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/Cub3D.h"
|
||||||
|
|
||||||
|
char next_space(char *str, int i)
|
||||||
|
{
|
||||||
|
while (str[i] == ' ')
|
||||||
|
i++;
|
||||||
|
return (str[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int next_space_index(char *str, int i)
|
||||||
|
{
|
||||||
|
while (str[i] == ' ')
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*char next_line_space(char **tab, int i)
|
||||||
|
{
|
||||||
|
while (!next_space(tab[i], 0))
|
||||||
|
i++;
|
||||||
|
return (tab[i]);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
char *transform_map(char **double_map, t_data *img)
|
||||||
|
{
|
||||||
|
char *map;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
int index;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
index = 0;
|
||||||
|
print_double_fd(double_map, 1);
|
||||||
|
img->map.size = img->map.x * img->map.y;
|
||||||
|
printf("x= %d y= %d\n", img->map.x, img->map.y);
|
||||||
|
map = ft_calloc(sizeof(char), img->map.size + 1);
|
||||||
|
while (double_map[++i])
|
||||||
|
{
|
||||||
|
j = -1;
|
||||||
|
while (double_map[i][++j])
|
||||||
|
{
|
||||||
|
//printf("INDEX= %d\n", i * img->map.x + j);
|
||||||
|
map[i * img->map.x + j] = double_map[i][j];
|
||||||
|
map[index] = double_map[i][j];
|
||||||
|
//printf("index= %d\n", index);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
img->map.simple_map = map;
|
||||||
|
printf("map = -%s-\n", map);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_texture_file(char *str, t_data *img, char c)
|
||||||
|
{
|
||||||
|
int index;
|
||||||
|
|
||||||
|
index = next_space_index(str, 0);
|
||||||
|
if (c == 'N')
|
||||||
|
{
|
||||||
|
if (str[index + 1] != 'O' || str[index + 2] != ' ')
|
||||||
|
ft_exit("Error\nBad syntax in map file\n");
|
||||||
|
}
|
||||||
|
if (c == 'S')
|
||||||
|
{
|
||||||
|
if (str[index + 1] != 'O' || str[index + 2] != ' ')
|
||||||
|
ft_exit("Error\nBad syntax in map file\n");
|
||||||
|
}
|
||||||
|
if (c == 'W')
|
||||||
|
{
|
||||||
|
if (str[index + 1] != 'E' || str[index + 2] != ' ')
|
||||||
|
ft_exit("Error\nBad syntax in map file\n");
|
||||||
|
}
|
||||||
|
if (c == 'E')
|
||||||
|
{
|
||||||
|
if (str[index + 1] != 'A' || str[index + 2] != ' ')
|
||||||
|
ft_exit("Error\nBad syntax in map file\n");
|
||||||
|
}
|
||||||
|
printf("path= -%s-\n", str + index + 3);
|
||||||
|
get_texture(c, str + index + 3, img);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_color(char *str, t_data *img)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
int index;
|
||||||
|
|
||||||
|
c = next_space(str, 0);
|
||||||
|
index = next_space_index(str, 0);
|
||||||
|
if (c == 'F')
|
||||||
|
{
|
||||||
|
if (str[index + 1] != ' ')
|
||||||
|
ft_exit("Error\nBad syntax in map file\n");
|
||||||
|
}
|
||||||
|
if (c == 'C')
|
||||||
|
{
|
||||||
|
if (str[index + 1] != ' ')
|
||||||
|
ft_exit("Error\nBad syntax in map file\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_texture_color(char **tab, t_data *img)
|
||||||
|
{
|
||||||
|
int next;
|
||||||
|
int index;
|
||||||
|
char line;
|
||||||
|
|
||||||
|
index = 0;
|
||||||
|
next = 0;
|
||||||
|
int i = 0;
|
||||||
|
while (tab[i])
|
||||||
|
printf("%s\n", tab[i++]);
|
||||||
|
while (next_space(tab[next], 0) != '1' && tab[next])
|
||||||
|
next++;
|
||||||
|
if (!tab[next])
|
||||||
|
ft_exit("Error\nBad syntax in map file\n");
|
||||||
|
while (index < next)
|
||||||
|
{
|
||||||
|
if (next_space(tab[index], 0) == 'N' || next_space(tab[index], 0) == 'S' || next_space(tab[index], 0) == 'W' || next_space(tab[index], 0) == 'E')
|
||||||
|
set_texture_file(tab[index], img, next_space(tab[index], 0));
|
||||||
|
else if (next_space(tab[index], 0) == 'F' || next_space(tab[index], 0) == 'C')
|
||||||
|
set_color(tab[index], img);
|
||||||
|
else if (next_space(tab[index], 0))
|
||||||
|
ft_exit("Error\nBad syntax in map file\n");
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return (next);
|
||||||
|
}
|
||||||
291
srcs/parsing/parse_map.c
Normal file
291
srcs/parsing/parse_map.c
Normal file
@ -0,0 +1,291 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parse_map.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/05/10 17:54:25 by sadjigui #+# #+# */
|
||||||
|
/* Updated: 2022/06/12 01:46:29 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../includes/Cub3D.h"
|
||||||
|
|
||||||
|
void ft_exit(char *str)
|
||||||
|
{
|
||||||
|
ft_putstr_fd(str, 2);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void error_msg(t_data *img)
|
||||||
|
{
|
||||||
|
if (img->map.error == 1)
|
||||||
|
ft_putstr_fd("Error: Map isn't closed\n", 2);
|
||||||
|
if (img->map.error == 2)
|
||||||
|
ft_putstr_fd("Error: Missing player\n", 2);
|
||||||
|
if (img->map.error == 3)
|
||||||
|
ft_putstr_fd("Error: Too many players\n", 2);
|
||||||
|
if (img->map.error == -1)
|
||||||
|
ft_putstr_fd("Error: Bad character in map\n", 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_tab(char **tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (tab[i])
|
||||||
|
{
|
||||||
|
free(tab[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(tab);
|
||||||
|
tab = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void size_line(char *str, t_data *img)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
|
||||||
|
while (str[i])
|
||||||
|
i++;
|
||||||
|
if (i > img->map.x)
|
||||||
|
img->map.x = i;
|
||||||
|
printf("sizelie= %s, size= %d\n", str, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *charge_new(t_data *img)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
str = malloc(sizeof(char) * (img->map.x + 3));
|
||||||
|
if (!str)
|
||||||
|
return (NULL);
|
||||||
|
while (i < img->map.x + 2)
|
||||||
|
{
|
||||||
|
str[i] = '3';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
str[i] = '\0';
|
||||||
|
return (str);
|
||||||
|
}
|
||||||
|
|
||||||
|
int reverse_comp(char *s1, char *s2)
|
||||||
|
{
|
||||||
|
int size_s1;
|
||||||
|
int size_s2;
|
||||||
|
|
||||||
|
size_s1 = ft_strlen(s1);
|
||||||
|
size_s2 = ft_strlen(s2);
|
||||||
|
while (size_s2 >= 0)
|
||||||
|
{
|
||||||
|
if (!(s2[size_s2] == s1[size_s1]))
|
||||||
|
return (1);
|
||||||
|
size_s1--;
|
||||||
|
size_s2--;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void inter_map(char **split, char **tmp)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (split[i])
|
||||||
|
{
|
||||||
|
j = 0;
|
||||||
|
while(split[i][j])
|
||||||
|
{
|
||||||
|
if (ft_isalnum(split[i][j]))
|
||||||
|
tmp[i + 1][j + 1] = split[i][j];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void close_or_not(char **tab, int i, int j, t_data *img)
|
||||||
|
{
|
||||||
|
if(tab[i + 1][j] == '3' || tab[i - 1][j] == '3')
|
||||||
|
img->map.error = 1;
|
||||||
|
if(tab[i][j + 1] == '3' || tab[i][j - 1] == '3')
|
||||||
|
img->map.error = 1;
|
||||||
|
if(tab[i + 1][j + 1] == '3' || tab[i + 1][j - 1] == '3')
|
||||||
|
img->map.error = 1;
|
||||||
|
if(tab[i - 1][j + 1] == '3' || tab[i - 1][j - 1] == '3')
|
||||||
|
img->map.error = 1;
|
||||||
|
if (img->map.error != 1)
|
||||||
|
img->map.error = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_inner_utils(char *line)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int player;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
player = 0;
|
||||||
|
while (line[i])
|
||||||
|
{
|
||||||
|
// if (line[i] == '0' || line[i] == '1' || line[i] == '3' || line[i] == '\n')
|
||||||
|
// i++;
|
||||||
|
if (line[i] == 'N' || line[i] == 'S' || line[i] == 'E' || line[i] == 'W')
|
||||||
|
player++;
|
||||||
|
else if (line[i] != '3' && line[i] != '0' && line[i] != '1')
|
||||||
|
return (100);
|
||||||
|
// else
|
||||||
|
// return (2);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (player);
|
||||||
|
}
|
||||||
|
|
||||||
|
void check_inner(char **map, t_data *img)//fonction bizarre
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int player;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
player = 0;
|
||||||
|
while (map[i])
|
||||||
|
{
|
||||||
|
player += check_inner_utils(map[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (player == 0)
|
||||||
|
img->map.error = 2;
|
||||||
|
if (player > 1 && player < 100)//?????? pk 3 et -1?
|
||||||
|
img->map.error = 3;
|
||||||
|
if (player >= 100)
|
||||||
|
img->map.error = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void check_border(char **tab, t_data *img)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (tab[i])
|
||||||
|
{
|
||||||
|
j = 0;
|
||||||
|
while (tab[i][j])
|
||||||
|
{
|
||||||
|
if (tab[i][j] == '0')
|
||||||
|
close_or_not(tab, i, j, img);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void check_zero_one(char **split, t_data *img)
|
||||||
|
{
|
||||||
|
char **tmp;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (split[i])
|
||||||
|
{
|
||||||
|
size_line(split[i], img);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
img->map.y = i;
|
||||||
|
tmp = malloc(sizeof(char *) * (i + 3));
|
||||||
|
if (!tmp)
|
||||||
|
return ;
|
||||||
|
i = 0;
|
||||||
|
while(i < img->map.y + 2)
|
||||||
|
{
|
||||||
|
tmp[i] = charge_new(img);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
tmp[i] = NULL;
|
||||||
|
inter_map(split, tmp);
|
||||||
|
check_border(tmp, img);
|
||||||
|
check_inner(tmp, img);
|
||||||
|
free_tab(tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **isafile(char **av, t_data *img)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char *line;
|
||||||
|
char *str;
|
||||||
|
char *tmp;
|
||||||
|
char **split;
|
||||||
|
|
||||||
|
fd = open(av[1], O_RDONLY);
|
||||||
|
str = NULL;
|
||||||
|
tmp = NULL;
|
||||||
|
if (fd == - 1)
|
||||||
|
ft_exit("Error: File doesn't exist\n");
|
||||||
|
while ((line = get_next_line(fd)) != NULL)
|
||||||
|
{
|
||||||
|
tmp = ft_strjoin(str, line);
|
||||||
|
if (str != NULL)
|
||||||
|
free(str);
|
||||||
|
str = tmp;
|
||||||
|
//size_line(line, img);
|
||||||
|
free(line);
|
||||||
|
line = NULL;
|
||||||
|
}
|
||||||
|
split = ft_split(str, '\n');
|
||||||
|
free(line);
|
||||||
|
free(str);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
int pass = 0;
|
||||||
|
pass = check_texture_color(split, img);
|
||||||
|
check_zero_one(split + pass, img);
|
||||||
|
//leaks here
|
||||||
|
transform_map(split + pass, img);
|
||||||
|
free_double(split);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_map(char **av, t_data *img)
|
||||||
|
{
|
||||||
|
char **map;
|
||||||
|
|
||||||
|
img->map.x = 0;
|
||||||
|
img->map.y = 0;
|
||||||
|
img->map.error = 0;
|
||||||
|
|
||||||
|
map = NULL;
|
||||||
|
if (reverse_comp(av[1], ".cub") || (ft_strlen(av[1]) == ft_strlen(".cub")))
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Error: Not a valid file \".cub\"\n", 2);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
map = isafile(av, img);
|
||||||
|
|
||||||
|
if (img->map.x > img->map.y)
|
||||||
|
img->map.max = img->map.x;
|
||||||
|
else
|
||||||
|
img->map.max = img->map.y;
|
||||||
|
|
||||||
|
if (img->map.error != 0)
|
||||||
|
{
|
||||||
|
error_msg(img);
|
||||||
|
exit(1);//surely leak
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*int main(int ac, char **av)
|
||||||
|
{
|
||||||
|
t_root img;
|
||||||
|
|
||||||
|
if (check_map(av, &img))
|
||||||
|
return (0);
|
||||||
|
else
|
||||||
|
printf("map is clean\n");
|
||||||
|
}*/
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* 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/06 21:38:31 by apommier ### ########.fr */
|
/* Updated: 2022/06/11 20:39:44 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user