-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.cpp
65 lines (54 loc) · 1.45 KB
/
input.cpp
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
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdlib.h>
#include "Main.h"
#include <iostream>
#include "input.h"
void setNonBlocking(int fd) {
int flags = fcntl(fd, F_GETFL, 0);
flags |= O_NONBLOCK;
flags = fcntl(fd, F_SETFL, flags);
}
void keyInput () {
// Set terminal to raw mode. Will detect user input
system("stty raw");
int character = getchar();
//character=character-32;
while (character > 0) {
switch (character)
{
case 'w':
if (snake.direction != DOWN) {
snake.direction = UP;
}
break;
case 's':
if (snake.direction != UP) {
snake.direction = DOWN;
}
break;
case 'd':
if (snake.direction != LEFT) {
snake.direction = RIGHT;
}
break;
case 'a':
if (snake.direction != RIGHT) {
snake.direction = LEFT;
}
break;
case 'q':
running = false;
break;
default:
// Do nothing
break;
}
character = getchar();
}
// Reset terminal to normal "cooked" mode
system("stty cooked");
}