diff options
| author | Dominik Kaiser | 2024-05-10 12:12:24 +0200 |
|---|---|---|
| committer | Dominik Kaiser | 2024-05-10 12:12:24 +0200 |
| commit | b114117e5f62761dfecd618432a44e75263a929c (patch) | |
| tree | 28cde7c27e8698e22110c4806e472ba2ea3311f7 | |
| parent | 546c596505ded408bd0a73343618b100013d310f (diff) | |
| download | so_long-b114117e5f62761dfecd618432a44e75263a929c.tar.gz so_long-b114117e5f62761dfecd618432a44e75263a929c.zip | |
Change input handling to MLX42 approach
| -rw-r--r-- | include/so_long.h | 5 | ||||
| -rw-r--r-- | src/init.c | 4 | ||||
| -rw-r--r-- | src/input.c | 53 |
3 files changed, 26 insertions, 36 deletions
diff --git a/include/so_long.h b/include/so_long.h index e4e4f57..b2bf7d7 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 14:14:02 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 11:24:42 by dkaiser ### ########.fr */ +/* Updated: 2024/05/10 12:11:11 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,6 @@ typedef struct s_game int init(t_game *game); void loop(void *game); int draw(t_game *game); -void on_key_down(mlx_key_data_t keydata, void *params); -int on_key_up(int keycode, t_game *game); +void on_key_input(mlx_key_data_t event, void *params); #endif // SO_LONG_H @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 14:50:09 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 11:49:11 by dkaiser ### ########.fr */ +/* Updated: 2024/05/10 12:11:01 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ int init(t_game *game) static void init_hooks(t_game *game) { mlx_loop_hook(game->mlx, loop, game); - mlx_key_hook(game->mlx, on_key_down, game); + mlx_key_hook(game->mlx, on_key_input, game); /* mlx_hook(game->window, 2, 0, on_key_down, game); */ /* mlx_hook(game->window, 3, 0, on_key_up, game); */ } diff --git a/src/input.c b/src/input.c index 7582226..40c5670 100644 --- a/src/input.c +++ b/src/input.c @@ -6,46 +6,37 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 15:37:40 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 11:33:09 by dkaiser ### ########.fr */ +/* Updated: 2024/05/10 12:11:48 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -void on_key_down(mlx_key_data_t keydata, void *params) +void on_key_input(mlx_key_data_t event, void *params) { t_game *game; - int keycode; - - keycode = keydata.os_key; game = (t_game *) params; - - if (keydata.action == MLX_RELEASE) + if (event.action == MLX_PRESS) { - on_key_up(keycode, game); - return; + if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP) + game->input_direction |= UP; + else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT) + game->input_direction |= LEFT; + else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN) + game->input_direction |= DOWN; + else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT) + game->input_direction |= RIGHT; + } + else if (event.action == MLX_RELEASE) + { + if (event.key == MLX_KEY_W || event.key == MLX_KEY_UP) + game->input_direction &= ~UP; + else if (event.key == MLX_KEY_A || event.key == MLX_KEY_LEFT) + game->input_direction &= ~LEFT; + else if (event.key == MLX_KEY_S || event.key == MLX_KEY_DOWN) + game->input_direction &= ~DOWN; + else if (event.key == MLX_KEY_D || event.key == MLX_KEY_RIGHT) + game->input_direction &= ~RIGHT; } - - if (keycode == 13 || keycode == 126) - game->input_direction |= UP; - else if (keycode == 0 || keycode == 123) - game->input_direction |= LEFT; - else if (keycode == 1 || keycode == 125) - game->input_direction |= DOWN; - else if (keycode == 2 || keycode == 124) - game->input_direction |= RIGHT; -} - -int on_key_up(int keycode, t_game *game) -{ - if (keycode == 13 || keycode == 126) - game->input_direction &= ~UP; - else if (keycode == 0 || keycode == 123) - game->input_direction &= ~LEFT; - else if (keycode == 1 || keycode == 125) - game->input_direction &= ~DOWN; - else if (keycode == 2 || keycode == 124) - game->input_direction &= ~RIGHT; - return (0); } |
