-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
203 lines (160 loc) · 3.25 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "Window.h"
#include "GLDraw.h"
#include "GameState.h"
#include "Timer.h"
#define HOST_DEFAULT_PORT 6112
#define CLIENT_DEFAULT_PORT 6113
GameState* gameState;
void mouseFunc(int type, int button, int x, int y);
void keyPress(int code);
void keyRelease(int code);
GameState* initGameState(int argc, char** argv)
{
GameState* gameState = NULL;
if(argc == 4)
{
gameState = createGameState(atoi(argv[1]), argv[2], atoi(argv[3]));
}
else if(argc == 3)
{
gameState = createGameState(atoi(argv[1]), argv[2], CLIENT_DEFAULT_PORT);
}
else if(argc == 2)
{
gameState = createGameState(atoi(argv[1]), NULL, CLIENT_DEFAULT_PORT);
}
else
{
gameState = createGameState(HOST_DEFAULT_PORT, NULL, CLIENT_DEFAULT_PORT);
}
return gameState;
}
void printInstruction(void)
{
printf("\nUsage: plot [host port] [host ip] [client port]\n\n");
printf("Keys:\n");
printf("\tThrottle: W and S\n");
printf("\tRudder: A and D\n");
printf("\tAilerons: Arrow Left and Arrow Right\n");
printf("\tElevator: Arrow Up and Arrow Down\n");
printf("\tFire: Shift Or Space\n");
printf("\tCameras: Numbers 1 to 4\n");
}
int main(int argc, char** argv)
{
printInstruction();
window* w = initWindow(400,400);
initGl();
gameState = initGameState(argc, argv);
long int lastTime = getCurrentTime();
while(processWindow(w, mouseFunc, keyPress, keyRelease))
{
if(getCurrentTime() - lastTime >= GAME_TIME_STEP)
{
processGameState(gameState, GAME_TIME_STEP);
lastTime += GAME_TIME_STEP;
drawGameState(gameState);
showWindow(w);
}
}
return 0;
}
void mouseFunc(int type, int button, int x, int y)
{
//printf("mouse - type: %d, button: %d, x: %d, y: %d\n", type, button, x, y);
}
void keyPress(int code)
{
//printf("key: %d, type: press\n", code);
switch(code)
{
case 10:
case 11:
case 12:
case 13:
{
setCamera(gameState, code - 10);
}break;
case 25: // W
{
setPlaneThrottle(gameState, 1);
}break;
case 38: // A
{
setPlaneRudder(gameState, 1);
}break;
case 39: // S
{
setPlaneThrottle(gameState, -1);
}break;
case 40: // D
{
setPlaneRudder(gameState, -1);
}break;
case 113: // LEFT ARROW
{
setPlaneAilerons(gameState, -1);
}break;
case 114: // RIGHT ARROW
{
setPlaneAilerons(gameState, 1);
}break;
case 111: // UP ARROW
{
setPlaneElevator(gameState, 1);
}break;
case 116: // DOWN ARROW
{
setPlaneElevator(gameState, -1);
}break;
case 50:
case 65: // SPACE
{
setFire(gameState, 1);
}break;
}
}
void keyRelease(int code)
{
//printf("key: %d, type: release\n", code);
switch(code)
{
case 25: // W
{
setPlaneThrottle(gameState, 0);
}break;
case 38: // A
{
setPlaneRudder(gameState, 0);
}break;
case 39: // S
{
setPlaneThrottle(gameState, 0);
}break;
case 40: // D
{
setPlaneRudder(gameState, 0);
}break;
case 113: // LEFT ARROW
{
setPlaneAilerons(gameState, 0);
}break;
case 114: // RIGHT ARROW
{
setPlaneAilerons(gameState, 0);
}break;
case 111: // UP ARROW
{
setPlaneElevator(gameState, 0);
}break;
case 116: // DOWN ARROW
{
setPlaneElevator(gameState, 0);
}break;
case 50:
case 65: // SPACE
{
setFire(gameState, 0);
}break;
}
}