feat: input handler

This commit is contained in:
mcolonna 2024-03-26 13:38:30 +01:00
parent 3abc6cbfd6
commit 78a6bbbb68
5 changed files with 87 additions and 4 deletions

View file

@ -2,7 +2,7 @@
NAME = so_long
SRCS = src/
INCLUDES = includes/
CODE = main error env \
CODE = main error env input \
data_assets data_sprites \
display1 display2 display_utils sprite \
room room_utils object

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:26:13 by mcolonna #+# #+# */
/* Updated: 2024/03/08 13:54:52 by mcolonna ### ########.fr */
/* Updated: 2024/03/26 13:20:06 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,6 +26,11 @@ typedef struct s_env
void *mlx;
void *win;
t_room room;
bool up;
bool down;
bool left;
bool right;
} t_env;
extern t_env g_env;

20
includes/input.h Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* input.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 12:16:32 by mcolonna #+# #+# */
/* Updated: 2024/03/26 13:26:27 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);
#endif

50
src/input.c Normal file
View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 12:26:17 by mcolonna #+# #+# */
/* Updated: 2024/03/26 13:36:33 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "error.h"
#include "libtf.h"
#include "env.h"
#include <X11/keysym.h>
void input_init(void)
{
g_env.up = false;
g_env.down = false;
g_env.left = false;
g_env.right = false;
}
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;
}
int key_press_hook(int keycode)
{
setkey(keycode, true);
return (0);
}
int key_release_hook(int keycode)
{
setkey(keycode, false);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 15:28:34 by mcolonna #+# #+# */
/* Updated: 2024/03/11 15:50:28 by mcolonna ### ########.fr */
/* Updated: 2024/03/26 13:26:58 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,8 @@
#include "display.h"
#include "data_assets.h"
#include "room.h"
#include "input.h"
#include <X11/Xlib.h>
int expose_hook(void)
{
@ -25,12 +27,14 @@ int expose_hook(void)
int close_hook(void)
{
mlx_do_key_autorepeaton(g_env.mlx);
finish();
return (0);
}
int loop_hook(void)
{
tf_printf("%i %i %i %i\n", g_env.up, g_env.down, g_env.left, g_env.right);
return (0);
}
@ -48,7 +52,11 @@ int main(void)
g_env.room = room_fromfile("room/room.ber");
room_draw(g_env.room);
mlx_expose_hook(g_env.win, expose_hook, NULL);
mlx_hook(g_env.win, 17, 1L << 0, close_hook, NULL);
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);
}