From 45622cf6472b206287cd1c1b492b871fffa3f202 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Fri, 29 Mar 2024 18:35:16 +0100 Subject: [PATCH] fix: input * Now it never misses any press on a key --- includes/env.h | 8 ++---- includes/input.h | 4 +-- src/input.c | 63 +++++++++++++++++++++++++++++++++++------------ src/main.c | 4 +-- src/object_snas.c | 10 ++++---- 5 files changed, 58 insertions(+), 31 deletions(-) diff --git a/includes/env.h b/includes/env.h index 7f503c4..94ae80b 100644 --- a/includes/env.h +++ b/includes/env.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/26 15:26:13 by mcolonna #+# #+# */ -/* Updated: 2024/03/28 12:50:14 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 18:27:02 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,11 +28,7 @@ typedef struct s_env void *mlx; void *win; t_room room; - - bool up; - bool down; - bool left; - bool right; + bool input[4]; } t_env; extern t_env g_env; diff --git a/includes/input.h b/includes/input.h index 0070580..b5c16b7 100644 --- a/includes/input.h +++ b/includes/input.h @@ -6,15 +6,15 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/26 12:16:32 by mcolonna #+# #+# */ -/* Updated: 2024/03/26 13:26:27 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 18:32:38 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef INPUT_H # define INPUT_H -void input_init(void); int key_press_hook(int keycode); int key_release_hook(int keycode); +void input_loop(void); #endif diff --git a/src/input.c b/src/input.c index 3ffe37a..060892e 100644 --- a/src/input.c +++ b/src/input.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/26 12:26:17 by mcolonna #+# #+# */ -/* Updated: 2024/03/26 13:36:33 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 18:32:27 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,26 +15,43 @@ #include "env.h" #include -void input_init(void) +static int g_input_i = 0; +static bool g_to_unpress[4] = {false, false, false, false}; +static bool g_inputbuf[4] = {false, false, false, false}; + +static void set_direction(t_direction direction, bool value) { - g_env.up = false; - g_env.down = false; - g_env.left = false; - g_env.right = false; + static int last_press[4] = {0, 0, 0, 0}; + + if (value) + { + last_press[direction] = g_input_i; + g_to_unpress[direction] = false; + } + if (!value && last_press[direction] == g_input_i) + { + g_to_unpress[direction] = true; + return ; + } + g_inputbuf[direction] = value; } static void setkey(int keycode, bool value) { - if (keycode == XK_Up || keycode == XK_Z || keycode == XK_z - || keycode == XK_W || keycode == XK_w) - g_env.up = value; - if (keycode == XK_Down || keycode == XK_S || keycode == XK_s) - g_env.down = value; - if (keycode == XK_Left || keycode == XK_Q || keycode == XK_q - || keycode == XK_A || keycode == XK_a) - g_env.left = value; - if (keycode == XK_Right || keycode == XK_D || keycode == XK_d) - g_env.right = value; + static const int keymap[] = { + XK_Down, XK_S, XK_s, 0, + XK_Up, XK_Z, XK_z, XK_W, XK_w, 0, + XK_Left, XK_Q, XK_q, XK_A, XK_a, 0, + XK_Right, XK_D, XK_d, 0}; + t_direction direction; + int i; + + direction = -1; + i = -1; + while (++direction < 4) + while (keymap[++i]) + if (keymap[i] == keycode) + return (set_direction(direction, value)); } int key_press_hook(int keycode) @@ -48,3 +65,17 @@ int key_release_hook(int keycode) setkey(keycode, false); return (0); } + +void input_loop(void) +{ + t_direction direction; + + g_input_i++; + direction = -1; + while (++direction < 4) + { + g_env.input[direction] = g_inputbuf[direction]; + if (g_to_unpress[direction]) + set_direction(direction, false); + } +} diff --git a/src/main.c b/src/main.c index 63afb0c..a378179 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */ -/* Updated: 2024/03/26 16:42:18 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 18:33:10 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,7 @@ int close_hook(void) static void loop(void) { + input_loop(); room_loop(g_env.room); display_erase(); room_draw(g_env.room); @@ -64,7 +65,6 @@ int main(void) mlx_hook(g_env.win, DestroyNotify, StructureNotifyMask, close_hook, NULL); mlx_loop_hook(g_env.mlx, loop_hook, NULL); mlx_do_key_autorepeatoff(g_env.mlx); - input_init(); mlx_hook(g_env.win, KeyPress, KeyPressMask, key_press_hook, NULL); mlx_hook(g_env.win, KeyRelease, KeyReleaseMask, key_release_hook, NULL); mlx_loop(g_env.mlx); diff --git a/src/object_snas.c b/src/object_snas.c index ca29185..d9805ef 100644 --- a/src/object_snas.c +++ b/src/object_snas.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/15 15:27:03 by mcolonna #+# #+# */ -/* Updated: 2024/03/29 15:52:13 by mcolonna ### ########.fr */ +/* Updated: 2024/03/29 18:29:12 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,13 +18,13 @@ static t_direction snas_brain(void) { - if (g_env.up && !g_env.down) + if (g_env.input[UP] && !g_env.input[DOWN]) return (UP); - if (g_env.down && !g_env.up) + if (g_env.input[DOWN] && !g_env.input[UP]) return (DOWN); - if (g_env.left && !g_env.right) + if (g_env.input[LEFT] && !g_env.input[RIGHT]) return (LEFT); - if (g_env.right && !g_env.left) + if (g_env.input[RIGHT] && !g_env.input[LEFT]) return (RIGHT); return (NO_DIRECTION); }