-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard.c
69 lines (59 loc) · 1.42 KB
/
keyboard.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
#include "keyboard.h"
void parse_key(int input)
{
switch(input)
{
case 'w':
move_player_up(&player1);
break;
case 'a':
move_player_left(&player1);
break;
case 's':
move_player_bottom(&player1);
break;
case 'd':
move_player_right(&player1);
break;
case 'i':
move_player_up(&player2);
break;
case 'j':
move_player_left(&player2);
break;
case 'k':
move_player_bottom(&player2);
break;
case 'l':
move_player_right(&player2);
break;
}
}
void enter_player_name(Player *player, char *default_name){
printf("Enter player name [%s] (8 character max): ", default_name);
// Get input from user
fgets((*player).name, 10, stdin);
// Clear stdin
if(!strchr((*player).name, '\n')){
char c;
while((c = getchar()) != '\n' && c != EOF);
}
(*player).name[strlen((*player).name) - 1] = '\0';
if(strlen((*player).name) == 0){
strcpy((*player).name, default_name);
}
}
int get_key(){
int key;
struct termios orig_term_attr;
struct termios new_term_attr;
tcgetattr(fileno(stdin), &orig_term_attr);
memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
new_term_attr.c_lflag &= ~(ECHO|ICANON);
new_term_attr.c_cc[VTIME] = 0;
new_term_attr.c_cc[VMIN] = 0;
tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
key = fgetc(stdin);
tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
return key;
}