From 90c84c7b37c89edff4a512adf4e528175e9b18e0 Mon Sep 17 00:00:00 2001 From: Adam JOLY Date: Wed, 30 Oct 2024 16:33:14 +0100 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip(R?= =?UTF-8?q?aycasting):=20Should=20be=20working=20but=20not?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/game/dda.h | 5 +- includes/game/game.h | 37 ++++--- includes/game/math.h | 14 +++ includes/game/settings.h | 13 +-- .../print_map.c => includes/game/utils_math.h | 34 ++----- src/cub3d.c | 47 ++++----- src/raycasting/dda.c | 78 +++++++++------ src/raycasting/get_player_image.c | 35 ------- src/raycasting/key_hook.c | 46 +++++---- src/raycasting/maths.c | 24 ++--- src/raycasting/print_frame.c | 59 ----------- src/raycasting/renderer.c | 97 +++++++++++++++++++ .../{graphic_utils/draw_square.c => vectwo.c} | 39 ++++---- 13 files changed, 281 insertions(+), 247 deletions(-) create mode 100644 includes/game/math.h rename src/raycasting/print_map.c => includes/game/utils_math.h (56%) delete mode 100644 src/raycasting/get_player_image.c delete mode 100644 src/raycasting/print_frame.c create mode 100644 src/raycasting/renderer.c rename src/raycasting/{graphic_utils/draw_square.c => vectwo.c} (56%) diff --git a/includes/game/dda.h b/includes/game/dda.h index cf5473b..eab5af4 100644 --- a/includes/game/dda.h +++ b/includes/game/dda.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/28 13:04:36 by adjoly #+# #+# */ -/* Updated: 2024/10/28 14:37:10 by adjoly ### ########.fr */ +/* Updated: 2024/10/29 14:15:59 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ typedef struct s_dda { bool h; int i; + bool s; t_vec2 map; t_vec2 vert; t_vec2 hori; @@ -37,6 +38,6 @@ typedef struct s_ray float distance; } t_ray; -void dda_algo(t_player *play, t_render *render); +void dda_algo(t_render *render, t_dda *dda, t_ray *ray); #endif diff --git a/includes/game/game.h b/includes/game/game.h index 07cf4e8..1f4a5f8 100644 --- a/includes/game/game.h +++ b/includes/game/game.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */ -/* Updated: 2024/10/28 14:08:09 by adjoly ### ########.fr */ +/* Updated: 2024/10/29 13:56:32 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,26 +30,30 @@ typedef struct s_map { char **arr; + int floor; + int celling; char p_side; t_coord p_spawnpoint; t_coord size; } t_map; -typedef struct s_render -{ - t_map *world; - void *mlx; - void *win; - void *texture[4]; -} t_render; - typedef struct s_player { t_vec2 coord; - t_coord map_coords; - double direction; + float direction; } t_player; +typedef struct s_render +{ + t_map *world; + void *mlx; + void *win; + void *img; + t_player *player; + void *texture[4]; +} t_render; + + /** * @brief This function is used to handle keypress * @@ -71,6 +75,15 @@ int key_hook(int key, void *param); * @param clockwise The direction which the player rotate * @param player A pointer to a t_player struct */ -void change_direction(double speed, bool clockwise, t_player *player); +void change_direction(float speed, bool clockwise, t_player *player); + + +/** + * @brief Render a cub3d frame by executing a dda on every ray and + * putting the pixel on the window + * + * @param render The render struct with everything i need + */ +void render_frame(t_render *render); #endif diff --git a/includes/game/math.h b/includes/game/math.h new file mode 100644 index 0000000..e7956f9 --- /dev/null +++ b/includes/game/math.h @@ -0,0 +1,14 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* math.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/29 10:07:24 by adjoly #+# #+# */ +/* Updated: 2024/10/29 10:08:53 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef +void fix_ang(float *ang); diff --git a/includes/game/settings.h b/includes/game/settings.h index c8e9169..0a36884 100644 --- a/includes/game/settings.h +++ b/includes/game/settings.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/04 14:06:39 by adjoly #+# #+# */ -/* Updated: 2024/10/28 14:04:25 by adjoly ### ########.fr */ +/* Updated: 2024/10/29 14:55:59 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,12 +23,13 @@ # define WHITE 0xFFFFFFFF # define DEG_TO_RAD 0.0174533 -# define WINDOW_Y 900 -# define WINDOW_X 1600 +# define WINDOW_H 900 +# define WINDOW_W 1600 # define PLAYER_ROT_SPEED (2 * M_PI) / 128 -# define PLAYER_SPEED 3 -# define CHUNK_SIZE 64 -# define RAY_SIZE 2 +# define PLAYER_SPEED 0.01 +# define RAY_SIZE 1 # define FOV ( 60 * (M_PI / 180) ) +# define CHUNK_SIZE 64 + #endif diff --git a/src/raycasting/print_map.c b/includes/game/utils_math.h similarity index 56% rename from src/raycasting/print_map.c rename to includes/game/utils_math.h index a129f79..1a36bc3 100644 --- a/src/raycasting/print_map.c +++ b/includes/game/utils_math.h @@ -1,36 +1,20 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* print_map.c :+: :+: :+: */ +/* utils_math.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/09/23 14:09:17 by adjoly #+# #+# */ -/* Updated: 2024/10/06 17:58:25 by adjoly ### ########.fr */ +/* Created: 2024/10/29 10:07:24 by adjoly #+# #+# */ +/* Updated: 2024/10/29 10:08:04 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "game/game.h" -#include "mlx.h" -#include +#ifndef UTILS_MATH_H +# define UTILS_MATH_H -void print_map(t_cub *cub) -{ - size_t i; - size_t j; +#include - i = 0; - while (cub->map[i]) - { - j = 0; - while (cub->map[i][j]) - { - if (cub->map[i][j] == '1') - { - draw_square(cub, (t_coord){j * MAP_CHUNK_SIZE, i * MAP_CHUNK_SIZE}, MAP_CHUNK_SIZE, WHITE); - } - j++; - } - i++; - } -} +void fix_ang(float *ang); + +#endif diff --git a/src/cub3d.c b/src/cub3d.c index 0ab9ec3..fede1d3 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */ -/* Updated: 2024/10/24 12:51:06 by adjoly ### ########.fr */ +/* Updated: 2024/10/30 10:02:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,32 +17,33 @@ #include "mlx.h" #include -void *print_frame(t_cub *cub); - int main(int ac, char **av) { - t_cub cub; + t_render render; + t_map world; + t_player player; - cub.map = ft_calloc(6, sizeof(char *)); - cub.map[0] = ft_strdup("111111"); - cub.map[1] = ft_strdup("101001"); - cub.map[2] = ft_strdup("100001"); - cub.map[3] = ft_strdup("100001"); - cub.map[4] = ft_strdup("111111"); - cub.map[5] = NULL; + render.player = &player; + render.world = &world; + world.celling = 0xffffa07a; + world.floor = 0xffadd8e6; + world.arr = ft_split("11111 10001 10001 10001 11111", ' '); + world.size.x = 5; + world.size.y = 5; + player.coord.x = 1; + player.coord.y = 1; + player.direction = 0; + render.mlx = mlx_init(); + render.win = mlx_new_window(render.mlx, WINDOW_W, WINDOW_H, "WTF"); + render.img = mlx_new_image(render.mlx, WINDOW_W, WINDOW_H); + render_frame(&render); + mlx_put_image_to_window(render.mlx, render.win, render.img, 0, 0); (void)ac; (void)av; - cub.mlx = mlx_init(); - cub.win = mlx_new_window(cub.mlx, WINDOW_X, WINDOW_Y, "WTF"); - cub.player.coords.x = 128; - cub.player.coords.y = 128; - cub.player.direction = 0; - cub.img = print_frame(&cub); - mlx_put_image_to_window(cub.mlx, cub.win, cub.img, 0, 0); - mlx_on_event(cub.mlx, cub.win, MLX_KEYDOWN, key_hook, &cub); - mlx_loop(cub.mlx); - mlx_destroy_image(cub.mlx, cub.img); - mlx_destroy_window(cub.mlx, cub.win); - mlx_destroy_display(cub.mlx); + mlx_on_event(render.mlx, render.win, MLX_KEYDOWN, key_hook, &render); + mlx_loop(render.mlx); + mlx_destroy_image(render.mlx, render.img); + mlx_destroy_window(render.mlx, render.win); + mlx_destroy_display(render.mlx); return (0); } diff --git a/src/raycasting/dda.c b/src/raycasting/dda.c index 1d13f69..23db3ec 100644 --- a/src/raycasting/dda.c +++ b/src/raycasting/dda.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/07 16:55:09 by adjoly #+# #+# */ -/* Updated: 2024/10/28 14:40:14 by adjoly ### ########.fr */ +/* Updated: 2024/10/30 14:42:06 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,40 +18,54 @@ void setup_dda_hor(t_player *play, t_ray *ray, t_dda *dda) { dda->h = true; + dda->s = true; ray->tan = tan(ray->angle); if (cos(ray->angle) < -0.001) { + ray->offset.x = -CHUNK_SIZE; + ray->offset.y = CHUNK_SIZE * ray->tan; ray->pos.x = (((int)play->coord.x >> 6) << 6) - 0.001; ray->pos.y -= (play->coord.x - ray->pos.x) * ray->tan + play->coord.y; - ray->offset.x = -CHUNK_SIZE; - ray->offset.y = -ray->offset.x * ray->tan; } else if(cos(ray->angle) > 0.001) { + ray->offset.x = CHUNK_SIZE; + ray->offset.y = -CHUNK_SIZE * ray->tan; ray->pos.x = (((int)play->coord.x >> 6) << 6) + CHUNK_SIZE; ray->pos.y -= (play->coord.x - ray->pos.x) * ray->tan + play->coord.y; - ray->offset.x = CHUNK_SIZE; - ray->offset.y = -ray->offset.x * ray->tan; + } + else + { + dda->s = false; + ray->pos.x = play->coord.x; + ray->pos.y = play->coord.y; } } void setup_dda_ver(t_player *play, t_ray *ray, t_dda *dda) { + dda->s = true; dda->h = false; ray->tan = 1 / tan(ray->angle); if (sin(ray->angle) < -0.001) { - ray->pos.y = (((int)play->coord.y >> 6) << 6) - 0.001; + ray->offset.y = CHUNK_SIZE; + ray->offset.x = -CHUNK_SIZE * ray->tan; + ray->pos.y = (((int)play->coord.y >> 6) << 6) - CHUNK_SIZE; ray->pos.x = (play->coord.y - ray->pos.y) * ray->tan + play->coord.x; - ray->offset.y = -CHUNK_SIZE; - ray->offset.x = -ray->offset.y * ray->tan; } else if(sin(ray->angle) > 0.001) { - ray->pos.y = (((int)play->coord.y >> 6) << 6) + CHUNK_SIZE; + ray->offset.y = -CHUNK_SIZE; + ray->offset.x = CHUNK_SIZE * ray->tan; + ray->pos.y = (((int)play->coord.y >> 6) << 6) + 0.001; ray->pos.x -= (play->coord.y - ray->pos.y) * ray->tan + play->coord.x; - ray->offset.y = CHUNK_SIZE; - ray->offset.x = -ray->offset.y * ray->tan; + } + else + { + dda->s = false; + ray->pos.x = play->coord.x; + ray->pos.y = play->coord.y; } } @@ -62,15 +76,18 @@ void dda_loop(t_dda *dda, t_ray *ray, t_map *map, t_player *play) w = map->size.x; h = map->size.y; - while (dda->i) + while (dda->s && dda->i) { - dda->map.x = ((int)ray->pos.x >> 6); - dda->map.y = ((int)ray->pos.x >> 6); + dda->map.x = (int)(ray->pos.x / 64); + dda->map.y = (int)(ray->pos.y / 64); + printf("%f, %f\n", ray->pos.x, ray->pos.y); if (dda->map.x >= 0 && dda->map.x < w && dda->map.y >= 0 && dda->map.y < h && \ map->arr[(int)dda->map.y][(int)dda->map.x] == '1') { dda->i = 0; if (dda->h) + dda->distance.y = vec2_dist(play->coord, ray->pos); + else dda->distance.x = vec2_dist(play->coord, ray->pos); } else @@ -81,25 +98,28 @@ void dda_loop(t_dda *dda, t_ray *ray, t_map *map, t_player *play) } } -void dda(t_player *player, t_render *render) +void dda_algo(t_render *render, t_dda *dda, t_ray *ray) { - t_dda dda; - t_ray ray; - - setup_dda_hor(player, &ray, &dda); - dda_loop(&dda, &ray, render->world, player); - setup_dda_ver(player, &ray, &dda); - dda_loop(&dda, &ray, render->world, player); - if (dda.distance.x < dda.distance.y) + setup_dda_hor(render->player, ray, dda); + dda->i = render->world->size.y; + dda_loop(dda, ray, render->world, render->player); + dda->hori.x = ray->pos.x; + dda->hori.y = ray->pos.y; + setup_dda_ver(render->player, ray, dda); + dda->i = render->world->size.x; + dda_loop(dda, ray, render->world, render->player); + dda->vert.x = ray->pos.x; + dda->vert.y = ray->pos.y; + if (dda->distance.y < dda->distance.x) { - ray.pos.x = fabsf(dda.hori.x); - ray.pos.y = fabsf(dda.hori.y); - ray.distance = dda.distance.x; + ray->pos.x = fabsf(dda->hori.x); + ray->pos.y = fabsf(dda->hori.y); + ray->distance = dda->distance.x; } else { - ray.pos.x = fabsf(dda.vert.x); - ray.pos.y = fabsf(dda.vert.y); - ray.distance = dda.distance.y; + ray->pos.x = fabsf(dda->vert.x); + ray->pos.y = fabsf(dda->vert.y); + ray->distance = dda->distance.y; } } diff --git a/src/raycasting/get_player_image.c b/src/raycasting/get_player_image.c deleted file mode 100644 index 08c7fa1..0000000 --- a/src/raycasting/get_player_image.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_player_image.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/09/13 11:05:02 by adjoly #+# #+# */ -/* Updated: 2024/10/24 11:17:06 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "game/game.h" -#include "game/settings.h" -#include "math.h" -#include - -void change_direction(double speed, bool clockwise, t_player *player) -{ - if (clockwise) - player->direction -= speed; - else - player->direction += speed; - if (player->direction >= 2 * M_PI) - player->direction = 0; - else if (player->direction < 0) - player->direction = 2 * M_PI; -} - -void get_player_image(t_cub *cub) -{ - print_map(cub); - draw_square(cub, (t_coord){cub->player.coords.x - 2, cub->player.coords.y - 2}, \ - 5, WHITE); -} diff --git a/src/raycasting/key_hook.c b/src/raycasting/key_hook.c index 153cf85..accc010 100644 --- a/src/raycasting/key_hook.c +++ b/src/raycasting/key_hook.c @@ -6,43 +6,49 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/11 16:37:56 by adjoly #+# #+# */ -/* Updated: 2024/10/24 11:41:30 by adjoly ### ########.fr */ +/* Updated: 2024/10/29 14:57:10 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +#include "game/settings.h" #include "mlx.h" -#include "game/game.h" +#include +#include -void *print_frame(t_cub *cub); +void change_direction(float speed, bool clockwise, t_player *player) +{ + if (clockwise) + player->direction += speed; + else + player->direction -= speed; + fix_ang(&player->direction); +} int key_hook(int key, void *param) { - t_cub *cub; + t_render *render; - cub = (t_cub *)param; - (void)cub; + render = (t_render *)param; if (key == ESCAPE_KEY) - { - mlx_loop_end(cub->mlx); - return (0); - } + return (mlx_loop_end(render->mlx)); if (key == W_KEY) { - cub->player.coords.x += PLAYER_SPEED * cos(cub->player.direction); - cub->player.coords.y += PLAYER_SPEED * sin(cub->player.direction); + render->player->coord.x += PLAYER_SPEED * cos(render->player->direction); + render->player->coord.y += PLAYER_SPEED * sin(render->player->direction); } else if (key == S_KEY) { - cub->player.coords.x -= PLAYER_SPEED * cos(cub->player.direction); - cub->player.coords.y -= PLAYER_SPEED * sin(cub->player.direction); + render->player->coord.x -= PLAYER_SPEED * cos(render->player->direction); + render->player->coord.y -= PLAYER_SPEED * sin(render->player->direction); } else if (key == D_KEY) - change_direction(PLAYER_ROT_SPEED, false, &cub->player); + change_direction(PLAYER_ROT_SPEED, false, render->player); else if (key == A_KEY) - change_direction(PLAYER_ROT_SPEED, true, &cub->player); - mlx_destroy_image(cub->mlx, cub->img); - cub->img = print_frame(cub); - //get_player_image(cub); - mlx_put_image_to_window(cub->mlx, cub->win, cub->img, 0, 0); + change_direction(PLAYER_ROT_SPEED, true, render->player); + printf("player coord x = %f, y = %f\n", render->player->coord.x, render->player->coord.y); + mlx_destroy_image(render->mlx, render->img); + render->img = mlx_new_image(render->mlx, WINDOW_W, WINDOW_H); + render_frame(render); + mlx_put_image_to_window(render->mlx, render->win, render->img, 0, 0); return (0); } diff --git a/src/raycasting/maths.c b/src/raycasting/maths.c index 3d5917c..4eaa3c2 100644 --- a/src/raycasting/maths.c +++ b/src/raycasting/maths.c @@ -5,27 +5,17 @@ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/10/26 15:47:29 by adjoly #+# #+# */ -/* Updated: 2024/10/28 14:00:34 by adjoly ### ########.fr */ +/* Created: 2024/10/29 10:05:15 by adjoly #+# #+# */ +/* Updated: 2024/10/29 10:05:29 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include #include -float vec2_dist(t_vec2 first, t_vec2 second) +void fix_ang(float *ang) { - return (sqrt(powf(second.x - first.x, 2) + powf(second.y - first.y, 2))); -} - -void vec2_add(t_vec2 *first, t_vec2 second) -{ - first->x += second.x; - first->y += second.y; -} - -void vec2_sub(t_vec2 *first, t_vec2 second) -{ - first->x -= second.x; - first->y -= second.y; + if (*ang < 0) + *ang += 2 * M_PI; + if (*ang > 2 * M_PI) + *ang -= 2 * M_PI; } diff --git a/src/raycasting/print_frame.c b/src/raycasting/print_frame.c deleted file mode 100644 index 5a562d0..0000000 --- a/src/raycasting/print_frame.c +++ /dev/null @@ -1,59 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* print_frame.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/10/13 21:01:53 by adjoly #+# #+# */ -/* Updated: 2024/10/23 11:19:58 by adjoly ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "game/dda.h" -#include "mlx.h" -#include -#include "game/settings.h" - -void draw_rectangle(t_cub *cub, void *img, t_coord start, t_coord end) -{ - int x; - int y; - - x = start.x; - y = start.y; - while (x < end.x) - { - while (y < end.y) - { - mlx_set_image_pixel(cub->mlx, img, x, y, WHITE); - y++; - } - x++; - } -} - -void *print_frame(t_cub *cub) -{ - uint16_t *lines; - int start; - int stop; - size_t i; - void *img; - - lines = dda(&(cub->player), cub->map); - img = mlx_new_image(cub->mlx, WINDOW_X, WINDOW_Y); - i = 0; - while (i < WINDOW_X / RAY_SIZE) - { - start = -lines[i] / 2 + WINDOW_Y / 2; - if (start < 0) - start = 0; - stop = lines[i] / 2 + WINDOW_Y / 2; - if (stop >= WINDOW_Y) - stop = WINDOW_Y - 1; - draw_rectangle(cub, img, (t_coord){i * RAY_SIZE, start}, (t_coord){i * RAY_SIZE + RAY_SIZE + 1, stop}); - i++; - } - return (img); -} diff --git a/src/raycasting/renderer.c b/src/raycasting/renderer.c new file mode 100644 index 0000000..03691a6 --- /dev/null +++ b/src/raycasting/renderer.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* renderer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/28 15:48:39 by adjoly #+# #+# */ +/* Updated: 2024/10/29 14:22:37 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "game/utils_math.h" +#include "mlx.h" +#include +#include +#include + +void draw_floor(t_render *render) +{ + size_t x; + size_t y; + + y = WINDOW_H / 2; + while(y < WINDOW_H - 1) + { + x = 0; + while (x < WINDOW_W) + { + mlx_set_image_pixel(render->mlx, render->img, x, y, render->world->floor); + x++; + } + y++; + } +} + +void draw_celling(t_render *render) +{ + size_t x; + size_t y; + + y = 0; + while(y < WINDOW_H / 2) + { + x = 0; + while (x < WINDOW_W) + { + mlx_set_image_pixel(render->mlx, render->img, x, y, render->world->celling); + x++; + } + y++; + } +} + +void print_line(t_render *render, t_ray *ray, int x) +{ + int line; + int line_start; + int y; + int j; + + line = (CHUNK_SIZE * WINDOW_H) / ray->distance; + line_start = -line / 2 + WINDOW_H / 2; + y = 0; + while (y < line) + { + j = -1; + while (++j <= RAY_SIZE) + mlx_set_image_pixel(render->mlx, render->img, x + j, line_start + y, WHITE); + y++; + } +} + +void render_frame(t_render *render) +{ + size_t i; + t_dda dda; + t_ray ray; + float ang; + + i = WINDOW_W; + ray.angle = render->player->direction - (M_PI / 180) / (FOV / 2); + fix_ang(&ray.angle); + draw_celling(render); + draw_floor(render); + while(i > 0) + { + dda_algo(render, &dda, &ray); + ang = render->player->direction - ray.angle; + fix_ang(&ang); + ray.distance *= cos(ang); + print_line(render, &ray, i); + ray.angle += (M_PI / 180) / (WINDOW_W / FOV); + fix_ang(&ray.angle); + i -= RAY_SIZE; + } +} diff --git a/src/raycasting/graphic_utils/draw_square.c b/src/raycasting/vectwo.c similarity index 56% rename from src/raycasting/graphic_utils/draw_square.c rename to src/raycasting/vectwo.c index 3f82596..5795d1a 100644 --- a/src/raycasting/graphic_utils/draw_square.c +++ b/src/raycasting/vectwo.c @@ -1,31 +1,32 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* draw_square.c :+: :+: :+: */ +/* vectwo.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/09/17 11:09:52 by adjoly #+# #+# */ -/* Updated: 2024/10/06 15:41:55 by adjoly ### ########.fr */ +/* Created: 2024/10/26 15:47:29 by adjoly #+# #+# */ +/* Updated: 2024/10/28 15:20:24 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "game/game.h" -#include "mlx.h" +#include "game/vectwo.h" +#include -void draw_square(t_cub *cub, t_coord coord, uint16_t size, int color) + +float vec2_dist(t_vec2 first, t_vec2 second) { - t_coord start; - - start = coord; - while (coord.x - start.x <= size) - { - coord.y = start.y; - while (coord.y - start.y <= size) - { - mlx_set_image_pixel(cub->mlx, cub->img, coord.x, coord.y, color); - coord.y++; - } - coord.x++; - } + return (sqrt(powf(second.x - first.x, 2) + powf(second.y - first.y, 2))); +} + +void vec2_add(t_vec2 *first, t_vec2 second) +{ + first->x += second.x; + first->y += second.y; +} + +void vec2_sub(t_vec2 *first, t_vec2 second) +{ + first->x -= second.x; + first->y -= second.y; }