fix: input *

Now it never misses any press on a key
This commit is contained in:
mcolonna 2024-03-29 18:35:16 +01:00
parent a7e8cdc0c1
commit 45622cf647
5 changed files with 58 additions and 31 deletions

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -6,15 +6,15 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <X11/keysym.h>
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);
}
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}