multi key action

This commit is contained in:
hugogogo
2022-03-23 14:36:20 +01:00
parent 95bd304708
commit f37f2891a4
4 changed files with 89 additions and 21 deletions

View File

@@ -24,9 +24,18 @@ void init_parsing(int ac, char **av, t_game *game);
// key_hook.c
int keypress(int keycode, t_game *game);
int keyrelease(int keycode, t_game *game);
// key_do_action.c
int shut_down(t_game *game);
void keypress_do_action(t_game *game);
// key_is_action_1.c
int is_esc(int *k_hook);
int is_go_left(int *k_hook);
int is_go_right(int *k_hook);
int is_go_forward(int *k_hook);
int is_go_backward(int *k_hook);
// -------------------------------

View File

@@ -9,6 +9,8 @@ int main(int ac, char **av)
// receive a keypress event
mlx_hook(game->win_ptr, 2, 1L << 0, keypress, game);
// receive a keyprelease event
mlx_hook(game->win_ptr, 3, 1L << 1, keyrelease, game);
// receive event when clicking the red button to close the window
mlx_hook(game->win_ptr, 17, 1L << 17, shut_down, game);
// infinite loop that waits for events to occurs

View File

@@ -7,3 +7,21 @@ int shut_down(t_game *game)
free(game);
return (0);
}
// temp, to test keypress hook
void keypress_do_action(t_game *game)
{
if (is_esc(game->k_hook))
shut_down(game);
if (is_go_left(game->k_hook))
(game->plr_x) -= 5;
if (is_go_right(game->k_hook))
(game->plr_x) += 5;
if (is_go_forward(game->k_hook))
(game->plr_y) -= 5;
if (is_go_backward(game->k_hook))
(game->plr_y) += 5;
mlx_pixel_put(game->mlx_ptr, game->win_ptr, game->plr_x, game->plr_y, 0x74db74);
}

View File

@@ -8,31 +8,70 @@
}
// temp end
static void keypress_action(int keycode, t_game *game)
{
// escape
if (keycode == 65307)
shut_down(game);
// left
if (keycode == 65361)
(game->plr_x) -= 5;
// right
if (keycode == 65363)
(game->plr_x) += 5;
// up
if (keycode == 65362)
(game->plr_y) -= 5;
// down
if (keycode == 65364)
(game->plr_y) += 5;
mlx_pixel_put(game->mlx_ptr, game->win_ptr, game->plr_x, game->plr_y, 0x74db74);
}
int keypress(int keycode, t_game *game)
{
unsigned i;
// temp
print_keycode(keycode);
keypress_action(keycode, game);
i = 0;
while (i < MAX_NB_KEY && game->k_hook[i] != 0 && game->k_hook[i] != keycode)
i++;
if (game->k_hook[i] == keycode && i < MAX_NB_KEY)
game->k_hook[i] = 0;
else if (i < MAX_NB_KEY)
game->k_hook[i] = keycode;
keypress_do_action(game);
return (0);
}
int keyrelease(int keycode, t_game *game)
{
unsigned i;
i = 0;
while (i < MAX_NB_KEY && game->k_hook[i] != keycode)
i++;
if (i < MAX_NB_KEY)
game->k_hook[i] = 0;
return (0);
}
/*
* first version, cannot combine key hook
*/
/*
* static void keypress_action(int keycode, t_game *game)
* {
* // escape
* if (keycode == 65307)
* shut_down(game);
* // left
* if (keycode == 65361)
* (game->plr_x) -= 5;
* // right
* if (keycode == 65363)
* (game->plr_x) += 5;
* // up
* if (keycode == 65362)
* (game->plr_y) -= 5;
* // down
* if (keycode == 65364)
* (game->plr_y) += 5;
* mlx_pixel_put(game->mlx_ptr, game->win_ptr, game->plr_x, game->plr_y, 0x74db74);
* }
*
* int keypress(int keycode, t_game *game)
* {
* // temp
* print_keycode(keycode);
*
* keypress_action(keycode, game);
* return (0);
* }
*
*/