From e731d9d1babaf0a17d6414d00d9db3aba2fc7cef Mon Sep 17 00:00:00 2001 From: kinou-p Date: Thu, 19 May 2022 19:34:30 +0200 Subject: [PATCH] full screen --- includes/Cub3D.h | 26 ++++----- srcs/cast_ray.c | 113 +++++++++++++++++++++------------------ srcs/get_texture_array.c | 40 +------------- srcs/main.c | 49 ++++++++--------- srcs/utils.c | 42 +++++++-------- 5 files changed, 123 insertions(+), 147 deletions(-) diff --git a/includes/Cub3D.h b/includes/Cub3D.h index 1d338ce..b0a205d 100644 --- a/includes/Cub3D.h +++ b/includes/Cub3D.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/13 16:30:59 by apommier #+# #+# */ -/* Updated: 2022/05/17 22:51:01 by apommier ### ########.fr */ +/* Updated: 2022/05/19 14:19:57 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,10 +26,10 @@ # define PI 3.1415926535 typedef struct ray_info{ - float ty; - float tx; - float mp; - float dist; + double ty; + double tx; + double mp; + double dist; int index; int wall_type; int pixel; @@ -57,16 +57,18 @@ typedef struct map_information{ typedef struct player_position { - float x; - float y; - float angle; - float vx; - float vy; + double x; + double y; + double angle; + double vx; + double vy; } player; typedef struct s_data { void *mlx; void *mlx_win; + void *mlx_test; + void *mlx_win_test; char **double_map; map_info map; player player; @@ -74,8 +76,8 @@ typedef struct s_data { int *get_texture(int type); int get_color(char one, char two, char three); -int reset_angle(int angle); -float deg_to_rad(int angle); +double reset_angle(double angle); +double deg_to_rad(double angle); void draw_ray(t_data *img); void print_ray(t_data *img); int key_press(int code, t_data *img); diff --git a/srcs/cast_ray.c b/srcs/cast_ray.c index 9462d35..da5a4f1 100644 --- a/srcs/cast_ray.c +++ b/srcs/cast_ray.c @@ -6,26 +6,27 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/04 18:08:14 by apommier #+# #+# */ -/* Updated: 2022/05/18 19:13:39 by apommier ### ########.fr */ +/* Updated: 2022/05/19 19:04:22 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/Cub3D.h" -void print_ray2(t_data *img, float vx, float vy, float dist) +void print_ray2(t_data *img, double vx, double vy, double dist) { int i = -1; int red = 0; + //printf("in print ray2\n"); red = red << 8; red +=255; red = red << 8; red = red << 8; while (++i < dist) { - mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (vx) * i) , (img->player.y + (vy) * i) , red); - mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (vx) * i) + 1, (img->player.y + (vy) * i) , red); - mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (vx) * i) , (img->player.y + (vy) * i) + 1, red); + mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (vx) * i) , (img->player.y + (vy) * i) , red); + mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (vx) * i) + 1, (img->player.y + (vy) * i) , red); + mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (vx) * i) , (img->player.y + (vy) * i) + 1, red); } } @@ -66,13 +67,13 @@ int get_dark_red() void draw_ray3d(t_data *img, ray ray) { - float line_height; - float line_offset; + double line_height; + double line_offset; int x = 0; - float y = 0; + double y = 0; int mx = 0; int my = 0; - int pixel; + //int pixel; int color; int texture_size = 64; //char *wall; @@ -89,8 +90,8 @@ void draw_ray3d(t_data *img, ray ray) }*/ - // float ty=ty_off*ty_step;//+hmt*32; - // float tx; + // double ty=ty_off*ty_step;//+hmt*32; + // double tx; // if(shade==1) // { // tx=(int)(rx/2.0)%32; @@ -106,17 +107,17 @@ void draw_ray3d(t_data *img, ray ray) //pixel = ((int)ty * 32 + (int)tx) * 3 + (hmt * 32 * 32 * 3); - int copy = ray.ty; + //int copy = ray.ty; //i = 0; - line_height = img->map.size * 320 / ray.dist; - if (line_height > 320) - line_height = 320; - line_offset = 160 - line_height / 2; - float gap = 1; - float old_y = 0; - float myy = 0; - printf("mp= %f modulo texture_size= %d\n", ray.mp, ((int)ray.mp / 4) % texture_size); + line_height = img->map.size * 960 / ray.dist; + //if (line_height > 512) + // line_height = 512; + line_offset = 256 - line_height / 2; + double gap = 1; + //double old_y = 0; + double myy = 0; + //printf("mp= %f modulo texture_size= %d\n", ray.mp, ((int)ray.mp / 4) % texture_size); //while (x < 8) //{ @@ -125,7 +126,7 @@ void draw_ray3d(t_data *img, ray ray) myy = 0; //double step = 1.0 * texHeight / lineHeight; gap = (texture_size / line_height); - old_y = 0; + //old_y = 0; //ray.ty = ; mx = ((int)ray.mp) % texture_size; //int texX = int(wallX * double(texWidth)); @@ -148,18 +149,18 @@ void draw_ray3d(t_data *img, ray ray) //ray.pixel = ((((int)ray.mp) % 16) * 16 + gap /*+ x*/) * 3 + 1 ; ray.pixel = ((my) * texture_size + mx)* 3 - 1; x = -1; - //printf("my= %d mx= %d pixel= %d\n", my, mx, ray.pixel); + //printf("x= %d y= %f pixel= %d\n", ray.index /** 2 + 530 + x*/, y, ray.pixel); 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 < 8) + while (++x < 4) { if (ray.wall_type) - mlx_pixel_put(img->mlx, img->mlx_win, ray.index * 8 + 530 + x, y + line_offset , color); + mlx_pixel_put(img->mlx, img->mlx_win, ray.index * 4 + x, y + line_offset , color); else - mlx_pixel_put(img->mlx, img->mlx_win, ray.index * 8 + 530 + 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); } //printf("pixel=%d ", ray.pixel); //if (ray.wall_type) - int l = 0; + //int l = 0; //while (l < 8) //{ //x = -1; @@ -173,32 +174,32 @@ void draw_ray3d(t_data *img, ray ray) } x++; //} - printf("\n"); + //printf("\n"); //ray.tx++; } void draw_ray(t_data *img) { - float ray_angle = 0; //ray angle - float ray_y = 0; //where ray touch x - float ray_x = 0; //where ray touch y - float next_x = 0; - float next_y = 0; - float dist_v; - float dist_h; - float dist_f; - float vx = 0; - float vy = 0; + double ray_angle = 0; //ray angle + double ray_y = 0; //where ray touch x + double ray_x = 0; //where ray touch y + double next_x = 0; + double next_y = 0; + double dist_v; + double dist_h; + double dist_f; + double vx = 0; + double vy = 0; int count = 0; - float aTan = 0; + double aTan = 0; int nb_ray = -1; int my = 0; int mx = 0; int mp = 0; (void)dist_f; - printf("---NEW RAY----\n\n"); + //printf("---NEW RAY----\n\n"); //printf("\nENTER DRAW RAY\n"); //while (++k < ft_strlen(img->map.simple_map)) // printf("%d--- %c\n", k, img->map.simple_map[k]); @@ -206,7 +207,7 @@ void draw_ray(t_data *img) count = 0; ray_angle = reset_angle(img->player.angle + 30); //ray_angle = reset_angle(img->player.angle); - while (++nb_ray < 60) + while (++nb_ray < 240) { //if (nb_ray) @@ -218,6 +219,12 @@ void draw_ray(t_data *img) //printf("player_angle= %f ray_angle= %f\n", img->player.angle, ray_angle); //----------start vertical ray---------- aTan = tan(deg_to_rad(ray_angle)); + //if (aTan != tan(deg_to_rad(ray_angle - 0.25))) + //double test = 0.1111111111111; + //printf("test= %f\n", test); + //printf("ray_angle = %f\n", ray_angle); + //printf("in atan= %f\n", deg_to_rad(ray_angle)); + //printf("atan= %f\n", aTan); if (cos(deg_to_rad(ray_angle)) > 0.001)//looking left { ray_x = (((int)img->player.x>>6)<<6) + 64; @@ -322,29 +329,29 @@ void draw_ray(t_data *img) if (dist_h != -1 && (dist_h < dist_v || dist_v == -1)) { - print_ray2(img, cos(deg_to_rad(ray_angle)), -sin(deg_to_rad(ray_angle)), fabs(dist_h)); + //print_ray2(img, cos(deg_to_rad(ray_angle)), -sin(deg_to_rad(ray_angle)), fabs(dist_h)); dist_f = dist_h; - printf("rx= %f ry= %f\n", ray_x, ray_y); + //printf("rx= %f ry= %f\n", ray_x, ray_y); ray_info.mp = ray_x; wall_type = 0; } else if (dist_v != -1) { dist_f = dist_v; - printf("vx= %f vy= %f\n", vx, vy); + //printf("vx= %f vy= %f\n", vx, vy); ray_x = vx; ray_y = vy; ray_info.mp = vy; - print_ray2(img, cos(deg_to_rad(ray_angle)), -sin(deg_to_rad(ray_angle)), fabs(dist_v)); + //print_ray2(img, cos(deg_to_rad(ray_angle)), -sin(deg_to_rad(ray_angle)), fabs(dist_v)); wall_type = 1; } else dist_f = 0; - float tx; - float ty_off = 0; - float ty_step = 32.0/dist_f; - //float ty = ; + double tx; + //double ty_off = 0; + //double ty_step = 32.0/dist_f; + //double ty = ; /*if(dist_f > 640) { ty_off = (dist_f - 320) / 2.0; @@ -356,17 +363,19 @@ void draw_ray(t_data *img) //if(ray_angle > 180) // tx = 15 - tx; //ray_info.pixel = ((int)ty * 16 + (int)tx) * 3 + 3; - printf("nb_ray= %d\n", nb_ray); + //printf("nb_ray= %d\n", nb_ray); ray_info.ty = ray_y; ray_info.tx = ray_x; - + //ray_info.mp = ray_info.mp / 2; ray_info.index = nb_ray; ray_info.wall_type = wall_type; int ca = reset_angle(img->player.angle - ray_angle); //fisheye dist_f = dist_f * cos(deg_to_rad(ca)); ray_info.dist = dist_f; //fisheye draw_ray3d(img, ray_info); - - ray_angle = reset_angle(ray_angle - 1); + //ray_angle -= 0.5; + //printf("entre ray_angle = %f\n", ray_angle); + ray_angle = reset_angle(ray_angle - 0.25); + //printf("after ray_angle = %f\n", ray_angle); } } \ No newline at end of file diff --git a/srcs/get_texture_array.c b/srcs/get_texture_array.c index 21216c2..62ad4a4 100644 --- a/srcs/get_texture_array.c +++ b/srcs/get_texture_array.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/06 16:10:11 by apommier #+# #+# */ -/* Updated: 2022/05/17 23:49:38 by apommier ### ########.fr */ +/* Updated: 2022/05/19 19:11:07 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,10 @@ int *get_texture(int type) { int fd; int *ret; - //int start = 1; int count; - int len; char *swap = 0; - //line = 0; + (void)type; count = 0; fd = open("./sprite/brick_wall.ppm", O_RDONLY); while (swap || !count) @@ -33,54 +31,20 @@ int *get_texture(int type) } close(fd); ret = ft_calloc(sizeof(int), count + 1); - //char **ret2 = ft_calloc(sizeof(ret), count + 1); fd = open("./sprite/brick_wall.ppm", O_RDONLY); if (!ret) return (0); ret[count] = -1; - len = count; count = 0; while (swap || !count) { if (swap) free(swap); swap = get_next_line(fd); - //printf("swap= %s\n", swap); ret[count] = (int)ft_atoi(swap); count++; } - int k = 0; - /*while (len) - { - printf("nbr= %d", ret[len--]); - }*/ - printf("nrb3= %d\n", ret[1]); - //print_double_fd(ret2, 1); - //free_double(ret2); close(fd); - - - /*int v = 0; - int b = 0; - int pixel = 0; - //printf("pixel=%d ", ray.pixel); - while (v < 16) - { - while (b < 16) - { - pixel = (v * 3) + b; - int color = get_color(ret[pixel], ret[pixel + 1], ret[pixel + 2]); - mlx_pixel_put(img->mlx, img->mlx_win, v, b, color); - b++; - } - v++; - }*/ - - - - - - return (ret); } \ No newline at end of file diff --git a/srcs/main.c b/srcs/main.c index c90a0fe..2c8698a 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/13 16:42:55 by apommier #+# #+# */ -/* Updated: 2022/05/17 23:52:27 by apommier ### ########.fr */ +/* Updated: 2022/05/19 19:09:32 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,9 @@ void print_ray(t_data *img) int i = -1; while (++i < 15) { - mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (img->player.vx) * i), (img->player.y + (img->player.vy) * i) , 255 << 8); - mlx_pixel_put(img->mlx, img->mlx_win, (img->player.x + (img->player.vx) * i) + 1, (img->player.y + (img->player.vy) * i) , 255 << 8); - mlx_pixel_put(img->mlx, img->mlx_win, (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) , 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); } } @@ -117,15 +117,15 @@ void print_case(char type, t_data *img, int y, int x) //printf("type= %c\n", type); if (type == '1') - buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/wall.xpm", + buffer = mlx_xpm_file_to_image(img->mlx_test, "./sprite/wall.xpm", &img_width, &img_height); else if (type == '0') - buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/back.xpm", + buffer = mlx_xpm_file_to_image(img->mlx_test, "./sprite/back.xpm", &img_width, &img_height); if (!buffer) ft_error("no buffer"); - mlx_put_image_to_window(img->mlx, img->mlx_win, buffer, x, y); - mlx_destroy_image(img->mlx, buffer); + mlx_put_image_to_window(img->mlx_test, img->mlx_win_test, buffer, x, y); + mlx_destroy_image(img->mlx_test, buffer); } void print_map(map_info map, t_data *img) @@ -159,19 +159,19 @@ void print_map(map_info map, t_data *img) { print_case(map[j][i], img, (j * 64), (i * 64)); i++; - x++; + x++; } j++; y++; }*/ } -void print_line(t_data *img, float x, float y) +void print_line(t_data *img, double x, double y) { - float i = -1; + double i = -1; int j = -1; while (++i < x && ++j < y) - mlx_pixel_put(img->mlx, img->mlx_win, (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) @@ -180,10 +180,10 @@ void print_player(player player, t_data *img) int img_width; int img_height; - buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/player.xpm", + buffer = mlx_xpm_file_to_image(img->mlx_test, "./sprite/player.xpm", &img_width, &img_height); - mlx_put_image_to_window(img->mlx, img->mlx_win, buffer, player.x - 3.5 , player.y -3.5); - mlx_destroy_image(img->mlx, buffer); + 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) @@ -195,11 +195,18 @@ int main(int argc, char **argv) if (argc != 2) ft_error("Error: bad number of arguments, only need a map"); 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, 1024, 512, "Cub3D"); + 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; @@ -208,14 +215,8 @@ int main(int argc, char **argv) img.player.angle = 90; img.player.vx = cos(deg_to_rad(img.player.angle)); img.player.vy = sin(deg_to_rad(img.player.angle)); - print_map(img.map, &img); - print_player(img.player, &img); - - - - - - + //print_map(img.map, &img); + //print_player(img.player, &img); //print_ray(&img); mlx_hook(img.mlx_win, 2, 1L << 0, &key_press, &img); mlx_hook(img.mlx_win, 17, 0L, &quit_game, &img); diff --git a/srcs/utils.c b/srcs/utils.c index ab771ad..91a6d23 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/05/18 00:08:25 by apommier ### ########.fr */ +/* Updated: 2022/05/19 15:32:21 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,17 +23,17 @@ int quit_game(t_data *img) exit(0); } -float deg_to_rad(int angle) +double deg_to_rad(double angle) { - return (angle * M_PI / 180.0); + return (angle * 3.14159265358979323846 / 180.0); } -int reset_angle(int angle) +double reset_angle(double angle) { if (angle > 359) - angle -= 360; + angle -= 360.0; if (angle < 0) - angle += 360; + angle += 360.0; return (angle); } @@ -49,8 +49,8 @@ void ft_error(char *error_msg) int is_good(t_data *img, int type) { //printf("touche= %c\n", type); - //printf("vx= %f vy= %f\n", img->player.vx, img->player.vy); - //printf("player: x= %f y= %f\n", img->player.x, img->player.y); + //printf("vx= %d vy= %d\n", img->player.vx, img->player.vy); + //printf("player: x= %d y= %d\n", img->player.x, img->player.y); // /*if(key=='a'){ pa+=5; pa=FixAng(pa); pdx=cos(deg_to_rad(pa)); pdy=-sin(deg_to_rad(pa));} @@ -94,19 +94,19 @@ int is_good(t_data *img, int type) } else return (0); - //printf("after player: x= %f y= %f\n", img->player.x, img->player.y); + //printf("after player: x= %d y= %d\n", img->player.x, img->player.y); return (1); } void print_back(t_data *img) { - int x = 530; - int y = 160; + int x = 0; + int y = 0; while (x < 1024) { y = 0; - while (y < 320) + while (y < 512) { mlx_pixel_put(img->mlx, img->mlx_win, x , y, get_color(128, 128, 128)); //mlx_pixel_put(img->mlx, img->mlx_win, x , y - 160, get_color(0, 255, 255)); @@ -126,22 +126,22 @@ int key_press(int code, t_data *img) { //printf("code = %d\n", code); //mlx_clear_window(img->mlx, img->mlx_win); - print_map(img->map, img); - print_player(img->player, img); + //print_map(img->map, img); + //print_player(img->player, img); print_back(img); draw_ray(img); - print_ray(img); + //print_ray(img); } } - int y = 0; - int x = 0; - int pixel = 0; - int color; + // int y = 0; + // int x = 0; + // int pixel = 0; + // int color; //printf("pixel=%d ", ray.pixel); - while (x < 16) + /*while (x < 16) { y = 0; while (y < 16) @@ -153,7 +153,7 @@ int key_press(int code, t_data *img) y++; } x++; - } + }*/