This commit is contained in:
Etienne Rey-bethbeder 2023-06-15 17:21:07 +02:00
parent 4bd265e213
commit 6cd5b54918
4 changed files with 42 additions and 36 deletions

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/11 14:41:48 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; int y;
y = -1; y = 0;
while (++y < dda->draw_start) while (y < dda->draw_start && y >= 0 && y < HEIGHT)
mlx_put_pixel(game->window, x, y, game->map.color_bot); mlx_put_pixel(game->window, x, y++, game->map.color_bot);
draw_texture(game, dda, x); draw_texture(game, dda, x);
y = dda->draw_end - 1; y = dda->draw_end;
while (++y < HEIGHT && y > 0) while (y < HEIGHT && y >= 0)
mlx_put_pixel(game->window, x, y, game->map.color_top); mlx_put_pixel(game->window, x, y++, game->map.color_top);
} }

View File

@ -6,13 +6,13 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:59:53 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" #include "game.h"
static int destroy(t_game *game) static void destroy(t_game *game)
{ {
if (game->textures[0]) if (game->textures[0])
{ {
@ -32,17 +32,13 @@ static int destroy(t_game *game)
} }
} }
} }
return (1);
} }
#include <stdio.h>
static void hook(void *param) static void hook(void *param)
{ {
t_game *game; t_game *game;
game = param; game = param;
//printf("%d\n", mlx_is_key_down(game->mlx, MLX_KEY_ESCAPE));
if (manage_keys(game)) if (manage_keys(game))
raycasting(game); raycasting(game);
} }
@ -52,17 +48,22 @@ int start_game(t_map map)
t_game game; t_game game;
if (init(map, &game)) if (init(map, &game))
return (destroy(&game)); {
destroy(&game);
return (1);
}
game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT); game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT);
if (!game.window) if (!game.window)
{ {
mlx_terminate(game.mlx); mlx_terminate(game.mlx);
return (destroy(&game)); destroy(&game);
return (1);
} }
if (mlx_image_to_window(game.mlx, game.window, 0, 0) == -1) if (mlx_image_to_window(game.mlx, game.window, 0, 0) == -1)
{ {
mlx_terminate(game.mlx); mlx_terminate(game.mlx);
return (destroy(&game)); destroy(&game);
return (1);
} }
raycasting(&game); raycasting(&game);
mlx_loop_hook(game.mlx, &hook, &game); mlx_loop_hook(game.mlx, &hook, &game);

View File

@ -6,13 +6,13 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:14:51 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" #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_x;
double old_dir_y; 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->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_x = old_plane_x * cos(speed) - old_plane_y * sin(speed);
ply->pla_y = old_plane_x * sin(speed) + old_plane_y * cos(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_x += x;
ply->pos_y += y; ply->pos_y += y;
return (1);
} }
int manage_movement(t_game *game) int manage_movement(t_game *game)
{ {
t_ply *ply; t_ply *ply;
int is_moving; int is_moving;
ply = &game->ply; ply = &game->ply;
is_moving = 1; is_moving = 0;
if (mlx_is_key_down(game->mlx, MLX_KEY_W)) if (mlx_is_key_down(game->mlx, MLX_KEY_W))
movement(ply, (ply->dir_x * MOVESPEED), (ply->dir_y * MOVESPEED)); is_moving = movement(ply, (ply->dir_x * MOVESPEED),
else if (mlx_is_key_down(game->mlx, MLX_KEY_S)) (ply->dir_y * MOVESPEED));
movement(ply, -(ply->dir_x * MOVESPEED), -(ply->dir_y * MOVESPEED)); if (mlx_is_key_down(game->mlx, MLX_KEY_S))
else if (mlx_is_key_down(game->mlx, MLX_KEY_D)) is_moving = movement(ply, -(ply->dir_x * MOVESPEED),
movement(ply, (ply->pla_x * MOVESPEED), (ply->pla_y * MOVESPEED)); -(ply->dir_y * MOVESPEED));
else if (mlx_is_key_down(game->mlx, MLX_KEY_A)) if (mlx_is_key_down(game->mlx, MLX_KEY_D))
movement(ply, -(ply->pla_x * MOVESPEED), -(ply->pla_y * MOVESPEED)); is_moving = movement(ply, (ply->pla_x * MOVESPEED),
else if (mlx_is_key_down(game->mlx, MLX_KEY_RIGHT)) (ply->pla_y * MOVESPEED));
rotate(ply, true); if (mlx_is_key_down(game->mlx, MLX_KEY_A))
else if (mlx_is_key_down(game->mlx, MLX_KEY_LEFT)) is_moving = movement(ply, -(ply->pla_x * MOVESPEED),
rotate(ply, false); -(ply->pla_y * MOVESPEED));
else if (mlx_is_key_down(game->mlx, MLX_KEY_RIGHT)
is_moving = 0; || 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); return (is_moving);
} }
int manage_keys(t_game *game) int manage_keys(t_game *game)
{ {
if (mlx_is_key_down(game->mlx, MLX_KEY_ESCAPE)) if (mlx_is_key_down(game->mlx, MLX_KEY_ESCAPE))
mlx_close_window(game->mlx); mlx_close_window(game->mlx);
if (manage_movement(game)) if (manage_movement(game))

View File

@ -12,7 +12,7 @@ C 225,30,0
101100000111000000000000001 101100000111000000000000001
101100000111000000000000001 1111 101100000111000000000000001 1111
1011000001110000000000000011111100001 1011000001110000000000000011111100001
101100000111000000000000000000000W001 101100000111000000000000000000000N001
1011000001110000000000000011111100001 1011000001110000000000000011111100001
101100000111000000000000001 10101 101100000111000000000000001 10101
101100000111000000000000001 11111 101100000111000000000000001 11111