From 546c596505ded408bd0a73343618b100013d310f Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 10 May 2024 12:01:25 +0200 Subject: Replicate main functionality in MLX42 --- src/input.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/input.c') diff --git a/src/input.c b/src/input.c index fe1f219..7582226 100644 --- a/src/input.c +++ b/src/input.c @@ -6,14 +6,27 @@ /* By: dkaiser input_direction |= UP; else if (keycode == 0 || keycode == 123) @@ -22,7 +35,6 @@ int on_key_down(int keycode, t_game *game) game->input_direction |= DOWN; else if (keycode == 2 || keycode == 124) game->input_direction |= RIGHT; - return (0); } int on_key_up(int keycode, t_game *game) -- cgit v1.2.3 From b114117e5f62761dfecd618432a44e75263a929c Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 10 May 2024 12:12:24 +0200 Subject: Change input handling to MLX42 approach --- include/so_long.h | 5 ++--- src/init.c | 4 ++-- src/input.c | 53 ++++++++++++++++++++++------------------------------- 3 files changed, 26 insertions(+), 36 deletions(-) (limited to 'src/input.c') 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 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 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); } -- cgit v1.2.3 From 7e992a69a2031f630f1e30b0d864c0e4e7d32067 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 10 May 2024 12:17:52 +0200 Subject: Make norminette happy --- src/init.c | 9 ++++++--- src/input.c | 8 ++++---- src/loop.c | 14 ++++++-------- 3 files changed, 16 insertions(+), 15 deletions(-) (limited to 'src/input.c') diff --git a/src/init.c b/src/init.c index d72f486..bd6b48f 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: dkaiser mlx = mlx_init(1920, 1080, "so_long", false); // TODO: make size and title dynamic game->input_direction = ZERO; init_hooks(game); init_player(&game->player, 960, 540); // TODO: make player spawn point dynamic - mlx_texture_t *texture = mlx_load_png("textures/player.png"); + texture = mlx_load_png("textures/player.png"); game->player.img = mlx_texture_to_image(game->mlx, texture); - mlx_image_to_window(game->mlx, game->player.img, game->player.position.x, game->player.position.y); + mlx_image_to_window(game->mlx, game->player.img, game->player.position.x, + game->player.position.y); return (0); } diff --git a/src/input.c b/src/input.c index 40c5670..a6cb8d4 100644 --- a/src/input.c +++ b/src/input.c @@ -6,17 +6,17 @@ /* By: dkaiser player.direction.x = ((game->input_direction & RIGHT) != 0) - ((game->input_direction & LEFT) != 0); game->player.direction.y = ((game->input_direction & DOWN) != 0) - ((game->input_direction & UP) != 0); - - game->player.position.x += game->player.direction.x * PLAYER_MOVE_SPEED; - game->player.position.y += game->player.direction.y * PLAYER_MOVE_SPEED; - - draw(game); + game->player.position.x += game->player.direction.x * PLAYER_MOVE_SPEED; + game->player.position.y += game->player.direction.y * PLAYER_MOVE_SPEED; + draw(game); } -- cgit v1.2.3