diff --git a/map/map b/map/map deleted file mode 100644 index 993aa6d..0000000 --- a/map/map +++ /dev/null @@ -1,8 +0,0 @@ -11111111 -10100001 -10100001 -10000001 -10000001 -11000101 -11000101 -11111111 diff --git a/map/map.cub b/map/map.cub new file mode 100644 index 0000000..52eb969 --- /dev/null +++ b/map/map.cub @@ -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 diff --git a/map/map8.cub b/map/map8.cub new file mode 100644 index 0000000..e02168b --- /dev/null +++ b/map/map8.cub @@ -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 \ No newline at end of file diff --git a/srcs/cast_ray.c b/srcs/cast_ray.c index 916f697..771a4ec 100644 --- a/srcs/cast_ray.c +++ b/srcs/cast_ray.c @@ -146,23 +146,25 @@ void draw_ray3d(t_data *img, ray ray) // int diff = line_height - 512 \ 2; myy += gap; my = (int)myy;//gap; - ray.pixel = ((my) * texture_size + mx)* 3 + 1; + ray.pixel = ((my) * 64 + mx)* 3 + 1; x = -1; if (ray.pixel > 12186) color = 0; else 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) { //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 { //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++; @@ -241,18 +243,20 @@ void draw_ray(t_data *img) { ray_x = img->player.x; 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); mx = (int)(ray_x)>>6; my = (int)(ray_y)>>6; mp = my * img->map.x + mx; - //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("mx=%d my=%d mp= %d\n", mx, my, mp); + //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"); 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_y = img->player.y; - count = 8; + count = img->map.max; }//looking straight left or right - while (count < 8) + while (count < img->map.max) { mx = (int)(ray_x)>>6; my = (int)(ray_y)>>6; 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); } else diff --git a/srcs/get_texture_array.c b/srcs/get_texture_array.c index 251f9e7..7d51616 100644 --- a/srcs/get_texture_array.c +++ b/srcs/get_texture_array.c @@ -6,22 +6,57 @@ /* 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" -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; unsigned char *ret; int count; char *swap = 0; + if (!path) + { + printf("no path\n"); + path = ft_strjoin("./sprite/brick_wall.ppm", 0); + } (void)type; 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) { if (swap) @@ -32,7 +67,7 @@ unsigned char *get_texture(int type) close(fd); //printf("count= %d\n", count); ret = ft_calloc(sizeof(char), count); - fd = open("./sprite/brick_wall.ppm", O_RDONLY); + fd = open(path, O_RDONLY); if (!ret) return (0); //ret[count] = -1; @@ -55,5 +90,6 @@ unsigned char *get_texture(int type) count++; } close(fd); + put_texture_in_struct(type, ret, img); return (ret); } \ No newline at end of file diff --git a/srcs/main.c b/srcs/main.c index a31264a..5ab5f3f 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,13 +6,13 @@ /* 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" -void print_ray(t_data *img) +/*void print_ray(t_data *img) { int i = -1; 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), (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; - char *map; - char *del; - char *swap; - 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); + img->map.texture.north = 0; + img->map.texture.east = 0; + img->map.texture.south = 0; + img->map.texture.west = 0; } -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 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); } -void print_map(map_info map, t_data *img) +/*void print_map(map_info map, t_data *img) { int i = 0; int j; @@ -89,7 +63,7 @@ void print_map(map_info map, t_data *img) x = 0; while (++j < 8) { - print_case(map.simple_map[i], img, (y * 64), (x * 64)); + print_case(map.[i], img, (y * 64), (x * 64)); i++; x++; } @@ -103,9 +77,9 @@ void print_line(t_data *img, double x, double y) int j = -1; 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); -} +}*/ -void print_player(player player, t_data *img) +/*void print_player(player player, t_data *img) { int *buffer = 0; int img_width; @@ -115,14 +89,14 @@ void print_player(player player, t_data *img) &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_destroy_image(img->mlx_test, buffer); -} +}*/ int main(int argc, char **argv) { t_data img; - sprite texture; + //sprite texture; - texture.north = get_texture(1); + //texture.north = get_texture(1, 0, img); //int 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]);*/ if (argc != 2) 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(); - if (!img.mlx) ft_error("Error: mlx_init fail"); - - img.map = set_map(argv); img.mlx_win = mlx_new_window(img.mlx, 960, 512, "Cub3D"); - - - //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.x = 64 * 3 + 16; + img.player.y = 64 + 16; img.player.angle = 90; img.player.vx = cos(deg_to_rad(img.player.angle)); img.player.vy = sin(deg_to_rad(img.player.angle)); diff --git a/srcs/parsing/check_color_texture.c b/srcs/parsing/check_color_texture.c new file mode 100644 index 0000000..d9201c6 --- /dev/null +++ b/srcs/parsing/check_color_texture.c @@ -0,0 +1,141 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_color_texture.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} \ No newline at end of file diff --git a/srcs/parsing/parse_map.c b/srcs/parsing/parse_map.c new file mode 100644 index 0000000..3cccd3f --- /dev/null +++ b/srcs/parsing/parse_map.c @@ -0,0 +1,291 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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"); +}*/ diff --git a/srcs/utils.c b/srcs/utils.c index b2d4a79..7246c25 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */