diff --git a/Makefile b/Makefile index 54f8490..f39b581 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/includes/env.h b/includes/env.h index 9f450cc..cc5b7b1 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/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; diff --git a/includes/input.h b/includes/input.h new file mode 100644 index 0000000..0070580 --- /dev/null +++ b/includes/input.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* input.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..3ffe37a --- /dev/null +++ b/src/input.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* input.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +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); +} diff --git a/src/main.c b/src/main.c index cb193df..c4462a3 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/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 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); }