-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
79 lines (71 loc) · 1.32 KB
/
main.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
73
74
75
76
77
78
79
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include "fbDrawer.h"
#include "gameControl.h"
void SetTimer(int sec, void(*proc)(int)) {
struct itimerval timer;
timer.it_interval.tv_sec = 1;
timer.it_interval.tv_usec = 0;
timer.it_value.tv_sec = 1;
timer.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &timer, NULL);
signal(SIGALRM, proc);
}
void alarm_handler(int signum) {
moveDown();
}
static struct termios newtermset, oldtermset;
void noecho(void) {
tcgetattr(STDIN_FILENO, &oldtermset);
newtermset = oldtermset;
newtermset.c_lflag &= ~ICANON;
newtermset.c_lflag &= ~ECHO;
newtermset.c_cc[VMIN] = 1;
tcsetattr(STDIN_FILENO, TCSANOW, &newtermset);
}
void reset_term(void) {
tcsetattr(STDIN_FILENO, TCSANOW, &oldtermset);
}
int getch(void) {
int ch;
noecho();
ch = getchar();
reset_term();
return ch;
}
int main(void) {
openfb("/dev/fb0");
initGame();
SetTimer(1,alarm_handler);
while(1) {
char c;
c = getch();
fflush(NULL);
switch(c) {
case 'w':
rotate();
break;
case 'a':
moveLeft();
break;
case 's':
moveDown();
break;
case 'd':
moveRight();
break;
case 'q':
printf("Game Aborted!!!\n");
printf("Your score:%d\n",getScore());
exit(0);
default:
break;
}
}
closefb();
return 0;
}