-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputs.c
72 lines (65 loc) · 2 KB
/
inputs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inputs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgrissen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/04 12:26:27 by hgrissen #+# #+# */
/* Updated: 2021/11/05 13:41:54 by hgrissen ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int key_pressed(int key, t_mlx *mlx)
{
int p;
p = mlx->plyr->input.pressed;
if (key == ESC_KEY)
ft_quit();
else if (key == A_KEY && !p)
mlx->plyr->input.left = 1;
else if (key == D_KEY && !p)
mlx->plyr->input.right = 1;
else if (key == S_KEY && !p)
mlx->plyr->input.down = 1;
else if (key == W_KEY && !p)
mlx->plyr->input.up = 1;
else
return (0);
mlx->plyr->input.pressed = 1;
return (1);
}
int key_released(int key, t_mlx *mlx)
{
if (key == A_KEY)
mlx->plyr->input.left = 0;
else if (key == D_KEY)
mlx->plyr->input.right = 0;
else if (key == S_KEY)
mlx->plyr->input.down = 0;
else if (key == W_KEY)
mlx->plyr->input.up = 0;
else
return (0);
mlx->plyr->input.pressed = 0;
return (1);
}
int check_input(t_input input)
{
if (input.up || input.down || input.right || input.left)
return (1);
return (0);
}
void refresh_input(t_input *input)
{
input->up = 0;
input->down = 0;
input->left = 0;
input->right = 0;
}
void get_input(t_mlx *mlx)
{
mlx_hook(mlx->mlx_win, 2, 0, key_pressed, mlx);
mlx_hook(mlx->mlx_win, 3, 0, key_released, mlx);
mlx_hook(mlx->mlx_win, 17, 0, ft_quit, mlx);
}