From bce9da0440c85e97c9246c707cd260c84763e887 Mon Sep 17 00:00:00 2001 From: Adam JOLY Date: Sat, 26 Oct 2024 09:42:15 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip(R?= =?UTF-8?q?aycasting):=20Seems=20working=20but=20not?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .envrc | 1 - Makefile | 4 +-- includes/game/dda.h | 4 +-- includes/game/game.h | 7 ++--- includes/game/settings.h | 6 ++-- includes/game/typedef.h | 9 +++--- includes/parsing.h | 2 +- src/cub3d.c | 26 ++++++++++-------- src/raycasting/dda/dda.c | 12 ++++---- src/raycasting/dda/dist_dda.c | 8 ++---- src/raycasting/dda/get_ray_angle.c | 11 ++++---- src/raycasting/dda/line_dda.c | 7 ++--- src/raycasting/dda/setup_dda.c | 26 ++++++++++-------- src/raycasting/dda/while_dda.c | 44 +++++++++++++++++++----------- src/raycasting/get_player_image.c | 24 ++++------------ src/raycasting/key_hook.c | 30 +++++++++++++------- src/raycasting/print_frame.c | 14 +++++----- 17 files changed, 122 insertions(+), 113 deletions(-) delete mode 100644 .envrc diff --git a/.envrc b/.envrc deleted file mode 100644 index 3550a30..0000000 --- a/.envrc +++ /dev/null @@ -1 +0,0 @@ -use flake diff --git a/Makefile b/Makefile index 44a6bc1..b74af95 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/07 16:02:18 by adjoly #+# #+# # -# Updated: 2024/10/19 12:19:00 by adjoly ### ########.fr # +# Updated: 2024/10/19 15:15:37 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -14,7 +14,7 @@ SHELL = bash NAME = cub3D -CC = gcc +CC = cc OBJSDIR = obj/ diff --git a/includes/game/dda.h b/includes/game/dda.h index 57cb364..3ff30be 100644 --- a/includes/game/dda.h +++ b/includes/game/dda.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/04 14:04:10 by adjoly #+# #+# */ -/* Updated: 2024/10/13 21:28:23 by adjoly ### ########.fr */ +/* Updated: 2024/10/23 15:00:05 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ void setup_dda(t_dda (*dda)[800], t_player *player); * @param map The map as a char ** * @param map_coord The coordinate of the player on the map_grid */ -void while_dda(t_dda (*dda)[800], char **map, t_coord map_coord); +void while_dda(t_dda (*dda)[800], char **map, t_player *player); /// NEED TO COMBINE THOSE IF NOT USEFULL TO SEPARATE /** diff --git a/includes/game/game.h b/includes/game/game.h index 65cb74a..afee3a5 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/08 21:42:52 by adjoly ### ########.fr */ +/* Updated: 2024/10/24 11:38:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,9 +41,8 @@ int key_hook(int key, void *param); * * @param cub The address of a t_cub struct filled with mlx data and other * thing - * */ -void get_player_image(t_cub *cub, uint8_t key_pressed); +void get_player_image(t_cub *cub); /** * @brief This function is here to change the direction of the player @@ -54,7 +53,6 @@ void get_player_image(t_cub *cub, uint8_t key_pressed); * @param speed The speed a which the player rotate * @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); @@ -65,7 +63,6 @@ void change_direction(double speed, bool clockwise, t_player *player); * @param coord The coordinate of the printed square * @param size The size of the printed square * @param color The color of the printed square - * */ void draw_square(t_cub *cub, t_coord coord, uint16_t size, int color); diff --git a/includes/game/settings.h b/includes/game/settings.h index 13294b9..fe2fd5b 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/04 16:38:10 by adjoly ### ########.fr */ +/* Updated: 2024/10/23 11:18:38 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,10 @@ # define WHITE 0xFFFFFFFF # define WINDOW_Y 900 # define WINDOW_X 1600 -# define PLAYER_ROT_SPEED (2 * M_PI) / 8 +# define PLAYER_ROT_SPEED (2 * M_PI) / 128 # define PLAYER_SPEED 3 # define MAP_CHUNK_SIZE 64 # define RAY_SIZE 2 -# define FOV 60 * (M_PI / 180) +# define FOV ( 60 * (M_PI / 180) ) #endif diff --git a/includes/game/typedef.h b/includes/game/typedef.h index ef15a02..4e56264 100644 --- a/includes/game/typedef.h +++ b/includes/game/typedef.h @@ -6,15 +6,15 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/04 14:28:24 by adjoly #+# #+# */ -/* Updated: 2024/10/08 13:43:59 by adjoly ### ########.fr */ +/* Updated: 2024/10/23 14:33:33 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef STRUCT_H # define STRUCT_H -# include "parsing.h" # include +# include "parsing.h" /** * @brief Enum for the wall side that has been hit @@ -39,8 +39,9 @@ typedef struct s_step typedef struct s_player { - t_coord coords; - double direction; + t_coord coords; + t_coord map_coords; + double direction; } t_player; typedef struct s_cub diff --git a/includes/parsing.h b/includes/parsing.h index 490f354..4671c29 100644 --- a/includes/parsing.h +++ b/includes/parsing.h @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/12 12:49:16 by madumerg #+# #+# */ -/* Updated: 2024/09/12 16:58:29 by madumerg ### ########.fr */ +/* Updated: 2024/10/19 15:37:50 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/cub3d.c b/src/cub3d.c index f23f72d..0ab9ec3 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -6,13 +6,16 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */ -/* Updated: 2024/10/14 12:32:14 by adjoly ### ########.fr */ +/* Updated: 2024/10/24 12:51:06 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" #include "game/game.h" +#include "game/settings.h" +#include "libft.h" #include "mlx.h" +#include void *print_frame(t_cub *cub); @@ -20,26 +23,25 @@ int main(int ac, char **av) { t_cub cub; - cub.map = (char *[]){ - "1111111\0", - "1000101\0", - "1000101\0", - "1000001\0", - "1111111\0", - NULL - }; + 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; (void)ac; (void)av; cub.mlx = mlx_init(); cub.win = mlx_new_window(cub.mlx, WINDOW_X, WINDOW_Y, "WTF"); - cub.player.coords.x = 100; - cub.player.coords.y = 100; + cub.player.coords.x = 128; + cub.player.coords.y = 128; cub.player.direction = 0; cub.img = print_frame(&cub); - //get_player_image(&cub, 0); 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); return (0); diff --git a/src/raycasting/dda/dda.c b/src/raycasting/dda/dda.c index 3429678..0667d8a 100644 --- a/src/raycasting/dda/dda.c +++ b/src/raycasting/dda/dda.c @@ -6,24 +6,26 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/07 16:55:09 by adjoly #+# #+# */ -/* Updated: 2024/10/13 21:27:59 by adjoly ### ########.fr */ +/* Updated: 2024/10/23 15:04:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "game/dda.h" #include "game/settings.h" +#include "sys/types.h" +#include +#include uint16_t *dda(t_player *player, char **map) { t_dda dda[WINDOW_X / RAY_SIZE]; - t_coord map_coord; float *wall_dist; uint16_t *line_size; setup_dda(&dda, player); - map_coord.x = player->coords.x - player->coords.x % MAP_CHUNK_SIZE; - map_coord.y = player->coords.y - player->coords.y % MAP_CHUNK_SIZE; - while_dda(&dda, map, map_coord); + player->map_coords.x = player->coords.x - player->coords.x % MAP_CHUNK_SIZE; + player->map_coords.y = player->coords.y - player->coords.y % MAP_CHUNK_SIZE; + while_dda(&dda, map, player); wall_dist = dist_dda(dda); line_size = line_dda(wall_dist); return (line_size); diff --git a/src/raycasting/dda/dist_dda.c b/src/raycasting/dda/dist_dda.c index cbab6e2..0abfa19 100644 --- a/src/raycasting/dda/dist_dda.c +++ b/src/raycasting/dda/dist_dda.c @@ -6,12 +6,11 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/08 13:26:19 by adjoly #+# #+# */ -/* Updated: 2024/10/13 21:30:15 by adjoly ### ########.fr */ +/* Updated: 2024/10/23 14:10:52 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "game/dda.h" -#include #include #include "game/settings.h" #include "libft.h" @@ -22,17 +21,16 @@ float *dist_dda(t_dda dda[WINDOW_X / RAY_SIZE]) float *wall_dist; i = 0; - wall_dist = ft_calloc(WINDOW_X / RAY_SIZE, sizeof(float)); + wall_dist = ft_calloc(WINDOW_X / RAY_SIZE + 1, sizeof(float)); if (!wall_dist) return (NULL); - while (dda) + while (i < WINDOW_X / RAY_SIZE) { if (dda[i].wall_side == HORIZONTAL) wall_dist[i] = dda[i].side_dist.x - dda[i].delta_dist.x; else wall_dist[i] = dda[i].side_dist.y - dda[i].delta_dist.y; i++; - dda++; } return (wall_dist); } diff --git a/src/raycasting/dda/get_ray_angle.c b/src/raycasting/dda/get_ray_angle.c index e6f69c9..f2d7509 100644 --- a/src/raycasting/dda/get_ray_angle.c +++ b/src/raycasting/dda/get_ray_angle.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/29 15:26:29 by adjoly #+# #+# */ -/* Updated: 2024/10/14 12:35:17 by adjoly ### ########.fr */ +/* Updated: 2024/10/23 14:19:21 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,18 +16,17 @@ #include #include -void get_ray_angle(t_player *player, t_dda (*dda)[800]) +void get_ray_angle(t_player *player, t_dda (*dda)[WINDOW_X / RAY_SIZE]) { uint16_t x; float ray_angle; x = 0; - printf("plyaer dir = %f", player->direction); while (x < WINDOW_X) { - ray_angle = player->direction - (FOV / 2) + (x / WINDOW_X) * FOV; - dda[x / RAY_SIZE]->ray_dir.x = cos(ray_angle); - dda[x / RAY_SIZE]->ray_dir.y = sin(ray_angle); + ray_angle = (float)(player->direction - (FOV / 2) + ((float)x / WINDOW_X) * FOV); + (*dda)[x / RAY_SIZE].ray_dir.x = cos(ray_angle); + (*dda)[x / RAY_SIZE].ray_dir.y = sin(ray_angle); x += RAY_SIZE; } } diff --git a/src/raycasting/dda/line_dda.c b/src/raycasting/dda/line_dda.c index d4b89ba..76afae6 100644 --- a/src/raycasting/dda/line_dda.c +++ b/src/raycasting/dda/line_dda.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/13 17:27:44 by adjoly #+# #+# */ -/* Updated: 2024/10/13 21:28:12 by adjoly ### ########.fr */ +/* Updated: 2024/10/22 13:03:40 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ #include "game/dda.h" #include -#include uint16_t *line_dda(float *wall_dist) { uint16_t i; @@ -22,9 +21,9 @@ uint16_t *line_dda(float *wall_dist) i = 0; line_size = malloc(WINDOW_X / RAY_SIZE * sizeof(uint16_t)); - while (wall_dist[i]) + while (i < WINDOW_X / RAY_SIZE) { - line_size[i] = (WINDOW_Y / wall_dist[i]); + line_size[i] = WINDOW_Y * MAP_CHUNK_SIZE / wall_dist[i]; i++; } return (line_size); diff --git a/src/raycasting/dda/setup_dda.c b/src/raycasting/dda/setup_dda.c index 458b987..5c6467e 100644 --- a/src/raycasting/dda/setup_dda.c +++ b/src/raycasting/dda/setup_dda.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/30 23:47:14 by adjoly #+# #+# */ -/* Updated: 2024/10/08 13:28:24 by adjoly ### ########.fr */ +/* Updated: 2024/10/24 12:40:00 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,27 +15,28 @@ #include #include #include +#include -void get_side_dist(t_dda *dda, t_coord incell_coords, t_player *player) +void get_side_dist(t_dda *dda, t_player *player) { if (dda->ray_dir.x < 0) { - dda->side_dist.x = incell_coords.x * dda->delta_dist.x; + dda->side_dist.x = (player->coords.x - player->map_coords.x) * dda->delta_dist.x; dda->step.x = -MAP_CHUNK_SIZE; } else { - dda->side_dist.x = (((int)player->coords.x + MAP_CHUNK_SIZE) - player->coords.x) * dda->delta_dist.x; + dda->side_dist.x = ((player->map_coords.x + MAP_CHUNK_SIZE) - player->coords.x) * dda->delta_dist.x; dda->step.x = MAP_CHUNK_SIZE; } if (dda->ray_dir.y < 0) { - dda->side_dist.y = incell_coords.y * dda->delta_dist.y; + dda->side_dist.y = (player->coords.y - player->map_coords.y) * dda->delta_dist.y; dda->step.y = -MAP_CHUNK_SIZE; } else { - dda->side_dist.y = (((int)player->coords.y + MAP_CHUNK_SIZE) - player->coords.y) * dda->delta_dist.y; + dda->side_dist.y = (player->map_coords.y + MAP_CHUNK_SIZE - player->coords.y) * dda->delta_dist.y; dda->step.y = MAP_CHUNK_SIZE; } } @@ -43,17 +44,18 @@ void get_side_dist(t_dda *dda, t_coord incell_coords, t_player *player) void setup_dda(t_dda (*dda)[WINDOW_X / RAY_SIZE], t_player *player) { size_t i; - t_coord incell_coords; i = 0; get_ray_angle(player, dda); - incell_coords.x = player->coords.x % MAP_CHUNK_SIZE; - incell_coords.y = player->coords.y % MAP_CHUNK_SIZE; while (i < WINDOW_X / RAY_SIZE) { - dda[i]->delta_dist.x = abs((int)(MAP_CHUNK_SIZE / dda[i]->ray_dir.x)); - dda[i]->delta_dist.y = abs((int)(MAP_CHUNK_SIZE / dda[i]->ray_dir.y)); - get_side_dist(dda[i], incell_coords, player); + (*dda)[i].delta_dist.x = fabsf(MAP_CHUNK_SIZE / (*dda)[i].ray_dir.x); + (*dda)[i].delta_dist.y = fabsf(MAP_CHUNK_SIZE / (*dda)[i].ray_dir.y); + printf("%f \n", (*dda)[i].delta_dist.x); + printf("%f \n", (*dda)[i].delta_dist.y); + get_side_dist(&(*dda)[i], player); + printf("side %f \n", (*dda)[i].side_dist.x); + printf("side %f \n", (*dda)[i].side_dist.y); i++; } } diff --git a/src/raycasting/dda/while_dda.c b/src/raycasting/dda/while_dda.c index 2f71620..381d60e 100644 --- a/src/raycasting/dda/while_dda.c +++ b/src/raycasting/dda/while_dda.c @@ -6,30 +6,42 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/06 18:46:56 by adjoly #+# #+# */ -/* Updated: 2024/10/13 21:34:01 by adjoly ### ########.fr */ +/* Updated: 2024/10/23 15:03:03 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "game/dda.h" +#include "game/settings.h" +#include -void while_dda(t_dda (*dda)[WINDOW_X / RAY_SIZE], char **map, t_coord map_coord) +void while_dda(t_dda (*dda)[WINDOW_X / RAY_SIZE], char **map, t_player *player) { - (*dda)->wall_hit = false; - while (!(*dda)->wall_hit) + uint16_t i; + t_coord map_coord; + + i = 0; + while (i < WINDOW_X / RAY_SIZE) { - if (( *dda )->side_dist.x < (*dda)->side_dist.y) + (*dda)[i].wall_hit = false; + map_coord.x = player->map_coords.x; + map_coord.y = player->map_coords.y; + while (!(*dda)[i].wall_hit) { - (*dda)->side_dist.x += (*dda)->delta_dist.x; - map_coord.x += (*dda)->step.x; - (*dda)->wall_side = HORIZONTAL; + if ((*dda)[i].side_dist.x < (*dda)[i].side_dist.y) + { + (*dda)[i].side_dist.x += (*dda)[i].delta_dist.x; + map_coord.x += (*dda)[i].step.x; + (*dda)[i].wall_side = HORIZONTAL; + } + else + { + (*dda)[i].side_dist.y += (*dda)[i].delta_dist.y; + map_coord.y += (*dda)[i].step.y; + (*dda)[i].wall_side = VERTICAL; + } + if (map_coord.x < 0 || map_coord.y < 0 || map[map_coord.x / MAP_CHUNK_SIZE][map_coord.y / MAP_CHUNK_SIZE] == '1') + (*dda)[i].wall_hit = true; } - else - { - (*dda)->side_dist.y += (*dda)->delta_dist.y; - map_coord.y += (*dda)->step.y; - (*dda)->wall_side = VERTICAL; - } - if (map[map_coord.x][map_coord.y] == '1') - (*dda)->wall_hit = true; + i++; } } diff --git a/src/raycasting/get_player_image.c b/src/raycasting/get_player_image.c index 2c0506b..08c7fa1 100644 --- a/src/raycasting/get_player_image.c +++ b/src/raycasting/get_player_image.c @@ -6,11 +6,13 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/13 11:05:02 by adjoly #+# #+# */ -/* Updated: 2024/10/05 14:30:08 by adjoly ### ########.fr */ +/* 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) @@ -20,27 +22,13 @@ void change_direction(double speed, bool clockwise, t_player *player) else player->direction += speed; if (player->direction >= 2 * M_PI) - player->direction -= 2 * M_PI; + player->direction = 0; else if (player->direction < 0) - player->direction += 2 * M_PI; + player->direction = 2 * M_PI; } -void get_player_image(t_cub *cub, uint8_t key_pressed) +void get_player_image(t_cub *cub) { - if (key_pressed == W_KEY) - { - cub->player.coords.x += PLAYER_SPEED * cos(cub->player.direction); - cub->player.coords.y += PLAYER_SPEED * sin(cub->player.direction); - } - else if (key_pressed == S_KEY) - { - cub->player.coords.x -= PLAYER_SPEED * cos(cub->player.direction); - cub->player.coords.y -= PLAYER_SPEED * sin(cub->player.direction); - } - else if (key_pressed == D_KEY) - change_direction(PLAYER_ROT_SPEED, false, &cub->player); - else if (key_pressed == A_KEY) - change_direction(PLAYER_ROT_SPEED, true, &cub->player); 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 34ae44e..153cf85 100644 --- a/src/raycasting/key_hook.c +++ b/src/raycasting/key_hook.c @@ -6,15 +6,15 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/11 16:37:56 by adjoly #+# #+# */ -/* Updated: 2024/10/04 15:29:00 by adjoly ### ########.fr */ +/* Updated: 2024/10/24 11:41:30 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include #include "mlx.h" -#include "parsing.h" #include "game/game.h" +void *print_frame(t_cub *cub); + int key_hook(int key, void *param) { t_cub *cub; @@ -24,15 +24,25 @@ int key_hook(int key, void *param) if (key == ESCAPE_KEY) { mlx_loop_end(cub->mlx); - mlx_destroy_image(cub->mlx, cub->img); + return (0); } - if (key == W_KEY || key == S_KEY || key == D_KEY || key == A_KEY) + if (key == W_KEY) { - mlx_clear_window(cub->mlx, cub->win); - mlx_destroy_image(cub->mlx, cub->img); - cub->img = mlx_new_image(cub->mlx, WINDOW_X, WINDOW_Y); - get_player_image(cub, key); - mlx_put_image_to_window(cub->mlx, cub->win, cub->img, 0, 0); + cub->player.coords.x += PLAYER_SPEED * cos(cub->player.direction); + cub->player.coords.y += PLAYER_SPEED * sin(cub->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); + } + else if (key == D_KEY) + change_direction(PLAYER_ROT_SPEED, false, &cub->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); return (0); } diff --git a/src/raycasting/print_frame.c b/src/raycasting/print_frame.c index 18daed2..5a562d0 100644 --- a/src/raycasting/print_frame.c +++ b/src/raycasting/print_frame.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/13 21:01:53 by adjoly #+# #+# */ -/* Updated: 2024/10/13 21:37:17 by adjoly ### ########.fr */ +/* Updated: 2024/10/23 11:19:58 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,12 +36,12 @@ void draw_rectangle(t_cub *cub, void *img, t_coord start, t_coord end) void *print_frame(t_cub *cub) { uint16_t *lines; - int start; - int stop; - size_t i; - void *img; + int start; + int stop; + size_t i; + void *img; - lines = dda(&cub->player, cub->map); + lines = dda(&(cub->player), cub->map); img = mlx_new_image(cub->mlx, WINDOW_X, WINDOW_Y); i = 0; while (i < WINDOW_X / RAY_SIZE) @@ -52,7 +52,7 @@ void *print_frame(t_cub *cub) 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}); + draw_rectangle(cub, img, (t_coord){i * RAY_SIZE, start}, (t_coord){i * RAY_SIZE + RAY_SIZE + 1, stop}); i++; } return (img);