From 8ad8eb1fbcf2915517354eb6b5f807151e063b10 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Thu, 7 Nov 2024 14:27:17 +0100 Subject: [PATCH] handle cross click --- include/input.h | 8 +++++--- src/input.c | 16 +++++++++++++--- src/main.c | 6 ++---- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/include/input.h b/include/input.h index ee61d05..6887ad7 100644 --- a/include/input.h +++ b/include/input.h @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:43:09 by mcolonna #+# #+# */ -/* Updated: 2024/11/01 20:59:28 by mc ### ########.fr */ +/* Updated: 2024/11/07 14:26:28 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,9 @@ int hook_keypress(int keycode); /// @return Unused. int hook_keyrelease(int keycode); -/// @brief To call at the start of the program. -void input_init(void); +/// @brief To handle the input of a window. +/// +/// @param win_ptr Reference to the window to use. +void input_init(void *win_ptr); #endif diff --git a/src/input.c b/src/input.c index 0897563..2d09381 100644 --- a/src/input.c +++ b/src/input.c @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:43:36 by mcolonna #+# #+# */ -/* Updated: 2024/11/01 20:59:32 by mc ### ########.fr */ +/* Updated: 2024/11/07 14:25:46 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,13 @@ t_input_actions g_input_actions; +static int set_quit(void *_) +{ + (void)_; + g_input_actions.quit = true; + return (0); +} + static void set_action(int keycode, bool value) { if (keycode == XK_Up || keycode == XK_z || keycode == XK_w) @@ -25,7 +32,7 @@ static void set_action(int keycode, bool value) if (keycode == XK_Left || keycode == XK_q || keycode == XK_a) g_input_actions.left = value; if (keycode == XK_Escape) - g_input_actions.quit = value; + set_quit(NULL); } int hook_keypress(int keycode) @@ -40,7 +47,10 @@ int hook_keyrelease(int keycode) return (0); } -void input_init(void) +void input_init(void *win_ptr) { ft_memset(&g_input_actions, 0, sizeof(g_input_actions)); + mlx_hook(win_ptr, DestroyNotify, StructureNotifyMask, set_quit, NULL); + mlx_hook(win_ptr, KeyPress, KeyPressMask, hook_keypress, NULL); + mlx_hook(win_ptr, KeyRelease, KeyReleaseMask, hook_keyrelease, NULL); } diff --git a/src/main.c b/src/main.c index 30db71a..23ab3c1 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 16:57:40 by mc #+# #+# */ -/* Updated: 2024/11/07 00:07:35 by mc ### ########.fr */ +/* Updated: 2024/11/07 14:25:29 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,7 +53,6 @@ static int loop_hook(void *param) int main(int argc, char *argv[]) { g_mlx = mlx_init(); - input_init(); if (argc != 2) { printf("Syntax: %s \n", argv[0]); @@ -62,8 +61,7 @@ int main(int argc, char *argv[]) if (!map_from_file(&g_map, argv[1])) return (1); g_win = mlx_new_window(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT, "cub3d"); - mlx_hook(g_win, KeyPress, KeyPressMask, hook_keypress, NULL); - mlx_hook(g_win, KeyRelease, KeyReleaseMask, hook_keyrelease, NULL); + input_init(g_win); mlx_loop_hook(g_mlx, loop_hook, NULL); draw_screen(); mlx_loop(g_mlx);