fix(hooks): Reworked keypresses events

Keypresses (along with keyreleases) events are now using booleans, a
function in the game loop will apply various changes as the key are
pressed. By default every boolean are set to false
This commit is contained in:
Theo Champion 2025-07-29 12:31:05 +02:00
parent fa3cb8da4a
commit 1626b1d9da
4 changed files with 43 additions and 27 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */ /* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */
/* Updated: 2025/07/17 15:54:29 by kcolin ### ########.fr */ /* Updated: 2025/07/29 12:26:32 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,7 @@
# include "map/mapdata.h" # include "map/mapdata.h"
# include "draw/img_data.h" # include "draw/img_data.h"
# include "player/player.h" # include "player/player.h"
#include "utils/hooks.h"
typedef struct s_cub3d_data typedef struct s_cub3d_data
{ {
@ -24,6 +25,7 @@ typedef struct s_cub3d_data
t_img_data *img_data; t_img_data *img_data;
t_mapdata *map; t_mapdata *map;
t_player player; t_player player;
t_keypresses keypresses;
} t_cub3d_data; } t_cub3d_data;
#endif // CUB3D_DATA_H #endif // CUB3D_DATA_H

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/07/28 17:18:38 by tchampio ### ########.fr */ /* Updated: 2025/07/29 12:33:46 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -162,6 +162,7 @@ int main(int argc, char **argv)
if (argc < 2) if (argc < 2)
return (ft_printf("Error: Missing cub3d file\n"), 1); return (ft_printf("Error: Missing cub3d file\n"), 1);
ft_bzero(&data, sizeof(data));
data.map = ft_calloc(sizeof(t_mapdata), 1); data.map = ft_calloc(sizeof(t_mapdata), 1);
if (!check_cubfile(argv[1], data.map)) if (!check_cubfile(argv[1], data.map))
return (ft_printf("Error: Wrong map file. Reason: %s\n", return (ft_printf("Error: Wrong map file. Reason: %s\n",

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */ /* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */
/* Updated: 2025/07/25 14:55:54 by tchampio ### ########.fr */ /* Updated: 2025/07/29 12:28:54 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,31 +22,34 @@ int keypress_handler(int keycode, t_cub3d_data *data)
{ {
if (keycode == XK_Escape) if (keycode == XK_Escape)
destroy(data); destroy(data);
if (keycode == XK_w)
data->keypresses.is_w_pressed = true;
if (keycode == XK_a)
data->keypresses.is_a_pressed = true;
if (keycode == XK_s)
data->keypresses.is_s_pressed = true;
if (keycode == XK_d)
data->keypresses.is_d_pressed = true;
if (keycode == XK_Left) if (keycode == XK_Left)
data->player.yaw += ROTATION_SPEED; data->keypresses.is_left_pressed = true;
else if (keycode == XK_Right) if (keycode == XK_Right)
data->player.yaw -= ROTATION_SPEED; data->keypresses.is_right_pressed = true;
if (data->player.yaw > 2 * M_PI)
data->player.yaw -= 2 * M_PI;
if (data->player.yaw < 0)
data->player.yaw += 2 * M_PI;
if (keycode == XK_a || keycode == XK_d
|| keycode == XK_w || keycode == XK_s)
apply_angle(keycode, data);
return (0); return (0);
} }
int keyrelease_handler(int keycode, t_cub3d_data *data) int keyrelease_handler(int keycode, t_cub3d_data *data)
{ {
if (keycode == XK_Escape)
destroy(data);
if (keycode == XK_a)
data->player.movement.x = 0;
if (keycode == XK_d)
data->player.movement.x = 0;
if (keycode == XK_w) if (keycode == XK_w)
data->player.movement.y = 0; data->keypresses.is_w_pressed = false;
if (keycode == XK_a)
data->keypresses.is_a_pressed = false;
if (keycode == XK_s) if (keycode == XK_s)
data->player.movement.y = 0; data->keypresses.is_s_pressed = false;
if (keycode == XK_d)
data->keypresses.is_d_pressed = false;
if (keycode == XK_Left)
data->keypresses.is_left_pressed = false;
if (keycode == XK_Right)
data->keypresses.is_right_pressed = false;
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:23:06 by kcolin #+# #+# */ /* Created: 2025/07/17 14:23:06 by kcolin #+# #+# */
/* Updated: 2025/07/17 15:50:08 by kcolin ### ########.fr */ /* Updated: 2025/07/29 12:26:04 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,16 @@
# include "../cub3d_data.h" # include "../cub3d_data.h"
typedef struct s_keypresses
{
bool is_w_pressed;
bool is_a_pressed;
bool is_s_pressed;
bool is_d_pressed;
bool is_left_pressed;
bool is_right_pressed;
} t_keypresses;
int keypress_handler(int keycode, t_cub3d_data *data); int keypress_handler(int keycode, t_cub3d_data *data);
int keyrelease_handler(int keycode, t_cub3d_data *data); int keyrelease_handler(int keycode, t_cub3d_data *data);