From 6cd5b549187602ff33cbae15c0783c4f7f900ee0 Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Thu, 15 Jun 2023 17:21:07 +0200 Subject: [PATCH] Finish --- game/draw.c | 14 +++++++------- game/game.c | 19 ++++++++++--------- game/manage_keys.c | 43 ++++++++++++++++++++++++------------------- maps/test1.cub | 2 +- 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/game/draw.c b/game/draw.c index d7ad6aa..58f76b8 100644 --- a/game/draw.c +++ b/game/draw.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/05/11 14:41:48 by erey-bet #+# #+# */ -/* Updated: 2023/05/11 16:05:42 by erey-bet ### ########.fr */ +/* Updated: 2023/06/15 12:52:23 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,11 +44,11 @@ void draw(t_game *game, t_dda *dda, int x) { int y; - y = -1; - while (++y < dda->draw_start) - mlx_put_pixel(game->window, x, y, game->map.color_bot); + y = 0; + while (y < dda->draw_start && y >= 0 && y < HEIGHT) + mlx_put_pixel(game->window, x, y++, game->map.color_bot); draw_texture(game, dda, x); - y = dda->draw_end - 1; - while (++y < HEIGHT && y > 0) - mlx_put_pixel(game->window, x, y, game->map.color_top); + y = dda->draw_end; + while (y < HEIGHT && y >= 0) + mlx_put_pixel(game->window, x, y++, game->map.color_top); } diff --git a/game/game.c b/game/game.c index 34fc845..9feea32 100644 --- a/game/game.c +++ b/game/game.c @@ -6,13 +6,13 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */ -/* Updated: 2023/06/13 14:19:08 by erey-bet ### ########.fr */ +/* Updated: 2023/06/15 12:40:32 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "game.h" -static int destroy(t_game *game) +static void destroy(t_game *game) { if (game->textures[0]) { @@ -32,17 +32,13 @@ static int destroy(t_game *game) } } } - return (1); } -#include - static void hook(void *param) { t_game *game; game = param; - //printf("%d\n", mlx_is_key_down(game->mlx, MLX_KEY_ESCAPE)); if (manage_keys(game)) raycasting(game); } @@ -52,17 +48,22 @@ int start_game(t_map map) t_game game; if (init(map, &game)) - return (destroy(&game)); + { + destroy(&game); + return (1); + } game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT); if (!game.window) { mlx_terminate(game.mlx); - return (destroy(&game)); + destroy(&game); + return (1); } if (mlx_image_to_window(game.mlx, game.window, 0, 0) == -1) { mlx_terminate(game.mlx); - return (destroy(&game)); + destroy(&game); + return (1); } raycasting(&game); mlx_loop_hook(game.mlx, &hook, &game); diff --git a/game/manage_keys.c b/game/manage_keys.c index 9ad817e..af73cff 100644 --- a/game/manage_keys.c +++ b/game/manage_keys.c @@ -6,13 +6,13 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */ -/* Updated: 2023/06/13 13:28:58 by erey-bet ### ########.fr */ +/* Updated: 2023/06/15 13:00:31 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "game.h" -void rotate(t_ply *ply, bool right) +static int rotate(t_ply *ply, bool right) { double old_dir_x; double old_dir_y; @@ -31,41 +31,46 @@ void rotate(t_ply *ply, bool right) ply->dir_y = old_dir_x * sin(speed) + old_dir_y * cos(speed); ply->pla_x = old_plane_x * cos(speed) - old_plane_y * sin(speed); ply->pla_y = old_plane_x * sin(speed) + old_plane_y * cos(speed); + return (1); } -void movement(t_ply *ply, double x, double y) +static int movement(t_ply *ply, double x, double y) { ply->pos_x += x; ply->pos_y += y; + return (1); } int manage_movement(t_game *game) { t_ply *ply; - int is_moving; + int is_moving; ply = &game->ply; - is_moving = 1; + is_moving = 0; if (mlx_is_key_down(game->mlx, MLX_KEY_W)) - movement(ply, (ply->dir_x * MOVESPEED), (ply->dir_y * MOVESPEED)); - else if (mlx_is_key_down(game->mlx, MLX_KEY_S)) - movement(ply, -(ply->dir_x * MOVESPEED), -(ply->dir_y * MOVESPEED)); - else if (mlx_is_key_down(game->mlx, MLX_KEY_D)) - movement(ply, (ply->pla_x * MOVESPEED), (ply->pla_y * MOVESPEED)); - else if (mlx_is_key_down(game->mlx, MLX_KEY_A)) - movement(ply, -(ply->pla_x * MOVESPEED), -(ply->pla_y * MOVESPEED)); - else if (mlx_is_key_down(game->mlx, MLX_KEY_RIGHT)) - rotate(ply, true); - else if (mlx_is_key_down(game->mlx, MLX_KEY_LEFT)) - rotate(ply, false); - else - is_moving = 0; + is_moving = movement(ply, (ply->dir_x * MOVESPEED), + (ply->dir_y * MOVESPEED)); + if (mlx_is_key_down(game->mlx, MLX_KEY_S)) + is_moving = movement(ply, -(ply->dir_x * MOVESPEED), + -(ply->dir_y * MOVESPEED)); + if (mlx_is_key_down(game->mlx, MLX_KEY_D)) + is_moving = movement(ply, (ply->pla_x * MOVESPEED), + (ply->pla_y * MOVESPEED)); + if (mlx_is_key_down(game->mlx, MLX_KEY_A)) + is_moving = movement(ply, -(ply->pla_x * MOVESPEED), + -(ply->pla_y * MOVESPEED)); + if (mlx_is_key_down(game->mlx, MLX_KEY_RIGHT) + || mlx_is_key_down(game->mlx, MLX_KEY_L)) + is_moving = rotate(ply, true); + else if (mlx_is_key_down(game->mlx, MLX_KEY_LEFT) + || mlx_is_key_down(game->mlx, MLX_KEY_J)) + is_moving = rotate(ply, false); return (is_moving); } int manage_keys(t_game *game) { - if (mlx_is_key_down(game->mlx, MLX_KEY_ESCAPE)) mlx_close_window(game->mlx); if (manage_movement(game)) diff --git a/maps/test1.cub b/maps/test1.cub index d9eb364..7f50b7d 100644 --- a/maps/test1.cub +++ b/maps/test1.cub @@ -12,7 +12,7 @@ C 225,30,0 101100000111000000000000001 101100000111000000000000001 1111 1011000001110000000000000011111100001 - 101100000111000000000000000000000W001 + 101100000111000000000000000000000N001 1011000001110000000000000011111100001 101100000111000000000000001 10101 101100000111000000000000001 11111