From 65a622bf10eeda40224edf94cface7ca6c2ce73b Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Tue, 2 May 2023 16:58:55 +0200 Subject: [PATCH] Add dda and raycasting start --- game/.game.h.swp | Bin 0 -> 12288 bytes game/dda.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++ game/game.h | 21 ++++++-- game/raycasting.c | 6 +-- 4 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 game/.game.h.swp create mode 100644 game/dda.c diff --git a/game/.game.h.swp b/game/.game.h.swp new file mode 100644 index 0000000000000000000000000000000000000000..8de16d6bcfe3054096eb7c0fd58c632372127d5e GIT binary patch literal 12288 zcmeI2Piz}S7{#Z(lm(Ikl{g@ThHM3`(|YZ-6Ov7-FiA)(aHP_tRXMO4XUF!c>-B1P zHjY&RLIQCGP6#esK;i;d4j^#?Avov65l-9!;>?@`s4Hq9ky#`?|9%zpD`=8Y#( zak%rwI@F;Bu1-`Rcz?WyLI3P3E_IvB$f-*a{~{ zznKdfB4j%(-ZhrvFp8t9X>7+$URg2Ptjm+E*qJP$T9pIJfulGOi`M0ZsjZ4xPL8@h z^E5s8*w#^$R3GJlazHtt98eA@2b2TK0p);l;DkCLf>Y!z%>8sW|3>ya^^a#(RS)HW zazHtt98eA@2b2TK0p);lKslfsP!1>uPM`zKA>@-Yg#7*hCXe6$^Z)<9I!nmwUMOoP83BIHl-8`uLsfUm(9;A8Ls zcpvmZ3|inO*Z>W%0Or9(@FX|`_8%nV7w|p!5_}3i1n+@6paX7zm%#Jj3U~&b2af{+ z_BBF&1$*FU@DunBd=5SXAAuI&;5DFw8Sp6h1HSEp#J>}`|7yz00p);lKslfs_@6tV zbKh}Wgt)%YLN?fDk#PO?1!2ouhqNH`i%Y~P&{u1>mTT8H>lS@SsGX0sBb1wZ)j=U5G`^=)1nt z>u0S(_V9L>$Q>dE0e5(dMk0*21&tER2$2Ii4nw^3RC7Ysw_);udL&pV@)pnax!vJz zyEANxT!-hcJMO4i@W7VyOOzv7!%Q@%7dJhw1w0JwKJz@=aU)R5Hjd}ud3FcVZKchA0%sYJRn`C2_TE)k$QwhCwB z0J$GQFFMc{)ycy{b-;0LSEtx!Y!Y2xt*@2wUHv`nW^W3^3Qg$bGK?qoHc8u@mV2# +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/05/02 15:49:00 by erey-bet #+# #+# */ +/* Updated: 2023/05/02 16:58:31 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "game.h" + +void init_dda(t_game *game, t_dda *dda) +{ + t_ray *ray + + ray = game->ray; + dda->map_x = (int)game->ply->pos_x; + dda->map_y = (int)game->ply->pos_y; + if (ray->dir_x == 0) + dda->delta_dist_x = 1e30; + else + dda->delta_dist_x = abs(1 / ray->dir_x); + if (ray->dir_y == 0) + dda->delta_dist_y = 1e30; + else + dda->delta_dist_y = abs(1 / ray->dir_y); + dda->hit = 0; + if (ray->dir_x < 0) + { + dda->step_x = -1; + dda->side_dist_x = (game->ply->pos_x - dda->map_x) * dda->delta_dist_x; + } + else + { + dda->step_x = 1; + dda->side_dist_x = (dda->map_x + 1.0 - game->ply->pos_y) * dda->delta_dist_x; + } + if (ray->dir_y < 0) + { + dda->step_y = -1; + dda->side_dist_y = (game->ply->pos_y - dda->map_y) * dda->delta_dist_y; + } + else + { + dda->step_y = 1; + dda->side_dist_y = (dda->map_y + 1.0 - game->ply->pos_y) * dda->delta_dist_y; + } +} + +void loop_dda(t_game *game, t_dda *dda) +{ + while (dda->hit == 0) + { + if (dda->side_dist_x < dda->side_dist_y) + { + dda->side_dist_x += dda->delta_dist_x; + dda->map_x += dda->step_x; + dda->side = 0; + } + else + { + dda->side_dist_y += dda->delta_dist_y; + dda->map_y += dda->step_y; + dda->side = 1; + } + if (game->map->map[dda->map_x][dda->map_y] > 0) + dda->hit = 1; + } +} + +void ray_dda(t_game *game, t_dda *dda) +{ + if(dda->side == 0) + dda->perp_wall_dist = (dda->side_dist_x - dda->delta_dist_x); + else + dda->perp_wall_dist = (dda->side_dist_y - dda->delta_dist_y); + dda->line_height = (int)(HEIGHT / dda->perp_wall_dist); + dda->draw_start = -dda->line_height / 2 + HEIGHT / 2; + if(dda->draw_start < 0) + dda->draw_start = 0; + dda->draw_end = dda->line_height / 2 + HEIGHT / 2; + if(draw_end >= HEIGHT) + draw_end = HEIGHT - 1; +} + +int create_trgb(int t, int r, int g, int b) +{ + return (t << 24 | r << 16 | g << 8 | b); +} + +#include +#include + +void draw_dda(t_game *game, t_dda *dda) +{ + int color; + + srand(time(NULL)); + int r = rand() % 4; + if (r == 0) + color = create_trgv(255, 150, 10, 200); + else if (r == 1) + color = create_trgv(255, 10, 150, 200); + else if (r == 2) + color = create_trgv(255, 200, 150, 10); + else if (r == 3) + color = create_trgv(255, 10, 200, 150); + + mlx_put_pixel(game->mlx->window, abs(x), y, +} + +void dda(t_game *game, t_dda *dda) +{ + init_dda(game, dda); + loop_dda(game, dda); + ray_dda(game, dda); + draw_dda(game, dda); +} diff --git a/game/game.h b/game/game.h index 841cb55..8709a06 100644 --- a/game/game.h +++ b/game/game.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */ -/* Updated: 2023/05/02 15:32:43 by erey-bet ### ########.fr */ +/* Updated: 2023/05/02 15:56:17 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,10 +31,25 @@ typedef struct s_player typedef struct s_raycast { - double x; - double y; + double dir_x; + double dir_y; } t_ray; +typedef struct s_dda +{ + int map_x; + int map_y; + double side_dist_x; + double side_dist_y; + double delta_dist_x; + double delta_dist_y; + double perp_wall_dist; + int step_x; + int step_y; + int hit; + int side; +} t_dda; + typedef struct s_game { mlx_t *mlx; diff --git a/game/raycasting.c b/game/raycasting.c index b46b795..742dbf0 100644 --- a/game/raycasting.c +++ b/game/raycasting.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/27 14:30:29 by erey-bet #+# #+# */ -/* Updated: 2023/05/02 13:12:15 by erey-bet ### ########.fr */ +/* Updated: 2023/05/02 15:56:09 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,8 +25,8 @@ int ray(t_game *game) while (i <= WIDTH) { camera = 2 * i / (double)WIDTH - 1; //x-coordinate in camera space - ray->x = p->dir_x + p->pla_x * camera; - ray->y = p->dir_y + p->pla_y * camera; + ray->dir_x = p->dir_x + p->pla_x * camera; + ray->dir_y = p->dir_y + p->pla_y * camera; } }