From e9b0dd7ec94c66aca9583f2b55a142673c59cabd Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 23 Apr 2022 11:58:11 +0200 Subject: [PATCH 1/7] possibility to choose angle of view --- headers/cube3d_macro.h | 20 ++++++++++++++++---- headers/cube3d_struct.h | 3 +++ srcs/cube3d.c | 2 +- srcs/draw/draw.c | 16 +++++++++------- srcs/draw/ray_intersect.c | 30 +++++++++++++++--------------- srcs/draw/raycast.c | 2 +- srcs/hook/keyhook.c | 16 ++++++++-------- srcs/init/init_struct.c | 27 +++++++++++++++++---------- srcs/player/player_limits.c | 4 ++-- 9 files changed, 72 insertions(+), 48 deletions(-) diff --git a/headers/cube3d_macro.h b/headers/cube3d_macro.h index 9efe213..698546d 100644 --- a/headers/cube3d_macro.h +++ b/headers/cube3d_macro.h @@ -1,13 +1,25 @@ #ifndef CUBE3D_MACRO_H # define CUBE3D_MACRO_H +/* + * game params + */ + +/* nbr pixel player move */ +# define PLR_MV 1 +/* nbr key you can press at the same time */ # define MAX_NB_KEY 3 -# define SCREEN_DIST 50 -# define SCREEN_SIZE 100 -# define SCREEN_DEF 50 -# define PLR_MV 5 +/* screen definition */ +# define SCREEN_DEF 100 +/* screen focal (in degree) */ +# define SCREEN_FOCAL 90 +/* size of a cell on the map */ # define CELL 20 +/* + * keys macro + */ + # define ARROW_LEFT 65361 # define ARROW_UP 65362 # define ARROW_RIGHT 65363 diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index a0b8e49..54e6a25 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -35,6 +35,8 @@ typedef struct s_rcast t_vec screen_dist; t_vec screen_size; t_vec ray; + int screen_def; + int cell; int cell_x; int cell_y; int slope_x; @@ -84,6 +86,7 @@ typedef struct s_map char **content; int size_x; int size_y; + int cell; } t_map; /* diff --git a/srcs/cube3d.c b/srcs/cube3d.c index f343b41..42937ad 100644 --- a/srcs/cube3d.c +++ b/srcs/cube3d.c @@ -53,7 +53,7 @@ int main(int ac, char **av) // infinite loop that waits for events to occurs */ -// mlx_loop_hook(game->mlx_ptr, hook_action, game); + mlx_loop_hook(game->mlx_ptr, hook_action, game); mlx_loop(game->mlx_ptr); return (0); } diff --git a/srcs/draw/draw.c b/srcs/draw/draw.c index 4d0a486..a506f44 100644 --- a/srcs/draw/draw.c +++ b/srcs/draw/draw.c @@ -75,7 +75,9 @@ void draw_line(t_game *game, t_vec vec, int color) { t_coord incr; t_coord pos; + int cell; + cell = game->map.cell; incr.x = 0; pos.x = 0; pos.y = 0; @@ -85,19 +87,19 @@ void draw_line(t_game *game, t_vec vec, int color) while ((game->map.content)[incr.x][incr.y]) { if ((game->map.content)[incr.x][incr.y] == '1' ) - draw_square(game, pos, 0x00999999, 0x00000000, CELL, 0); + draw_square(game, pos, 0x00999999, 0x00000000, cell, 0); else - draw_square(game, pos, 0x00555555, 0x00333333, CELL, 0); + draw_square(game, pos, 0x00555555, 0x00333333, cell, 0); (incr.y)++; - pos.x += CELL; + pos.x += cell; } (incr.x)++; pos.x = 0; - pos.y += CELL; + pos.y += cell; } - pos.x = game->plr.pos.x - CELL / 2; - pos.y = game->plr.pos.y - CELL / 2; - draw_square(game, pos, 0x00999900, 0x00330033, CELL, 1); + pos.x = game->plr.pos.x - cell / 2; + pos.y = game->plr.pos.y - cell / 2; + draw_square(game, pos, 0x00999900, 0x00330033, cell, 1); } static void draw_screen(t_game *game, t_rcast *rcast) diff --git a/srcs/draw/ray_intersect.c b/srcs/draw/ray_intersect.c index 6eeaa9a..ed7d409 100644 --- a/srcs/draw/ray_intersect.c +++ b/srcs/draw/ray_intersect.c @@ -12,8 +12,8 @@ static void init_raycast(t_rcast *rcast, t_vec *ray) if (ray->start.y < ray->end.y) rcast->ray_sign_y = -1; // cell position of ray - rcast->cell_x = ray->start.x / CELL; - rcast->cell_y = ray->start.y / CELL; + rcast->cell_x = ray->start.x / rcast->cell; + rcast->cell_y = ray->start.y / rcast->cell; // slope of ray rcast->slope_x = ray->end.x - ray->start.x; rcast->slope_y = ray->end.y - ray->start.y; @@ -21,23 +21,23 @@ static void init_raycast(t_rcast *rcast, t_vec *ray) rcast->next_cell_x = -rcast->ray_sign_x; rcast->next_cell_y = -rcast->ray_sign_y; // ray steps in both axis - rcast->ray_step_x = ft_abs(CELL * rcast->slope_y); - rcast->ray_step_y = ft_abs(CELL * rcast->slope_x); + rcast->ray_step_x = ft_abs(rcast->cell * rcast->slope_y); + rcast->ray_step_y = ft_abs(rcast->cell * rcast->slope_x); } static void init_first_step(t_rcast *rcast, t_vec *ray) { // first next time ray cross grid - rcast->first_next_x = ray->start.x % CELL; + rcast->first_next_x = ray->start.x % rcast->cell; if (rcast->first_next_x && rcast->ray_sign_x < 0) - rcast->first_next_x = CELL - rcast->first_next_x; + rcast->first_next_x = rcast->cell - rcast->first_next_x; if (!rcast->first_next_x && rcast->ray_sign_x < 0) - rcast->first_next_x = CELL; - rcast->first_next_y = ray->start.y % CELL; + rcast->first_next_x = rcast->cell; + rcast->first_next_y = ray->start.y % rcast->cell; if (rcast->first_next_y && rcast->ray_sign_y < 0) - rcast->first_next_y = CELL - rcast->first_next_y; + rcast->first_next_y = rcast->cell - rcast->first_next_y; if (!rcast->first_next_y && rcast->ray_sign_y < 0) - rcast->first_next_y = CELL; + rcast->first_next_y = rcast->cell; rcast->next_x = ft_abs(rcast->first_next_x * rcast->slope_y); rcast->next_y = ft_abs(rcast->first_next_y * rcast->slope_x); } @@ -62,9 +62,9 @@ static void calcul_ray_end(t_rcast *rcast, t_vec *ray) { if (rcast->is_x) { - ray->end.x = rcast->cell_x * CELL; + ray->end.x = rcast->cell_x * rcast->cell; if (rcast->ray_sign_x == 1) - ray->end.x += CELL; + ray->end.x += rcast->cell; if (rcast->slope_x) { rcast->ratio = (double)(ray->end.x - ray->start.x) / (double)rcast->slope_x; @@ -73,9 +73,9 @@ static void calcul_ray_end(t_rcast *rcast, t_vec *ray) } else { - ray->end.y = rcast->cell_y * CELL; + ray->end.y = rcast->cell_y * rcast->cell; if (rcast->ray_sign_y == 1) - ray->end.y += CELL; + ray->end.y += rcast->cell; if (rcast->slope_y) { rcast->ratio = (double)(ray->end.y - ray->start.y) / (double)rcast->slope_y; @@ -89,7 +89,7 @@ void ray_intersect(t_game *game, t_rcast *rcast, t_vec *ray) ray->start.x = rcast->ray.start.x + game->plr.pos.x; ray->start.y = rcast->ray.start.y + game->plr.pos.y; ray->end.x = rcast->ray.end.x + game->plr.pos.x; - ray->end.x += rcast->ray_nb * SCREEN_SIZE / SCREEN_DEF; + ray->end.x += rcast->ray_nb; ray->end.y = rcast->ray.end.y + game->plr.pos.y; rotate(&(game->plr), &(ray->end)); init_raycast(rcast, ray); diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 99d05e4..4abadf5 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -5,7 +5,7 @@ void raycast(t_game *game, t_rcast *rcast) t_vec ray; rcast->ray_nb = 0; - while (rcast->ray_nb <= SCREEN_DEF) + while (rcast->ray_nb <= rcast->screen_def) { ray_intersect(game, rcast, &ray); diff --git a/srcs/hook/keyhook.c b/srcs/hook/keyhook.c index f45e6d0..8641fd2 100644 --- a/srcs/hook/keyhook.c +++ b/srcs/hook/keyhook.c @@ -1,12 +1,12 @@ #include "cube3d.h" // temp, to map all the keys on linux and mac -static int print_keycode(int keycode) -{ - ft_putnbr_fd(keycode, 1); - ft_putchar_fd('\n', 1); - return(0); -} + static int print_keycode(int keycode) + { + ft_putnbr_fd(keycode, 1); + ft_putchar_fd('\n', 1); + return(0); + } // temp end int hook_action(t_game *game) @@ -38,7 +38,7 @@ int keypress(int keycode, t_game *game) unsigned i; // temp - print_keycode(keycode); + print_keycode(keycode); // temp end i = 0; @@ -48,7 +48,7 @@ int keypress(int keycode, t_game *game) game->k_hook[i] = 0; else if (i < MAX_NB_KEY) game->k_hook[i] = keycode; - hook_action(game); +// hook_action(game); return (0); } diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index b5d0307..cc07c9a 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -12,6 +12,7 @@ static void init_map(t_map *map) { map->size_x = 24; map->size_y = 24; + map->cell = CELL; // map int tmp[24][24] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, @@ -64,21 +65,26 @@ static void init_map(t_map *map) static void init_raycast(t_rcast *rcast) { - // draw screen dist + double dist; + + dist = (SCREEN_FOCAL / 2) * M_PI / 180; + dist = cos(dist) * ((SCREEN_DEF / 2) / sin(dist)); + rcast->screen_def = SCREEN_DEF; + // screen dist rcast->screen_dist.start.x = 0; rcast->screen_dist.start.y = 0; rcast->screen_dist.end.x = 0; - rcast->screen_dist.end.y = -SCREEN_DIST; - // draw screen size - rcast->screen_size.start.x = -SCREEN_SIZE / 2; - rcast->screen_size.start.y = -SCREEN_DIST; - rcast->screen_size.end.x = SCREEN_SIZE / 2; - rcast->screen_size.end.y = -SCREEN_DIST; - // draw ray + rcast->screen_dist.end.y = -dist; + // screen size + rcast->screen_size.start.x = -SCREEN_DEF / 2; + rcast->screen_size.start.y = -dist; + rcast->screen_size.end.x = SCREEN_DEF / 2; + rcast->screen_size.end.y = -dist; + // first ray rcast->ray.start.x = 0; rcast->ray.start.y = 0; - rcast->ray.end.x = -SCREEN_SIZE / 2; - rcast->ray.end.y = -SCREEN_DIST; + rcast->ray.end.x = -SCREEN_DEF / 2; + rcast->ray.end.y = -dist; } static void init_plr(t_plr *plr) @@ -118,6 +124,7 @@ t_game *init_game(void) ft_bzero(&game->k_hook, sizeof(game->k_hook)); // raycasting init_raycast(&(game->rcast)); + game->rcast.cell = game->map.cell; // create image and get its data address init_img(&(game->map_img), game); return (game); diff --git a/srcs/player/player_limits.c b/srcs/player/player_limits.c index 7d9481a..9890d6d 100644 --- a/srcs/player/player_limits.c +++ b/srcs/player/player_limits.c @@ -20,8 +20,8 @@ int plr_out_limits(t_game *game, int x, int y) xmax = game->map_img.sizel / (game->map_img.bpp / 8); ymax = game->map_win.size_y; - cell_x = x / CELL; - cell_y = y / CELL; + cell_x = x / game->map.cell; + cell_y = y / game->map.cell; if (is_wall(game, cell_x, cell_y)) return (1); if (x < 0 || y < 0 || x > xmax || y > ymax) From 9359e87f6fbdf628161cd520ab7bfeac6b2ce0dd Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sat, 23 Apr 2022 12:55:10 +0200 Subject: [PATCH 2/7] temp test other way of writting ray end calcul --- srcs/draw/ray_intersect.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/srcs/draw/ray_intersect.c b/srcs/draw/ray_intersect.c index ed7d409..3896445 100644 --- a/srcs/draw/ray_intersect.c +++ b/srcs/draw/ray_intersect.c @@ -60,6 +60,7 @@ static void next_cell(t_rcast *rcast) static void calcul_ray_end(t_rcast *rcast, t_vec *ray) { + /* if (rcast->is_x) { ray->end.x = rcast->cell_x * rcast->cell; @@ -82,6 +83,43 @@ static void calcul_ray_end(t_rcast *rcast, t_vec *ray) ray->end.x = ray->start.x + (double)rcast->slope_x * rcast->ratio; } } + */ + int *nd1; + int *cell1; + int *sign1; + int *st1; + int *slp1; + int *nd2; + int *st2; + int *slp2; + + if (rcast->is_x) + { + nd1 = &(ray->end.x); + cell1 = &(rcast->cell_x); + sign1 = &(rcast->ray_sign_x); + st1 = &(ray->start.x); + slp1 = &(rcast->slope_x); + nd2 = &(ray->end.y); + st2 = &(ray->start.y); + slp2 = &(rcast->slope_y); + } + else + { + nd1 = &(ray->end.y); + cell1 = &(rcast->cell_y); + sign1 = &(rcast->ray_sign_y); + st1 = &(ray->start.y); + slp1 = &(rcast->slope_y); + nd2 = &(ray->end.x); + st2 = &(ray->start.x); + slp2 = &(rcast->slope_x); + } + *nd1 = *cell1 * rcast->cell; + if (*sign1 == 1) + *nd1 += rcast->cell; + if (*slp1) + *nd2 = *st2 + (double)(*slp2) * (double)(*nd1 - *st1) / (double)(*slp1); } void ray_intersect(t_game *game, t_rcast *rcast, t_vec *ray) From bb3a9db930d803df9bb5bd7e5281e80ff31d52e1 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 24 Apr 2022 11:12:39 +0200 Subject: [PATCH 3/7] add place for game window --- headers/cube3d_macro.h | 6 ++-- headers/cube3d_struct.h | 10 +++--- srcs/cube3d.c | 17 ++-------- srcs/draw/draw.c | 2 +- srcs/draw/ray_intersect.c | 68 --------------------------------------- srcs/draw/raycast.c | 40 ++++++++++++++++++++++- srcs/init/init_struct.c | 35 ++++++++++++-------- 7 files changed, 74 insertions(+), 104 deletions(-) diff --git a/headers/cube3d_macro.h b/headers/cube3d_macro.h index 698546d..1f4d472 100644 --- a/headers/cube3d_macro.h +++ b/headers/cube3d_macro.h @@ -9,8 +9,10 @@ # define PLR_MV 1 /* nbr key you can press at the same time */ # define MAX_NB_KEY 3 -/* screen definition */ -# define SCREEN_DEF 100 +/* screen width */ +# define SCREEN_WIDTH 500 +/* screen height */ +# define SCREEN_HEIGHT 200 /* screen focal (in degree) */ # define SCREEN_FOCAL 90 /* size of a cell on the map */ diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index 54e6a25..3ebeffb 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -113,18 +113,18 @@ typedef struct s_plr typedef struct s_game { void *mlx_ptr; - // map window - t_win map_win; - t_img map_img; // game window -// t_win win; -// t_img img; + t_win win; + t_img img; // player t_plr plr; // rays t_rcast rcast; // map t_map map; + // map window + t_win map_win; + t_img map_img; // key hook int k_hook[MAX_NB_KEY]; } t_game; diff --git a/srcs/cube3d.c b/srcs/cube3d.c index 42937ad..4890f0d 100644 --- a/srcs/cube3d.c +++ b/srcs/cube3d.c @@ -11,8 +11,9 @@ void destroy_mlx(void *param) mlx_destroy_window(game->mlx_ptr, game->map_win.ptr); // tmp end -// mlx_destroy_image(game->mlx_ptr, game->img.ptr); -// mlx_destroy_window(game->mlx_ptr, game->win.ptr); + mlx_destroy_image(game->mlx_ptr, game->img.ptr); + mlx_destroy_window(game->mlx_ptr, game->win.ptr); + mlx_destroy_display(game->mlx_ptr); } @@ -33,17 +34,6 @@ int main(int ac, char **av) // draw game a first time before it start draw(game); - // tmp, to draw map - // receive a keypress event - mlx_hook(game->map_win.ptr, 2, 1L << 0, keypress, game); - // receive a keyprelease event - mlx_hook(game->map_win.ptr, 3, 1L << 1, keyrelease, game); - // receive event when clicking the red button to close the window - mlx_hook(game->map_win.ptr, 17, 1L << 17, shut_down, NULL); - // infinite loop that waits for events to occurs - // tmp end - -/* // receive a keypress event mlx_hook(game->win.ptr, 2, 1L << 0, keypress, game); // receive a keyprelease event @@ -51,7 +41,6 @@ int main(int ac, char **av) // receive event when clicking the red button to close the window mlx_hook(game->win.ptr, 17, 1L << 17, shut_down, NULL); // infinite loop that waits for events to occurs -*/ mlx_loop_hook(game->mlx_ptr, hook_action, game); mlx_loop(game->mlx_ptr); diff --git a/srcs/draw/draw.c b/srcs/draw/draw.c index a506f44..62f4e72 100644 --- a/srcs/draw/draw.c +++ b/srcs/draw/draw.c @@ -134,7 +134,7 @@ void draw(t_game *game) // tmp, to draw map draw_screen(game, &(game->rcast)); - mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, 0); + mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, SCREEN_HEIGHT); // tmp end // mlx_put_image_to_window(game->mlx_ptr, game->win.ptr, game->img.ptr, 0, 0); diff --git a/srcs/draw/ray_intersect.c b/srcs/draw/ray_intersect.c index 3896445..decb5a5 100644 --- a/srcs/draw/ray_intersect.c +++ b/srcs/draw/ray_intersect.c @@ -27,7 +27,6 @@ static void init_raycast(t_rcast *rcast, t_vec *ray) static void init_first_step(t_rcast *rcast, t_vec *ray) { - // first next time ray cross grid rcast->first_next_x = ray->start.x % rcast->cell; if (rcast->first_next_x && rcast->ray_sign_x < 0) rcast->first_next_x = rcast->cell - rcast->first_next_x; @@ -58,70 +57,6 @@ static void next_cell(t_rcast *rcast) } } -static void calcul_ray_end(t_rcast *rcast, t_vec *ray) -{ - /* - if (rcast->is_x) - { - ray->end.x = rcast->cell_x * rcast->cell; - if (rcast->ray_sign_x == 1) - ray->end.x += rcast->cell; - if (rcast->slope_x) - { - rcast->ratio = (double)(ray->end.x - ray->start.x) / (double)rcast->slope_x; - ray->end.y = ray->start.y + (double)rcast->slope_y * rcast->ratio; - } - } - else - { - ray->end.y = rcast->cell_y * rcast->cell; - if (rcast->ray_sign_y == 1) - ray->end.y += rcast->cell; - if (rcast->slope_y) - { - rcast->ratio = (double)(ray->end.y - ray->start.y) / (double)rcast->slope_y; - ray->end.x = ray->start.x + (double)rcast->slope_x * rcast->ratio; - } - } - */ - int *nd1; - int *cell1; - int *sign1; - int *st1; - int *slp1; - int *nd2; - int *st2; - int *slp2; - - if (rcast->is_x) - { - nd1 = &(ray->end.x); - cell1 = &(rcast->cell_x); - sign1 = &(rcast->ray_sign_x); - st1 = &(ray->start.x); - slp1 = &(rcast->slope_x); - nd2 = &(ray->end.y); - st2 = &(ray->start.y); - slp2 = &(rcast->slope_y); - } - else - { - nd1 = &(ray->end.y); - cell1 = &(rcast->cell_y); - sign1 = &(rcast->ray_sign_y); - st1 = &(ray->start.y); - slp1 = &(rcast->slope_y); - nd2 = &(ray->end.x); - st2 = &(ray->start.x); - slp2 = &(rcast->slope_x); - } - *nd1 = *cell1 * rcast->cell; - if (*sign1 == 1) - *nd1 += rcast->cell; - if (*slp1) - *nd2 = *st2 + (double)(*slp2) * (double)(*nd1 - *st1) / (double)(*slp1); -} - void ray_intersect(t_game *game, t_rcast *rcast, t_vec *ray) { ray->start.x = rcast->ray.start.x + game->plr.pos.x; @@ -132,9 +67,6 @@ void ray_intersect(t_game *game, t_rcast *rcast, t_vec *ray) rotate(&(game->plr), &(ray->end)); init_raycast(rcast, ray); init_first_step(rcast, ray); - // loop through grid while (!is_wall(game, rcast->cell_x, rcast->cell_y)) next_cell(rcast); - // end ray position - calcul_ray_end(rcast, ray); } diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 4abadf5..7fbe603 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -1,5 +1,42 @@ #include "cube3d.h" +// tmp, to draw rays + static void calcul_ray_end(t_rcast *rcast, t_vec *ray) + { + if (rcast->is_x) + { + ray->end.x = rcast->cell_x * rcast->cell; + if (rcast->ray_sign_x == 1) + ray->end.x += rcast->cell; + if (rcast->slope_x) + { + rcast->ratio = (double)(ray->end.x - ray->start.x); + rcast->ratio /= (double)rcast->slope_x; + ray->end.y = ray->start.y + (double)rcast->slope_y * rcast->ratio; + } + } + else + { + ray->end.y = rcast->cell_y * rcast->cell; + if (rcast->ray_sign_y == 1) + ray->end.y += rcast->cell; + if (rcast->slope_y) + { + rcast->ratio = (double)(ray->end.y - ray->start.y); + rcast->ratio /= (double)rcast->slope_y; + ray->end.x = ray->start.x + (double)rcast->slope_x * rcast->ratio; + } + } + } +// tmp end + +void draw_column(t_game *game, t_rcast *rcast, t_vec *ray) +{ + (void)game; + (void)rcast; + (void)ray; +} + void raycast(t_game *game, t_rcast *rcast) { t_vec ray; @@ -10,10 +47,11 @@ void raycast(t_game *game, t_rcast *rcast) ray_intersect(game, rcast, &ray); // tmp, to draw the map + calcul_ray_end(rcast, &ray); draw_line(game, ray, 0x00FF00FF); // tmp end - // draw_column(); + draw_column(game, rcast, &ray); (rcast->ray_nb)++; } } diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index cc07c9a..d2d465e 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -68,22 +68,22 @@ static void init_raycast(t_rcast *rcast) double dist; dist = (SCREEN_FOCAL / 2) * M_PI / 180; - dist = cos(dist) * ((SCREEN_DEF / 2) / sin(dist)); - rcast->screen_def = SCREEN_DEF; + dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist)); + rcast->screen_def = SCREEN_WIDTH; // screen dist rcast->screen_dist.start.x = 0; rcast->screen_dist.start.y = 0; rcast->screen_dist.end.x = 0; rcast->screen_dist.end.y = -dist; // screen size - rcast->screen_size.start.x = -SCREEN_DEF / 2; + rcast->screen_size.start.x = -SCREEN_WIDTH / 2; rcast->screen_size.start.y = -dist; - rcast->screen_size.end.x = SCREEN_DEF / 2; + rcast->screen_size.end.x = SCREEN_WIDTH / 2; rcast->screen_size.end.y = -dist; // first ray rcast->ray.start.x = 0; rcast->ray.start.y = 0; - rcast->ray.end.x = -SCREEN_DEF / 2; + rcast->ray.end.x = -SCREEN_WIDTH / 2; rcast->ray.end.y = -dist; } @@ -111,21 +111,30 @@ t_game *init_game(void) init_map(&(game->map)); // plr init_plr(&(game->plr)); - // size window map - game->map_win.size_x = game->map.size_x * CELL; - game->map_win.size_y = game->map.size_y * CELL; // init connexion to server game->mlx_ptr = mlx_init(); mb_add(game->mlx_ptr); - // create the window - game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x, - game->map_win.size_y, "test"); + + // tmp draw map + // size window map + game->map_win.size_x = game->map.size_x * CELL; + game->map_win.size_y = game->map.size_y * CELL + SCREEN_HEIGHT; + // create the window + game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x, + game->map_win.size_y, "map"); + // create image and get its data address + init_img(&(game->map_img), game); + // tmp end + + game->win.size_x = SCREEN_WIDTH; + game->win.size_y = SCREEN_HEIGHT; + game->win.ptr = mlx_new_window(game->mlx_ptr, game->win.size_x, + game->win.size_y, "cub3d"); + init_img(&(game->img), game); // k(ey)_hook is the array containing the values of key press events ft_bzero(&game->k_hook, sizeof(game->k_hook)); // raycasting init_raycast(&(game->rcast)); game->rcast.cell = game->map.cell; - // create image and get its data address - init_img(&(game->map_img), game); return (game); } From a11ea35ac1646b8ab2bde071b93039e745955de9 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 24 Apr 2022 19:41:44 +0200 Subject: [PATCH 4/7] changed functions draw line and pixel to receive an img ptr instead of the whole structure --- headers/cube3d_proto.h | 4 ++-- headers/cube3d_struct.h | 6 ++++-- srcs/draw/draw.c | 28 ++++++++++++++-------------- srcs/draw/raycast.c | 18 +++++++++++++++++- srcs/init/init_struct.c | 20 ++++++++------------ srcs/player/player_limits.c | 2 +- 6 files changed, 46 insertions(+), 32 deletions(-) diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index 2fcbe4a..011bfe4 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -62,8 +62,8 @@ int is_wall(t_game *game, int cell_x, int cell_y); // SRC/DRAW // ------------------------------- // draw.c -void draw_pixel(t_game *game, int x, int y, int color); -void draw_line(t_game *game, t_vec vec, int color); +void draw_pixel(t_img *img, int x, int y, int color); +void draw_line(t_img *img, t_vec vec, int color); void draw(t_game *game); // raycast.c void raycast(t_game *game, t_rcast *rcast); diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index 3ebeffb..a092d92 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -54,6 +54,7 @@ typedef struct s_rcast int is_x; int ray_nb; int wall_height; + int screen_center; double ratio; } t_rcast; @@ -73,8 +74,9 @@ typedef struct s_img void *ptr; char *data; int bpp; - int sizel; - int endian; + int szl; + int ndn; + int height; } t_img; /* diff --git a/srcs/draw/draw.c b/srcs/draw/draw.c index 62f4e72..077c3a2 100644 --- a/srcs/draw/draw.c +++ b/srcs/draw/draw.c @@ -1,28 +1,28 @@ #include "cube3d.h" -static int pxl_out_limits(t_game *game, int x, int y) +static int pxl_out_limits(t_img *img, int x, int y) { int xmax; int ymax; - xmax = game->map_img.sizel / (game->map_img.bpp / 8); - ymax = game->map_win.size_y; + xmax = img->szl / (img->bpp / 8); + ymax = img->height; if (x < 0 || y < 0 || x > xmax || y > ymax) return (1); return (0); } -void draw_pixel(t_game *game, int x, int y, int color) +void draw_pixel(t_img *img, int x, int y, int color) { unsigned int position; - if (pxl_out_limits(game, x, y)) + if (pxl_out_limits(img, x, y)) return ; - position = y * game->map_img.sizel + x * (game->map_img.bpp / 8); - *(unsigned int*)(game->map_img.data + position) = color; + position = y * img->szl + x * (img->bpp / 8); + *(unsigned int*)(img->data + position) = color; } -void draw_line(t_game *game, t_vec vec, int color) +void draw_line(t_img *img, t_vec vec, int color) { t_coord dist; int i; @@ -34,7 +34,7 @@ void draw_line(t_game *game, t_vec vec, int color) j = 0; while (ft_abs(i) <= ft_abs(dist.x) && ft_abs(j) <= ft_abs(dist.y)) { - draw_pixel(game, vec.start.x + i, vec.start.y + j, color); + draw_pixel(img, vec.start.x + i, vec.start.y + j, color); if (!ft_abs(dist.x) || ft_abs(j) < ft_abs(i * dist.y / dist.x)) j += ft_sign(dist.y); else @@ -60,11 +60,11 @@ void draw_line(t_game *game, t_vec vec, int color) if (rotation) rotate(&(game->plr), &(new)); if (!i || i == size - 1) - draw_pixel(game, new.x, new.y, border); + draw_pixel(&game->map_img, new.x, new.y, border); else if (!j || j == size - 1) - draw_pixel(game, new.x, new.y, border); + draw_pixel(&game->map_img, new.x, new.y, border); else - draw_pixel(game, new.x, new.y, fill); + draw_pixel(&game->map_img, new.x, new.y, fill); j++; } i++; @@ -113,14 +113,14 @@ void draw_line(t_game *game, t_vec vec, int color) screen.end.x = rcast->screen_size.end.x + game->plr.pos.x; screen.end.y = rcast->screen_size.end.y + game->plr.pos.y; rotate(&(game->plr), &(screen.end)); - draw_line(game, screen, 0x00FFFFFF); + draw_line(&game->map_img, screen, 0x00FFFFFF); // draw screen dist screen.start.x = rcast->screen_dist.start.x + game->plr.pos.x; screen.start.y = rcast->screen_dist.start.y + game->plr.pos.y; screen.end.x = rcast->screen_dist.end.x + game->plr.pos.x; screen.end.y = rcast->screen_dist.end.y + game->plr.pos.y; rotate(&(game->plr), &(screen.end)); - draw_line(game, screen, 0x00FFFFFF); + draw_line(&game->map_img, screen, 0x00FFFFFF); } // tmp end diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 7fbe603..8c69fbd 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -32,9 +32,25 @@ void draw_column(t_game *game, t_rcast *rcast, t_vec *ray) { + t_vec wall; + (void)game; (void)rcast; (void)ray; + (void)wall; + + wall.start.x = rcast->ray_nb; + wall.end.x = rcast->ray_nb; + if (rcast->is_x == 1 && rcast->slope_y != 0) + { + rcast->wall_height = rcast->next_x - rcast->ray_step_x; + rcast->wall_height /= ft_abs(rcast->slope_y); + rcast->wall_height /= 2; +//printf("rcast->wall: %i\n", rcast->wall_height); + wall.start.y = rcast->screen_center / 2 + rcast->wall_height; + wall.end.y = rcast->screen_center / 2 - rcast->wall_height; + } +// draw_line(&game->img, wall, 0x00FF0000); } void raycast(t_game *game, t_rcast *rcast) @@ -48,7 +64,7 @@ void raycast(t_game *game, t_rcast *rcast) // tmp, to draw the map calcul_ray_end(rcast, &ray); - draw_line(game, ray, 0x00FF00FF); + draw_line(&game->map_img, ray, 0x00FF00FF); // tmp end draw_column(game, rcast, &ray); diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index d2d465e..188c40a 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -1,11 +1,10 @@ #include "cube3d.h" -static void init_img(t_img *img, t_game *game) +static void init_img(t_img *img, void *mlx_ptr, t_win *win) { - img->ptr = mlx_new_image(game->mlx_ptr, game->map_win.size_x, - game->map_win.size_y); - img->data = mlx_get_data_addr(img->ptr, &(img->bpp), - &(img->sizel), &(img->endian)); + img->ptr = mlx_new_image(mlx_ptr, win->size_x, win->size_y); + img->data = mlx_get_data_addr(img->ptr, &img->bpp, &img->szl, &img->ndn); + img->height = win->size_y; } static void init_map(t_map *map) @@ -70,6 +69,7 @@ static void init_raycast(t_rcast *rcast) dist = (SCREEN_FOCAL / 2) * M_PI / 180; dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist)); rcast->screen_def = SCREEN_WIDTH; + rcast->screen_center = SCREEN_HEIGHT / 2; // screen dist rcast->screen_dist.start.x = 0; rcast->screen_dist.start.y = 0; @@ -116,21 +116,17 @@ t_game *init_game(void) mb_add(game->mlx_ptr); // tmp draw map - // size window map game->map_win.size_x = game->map.size_x * CELL; game->map_win.size_y = game->map.size_y * CELL + SCREEN_HEIGHT; - // create the window - game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x, - game->map_win.size_y, "map"); - // create image and get its data address - init_img(&(game->map_img), game); + game->map_win.ptr = mlx_new_window(game->mlx_ptr, game->map_win.size_x, game->map_win.size_y, "map"); + init_img(&(game->map_img), game->mlx_ptr, &(game->map_win)); // tmp end game->win.size_x = SCREEN_WIDTH; game->win.size_y = SCREEN_HEIGHT; game->win.ptr = mlx_new_window(game->mlx_ptr, game->win.size_x, game->win.size_y, "cub3d"); - init_img(&(game->img), game); + init_img(&(game->img), game->mlx_ptr, &(game->win)); // k(ey)_hook is the array containing the values of key press events ft_bzero(&game->k_hook, sizeof(game->k_hook)); // raycasting diff --git a/srcs/player/player_limits.c b/srcs/player/player_limits.c index 9890d6d..41e83c5 100644 --- a/srcs/player/player_limits.c +++ b/srcs/player/player_limits.c @@ -18,7 +18,7 @@ int plr_out_limits(t_game *game, int x, int y) int cell_x; int cell_y; - xmax = game->map_img.sizel / (game->map_img.bpp / 8); + xmax = game->map_img.szl / (game->map_img.bpp / 8); ymax = game->map_win.size_y; cell_x = x / game->map.cell; cell_y = y / game->map.cell; From 03f61d989b033256a966dcd112d877321cd349b5 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 24 Apr 2022 21:18:36 +0200 Subject: [PATCH 5/7] game image created but all wrong --- headers/cube3d_proto.h | 2 +- headers/cube3d_struct.h | 6 ++--- srcs/draw/draw.c | 14 +++++----- srcs/draw/ray_intersect.c | 1 - srcs/draw/raycast.c | 56 ++++++++++++++++++++++++++------------- srcs/init/init_struct.c | 4 +-- 6 files changed, 50 insertions(+), 33 deletions(-) diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index 011bfe4..a699fc0 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -63,7 +63,7 @@ int is_wall(t_game *game, int cell_x, int cell_y); // ------------------------------- // draw.c void draw_pixel(t_img *img, int x, int y, int color); -void draw_line(t_img *img, t_vec vec, int color); +void draw_line(t_img *img, t_vec *vec, int color); void draw(t_game *game); // raycast.c void raycast(t_game *game, t_rcast *rcast); diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index a092d92..81cf47d 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -35,7 +35,9 @@ typedef struct s_rcast t_vec screen_dist; t_vec screen_size; t_vec ray; - int screen_def; + t_vec wall; + int screen_width; + int screen_height; int cell; int cell_x; int cell_y; @@ -53,8 +55,6 @@ typedef struct s_rcast int ray_step_y; int is_x; int ray_nb; - int wall_height; - int screen_center; double ratio; } t_rcast; diff --git a/srcs/draw/draw.c b/srcs/draw/draw.c index 077c3a2..08a0245 100644 --- a/srcs/draw/draw.c +++ b/srcs/draw/draw.c @@ -22,19 +22,19 @@ void draw_pixel(t_img *img, int x, int y, int color) *(unsigned int*)(img->data + position) = color; } -void draw_line(t_img *img, t_vec vec, int color) +void draw_line(t_img *img, t_vec *vec, int color) { t_coord dist; int i; int j; - dist.x = vec.end.x - vec.start.x; - dist.y = vec.end.y - vec.start.y; + dist.x = (*vec).end.x - (*vec).start.x; + dist.y = (*vec).end.y - (*vec).start.y; i = 0; j = 0; while (ft_abs(i) <= ft_abs(dist.x) && ft_abs(j) <= ft_abs(dist.y)) { - draw_pixel(img, vec.start.x + i, vec.start.y + j, color); + draw_pixel(img, (*vec).start.x + i, (*vec).start.y + j, color); if (!ft_abs(dist.x) || ft_abs(j) < ft_abs(i * dist.y / dist.x)) j += ft_sign(dist.y); else @@ -113,14 +113,14 @@ void draw_line(t_img *img, t_vec vec, int color) screen.end.x = rcast->screen_size.end.x + game->plr.pos.x; screen.end.y = rcast->screen_size.end.y + game->plr.pos.y; rotate(&(game->plr), &(screen.end)); - draw_line(&game->map_img, screen, 0x00FFFFFF); + draw_line(&game->map_img, &screen, 0x00FFFFFF); // draw screen dist screen.start.x = rcast->screen_dist.start.x + game->plr.pos.x; screen.start.y = rcast->screen_dist.start.y + game->plr.pos.y; screen.end.x = rcast->screen_dist.end.x + game->plr.pos.x; screen.end.y = rcast->screen_dist.end.y + game->plr.pos.y; rotate(&(game->plr), &(screen.end)); - draw_line(&game->map_img, screen, 0x00FFFFFF); + draw_line(&game->map_img, &screen, 0x00FFFFFF); } // tmp end @@ -137,5 +137,5 @@ void draw(t_game *game) mlx_put_image_to_window(game->mlx_ptr, game->map_win.ptr, game->map_img.ptr, 0, SCREEN_HEIGHT); // tmp end -// mlx_put_image_to_window(game->mlx_ptr, game->win.ptr, game->img.ptr, 0, 0); + mlx_put_image_to_window(game->mlx_ptr, game->win.ptr, game->img.ptr, 0, 0); } diff --git a/srcs/draw/ray_intersect.c b/srcs/draw/ray_intersect.c index decb5a5..163160e 100644 --- a/srcs/draw/ray_intersect.c +++ b/srcs/draw/ray_intersect.c @@ -3,7 +3,6 @@ static void init_raycast(t_rcast *rcast, t_vec *ray) { rcast->is_x = 0; - rcast->wall_height = 0; // ray sign rcast->ray_sign_x = 1; if (ray->start.x < ray->end.x) diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 8c69fbd..2115a99 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -30,27 +30,44 @@ } // tmp end -void draw_column(t_game *game, t_rcast *rcast, t_vec *ray) +static void calcul_wall(t_rcast *rcast) { - t_vec wall; + int height; - (void)game; - (void)rcast; - (void)ray; - (void)wall; - - wall.start.x = rcast->ray_nb; - wall.end.x = rcast->ray_nb; + rcast->wall.start.x = rcast->ray_nb; + rcast->wall.end.x = rcast->ray_nb; if (rcast->is_x == 1 && rcast->slope_y != 0) + height = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); + if (rcast->is_x == 0 && rcast->slope_x != 0) + height = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); + if (height > rcast->screen_height) + height = rcast->screen_height; + rcast->wall.start.y = rcast->screen_height / 2 + height / 2; + rcast->wall.end.y = rcast->screen_height / 2 - height / 2; +} + +void draw_column(t_game *game, t_rcast *rcast) +{ + t_vec floor; + t_vec ceilling; + + if (rcast->wall.start.y > 0) { - rcast->wall_height = rcast->next_x - rcast->ray_step_x; - rcast->wall_height /= ft_abs(rcast->slope_y); - rcast->wall_height /= 2; -//printf("rcast->wall: %i\n", rcast->wall_height); - wall.start.y = rcast->screen_center / 2 + rcast->wall_height; - wall.end.y = rcast->screen_center / 2 - rcast->wall_height; + floor.start.x = rcast->ray_nb; + floor.end.x = rcast->ray_nb; + floor.start.y = rcast->screen_height; + floor.end.y = rcast->wall.start.y; + draw_line(&game->img, &floor, 0x00FF0000); } -// draw_line(&game->img, wall, 0x00FF0000); + if (rcast->wall.start.y < rcast->screen_height) + { + ceilling.start.x = rcast->ray_nb; + ceilling.end.x = rcast->ray_nb; + ceilling.start.y = rcast->wall.end.y; + ceilling.end.y = 0; + draw_line(&game->img, &ceilling, 0x00FF0000); + } + draw_line(&game->img, &rcast->wall, 0x00FF00FF); } void raycast(t_game *game, t_rcast *rcast) @@ -58,16 +75,17 @@ void raycast(t_game *game, t_rcast *rcast) t_vec ray; rcast->ray_nb = 0; - while (rcast->ray_nb <= rcast->screen_def) + while (rcast->ray_nb <= rcast->screen_width) { ray_intersect(game, rcast, &ray); // tmp, to draw the map calcul_ray_end(rcast, &ray); - draw_line(&game->map_img, ray, 0x00FF00FF); + draw_line(&game->map_img, &ray, 0x00FF00FF); // tmp end - draw_column(game, rcast, &ray); + calcul_wall(rcast); + draw_column(game, rcast); (rcast->ray_nb)++; } } diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index 188c40a..d18d01f 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -68,8 +68,8 @@ static void init_raycast(t_rcast *rcast) dist = (SCREEN_FOCAL / 2) * M_PI / 180; dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist)); - rcast->screen_def = SCREEN_WIDTH; - rcast->screen_center = SCREEN_HEIGHT / 2; + rcast->screen_width = SCREEN_WIDTH; + rcast->screen_height = SCREEN_HEIGHT; // screen dist rcast->screen_dist.start.x = 0; rcast->screen_dist.start.y = 0; From 0d9e5879c0ada430f874b67ad375995891a217bc Mon Sep 17 00:00:00 2001 From: hugogogo Date: Mon, 25 Apr 2022 09:13:10 +0200 Subject: [PATCH 6/7] wall grows proportionally --- headers/cube3d_macro.h | 2 ++ headers/cube3d_struct.h | 1 + srcs/draw/raycast.c | 37 +++++++++++++++++++++---------------- srcs/init/init_struct.c | 1 + 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/headers/cube3d_macro.h b/headers/cube3d_macro.h index 1f4d472..477cce1 100644 --- a/headers/cube3d_macro.h +++ b/headers/cube3d_macro.h @@ -17,6 +17,8 @@ # define SCREEN_FOCAL 90 /* size of a cell on the map */ # define CELL 20 +/* height of a wall without perpectiv */ +# define WALL_HEIGHT 300 /* * keys macro diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index 81cf47d..012c6d6 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -36,6 +36,7 @@ typedef struct s_rcast t_vec screen_size; t_vec ray; t_vec wall; + int wall_height; int screen_width; int screen_height; int cell; diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 2115a99..26b0472 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -32,42 +32,47 @@ static void calcul_wall(t_rcast *rcast) { + int length; int height; rcast->wall.start.x = rcast->ray_nb; rcast->wall.end.x = rcast->ray_nb; if (rcast->is_x == 1 && rcast->slope_y != 0) - height = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); + length = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); if (rcast->is_x == 0 && rcast->slope_x != 0) - height = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); + length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); + height = rcast->wall_height - length; + if (height < 0) + height = 0; if (height > rcast->screen_height) height = rcast->screen_height; rcast->wall.start.y = rcast->screen_height / 2 + height / 2; - rcast->wall.end.y = rcast->screen_height / 2 - height / 2; + rcast->wall.end.y = rcast->screen_height / 2 - height / 2; } void draw_column(t_game *game, t_rcast *rcast) { - t_vec floor; - t_vec ceilling; + t_vec plane; + int color; + plane.start.x = rcast->ray_nb; + plane.end.x = rcast->ray_nb; if (rcast->wall.start.y > 0) { - floor.start.x = rcast->ray_nb; - floor.end.x = rcast->ray_nb; - floor.start.y = rcast->screen_height; - floor.end.y = rcast->wall.start.y; - draw_line(&game->img, &floor, 0x00FF0000); + plane.start.y = rcast->screen_height; + plane.end.y = rcast->wall.start.y; + draw_line(&game->img, &plane, 0x00FF0000); } if (rcast->wall.start.y < rcast->screen_height) { - ceilling.start.x = rcast->ray_nb; - ceilling.end.x = rcast->ray_nb; - ceilling.start.y = rcast->wall.end.y; - ceilling.end.y = 0; - draw_line(&game->img, &ceilling, 0x00FF0000); + plane.start.y = rcast->wall.end.y; + plane.end.y = 0; + draw_line(&game->img, &plane, 0x000000FF); } - draw_line(&game->img, &rcast->wall, 0x00FF00FF); + color = 0x00FF00FF; + if (rcast->is_x) + color = 0x00EE00EE; + draw_line(&game->img, &rcast->wall, color); } void raycast(t_game *game, t_rcast *rcast) diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index d18d01f..5546c5a 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -70,6 +70,7 @@ static void init_raycast(t_rcast *rcast) dist = cos(dist) * ((SCREEN_WIDTH / 2) / sin(dist)); rcast->screen_width = SCREEN_WIDTH; rcast->screen_height = SCREEN_HEIGHT; + rcast->wall_height = WALL_HEIGHT; // screen dist rcast->screen_dist.start.x = 0; rcast->screen_dist.start.y = 0; From ef4cc1a996f5a75f89905f18052847bb98ae719c Mon Sep 17 00:00:00 2001 From: hugogogo Date: Mon, 25 Apr 2022 13:17:55 +0200 Subject: [PATCH 7/7] visual 3d with buble effect and rays for slope null --- headers/cube3d_macro.h | 2 +- srcs/draw/raycast.c | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/headers/cube3d_macro.h b/headers/cube3d_macro.h index 477cce1..1e95774 100644 --- a/headers/cube3d_macro.h +++ b/headers/cube3d_macro.h @@ -18,7 +18,7 @@ /* size of a cell on the map */ # define CELL 20 /* height of a wall without perpectiv */ -# define WALL_HEIGHT 300 +# define WALL_HEIGHT 200 /* * keys macro diff --git a/srcs/draw/raycast.c b/srcs/draw/raycast.c index 26b0472..c0d0ea1 100644 --- a/srcs/draw/raycast.c +++ b/srcs/draw/raycast.c @@ -38,9 +38,18 @@ static void calcul_wall(t_rcast *rcast) rcast->wall.start.x = rcast->ray_nb; rcast->wall.end.x = rcast->ray_nb; if (rcast->is_x == 1 && rcast->slope_y != 0) + { length = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); + length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_x; +// length = (rcast->next_x - rcast->ray_step_x) / ft_abs(rcast->slope_y); + } if (rcast->is_x == 0 && rcast->slope_x != 0) + { length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); + length = (double)length * (double)rcast->screen_dist.end.y / (double)rcast->slope_y; +// length = (rcast->next_y - rcast->ray_step_y) / ft_abs(rcast->slope_x); + } + length = ft_abs(length); height = rcast->wall_height - length; if (height < 0) height = 0;