-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_keyboard_input.c
52 lines (47 loc) · 973 Bytes
/
get_keyboard_input.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
#include "main.h"
/**
* get_keyboard_input - Function to get keyboard input.
*
* @x_direction: pointer to the current X direction.
* @y_direction: pointer to the current Y direction.
* @quit: pointer to the quit flag.
*/
void get_keyboard_input(int *x_direction, int *y_direction, int *quit)
{
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
if (FD_ISSET(STDIN_FILENO, &fds))
{
int ch = getchar();
if (ch == 27)
{
*quit = 1;
printf("\033[2J\033[H");
}
else if (ch == 'q' && *x_direction != 1)
{
*x_direction = -1;
*y_direction = 0;
}
else if (ch == 'd' && *x_direction != -1)
{
*x_direction = 1;
*y_direction = 0;
}
else if (ch == 's' && *y_direction != -1)
{
*x_direction = 0;
*y_direction = 1;
}
else if (ch == 'z' && *y_direction != 1)
{
*x_direction = 0;
*y_direction = -1;
}
}
}