-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.cpp
431 lines (336 loc) · 11.5 KB
/
Engine.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "Engine.h"
TCPsocket Engine::socketDescriptor[MAX_SERVERS];
IPaddress Engine::serverIP[MAX_SERVERS];
int Engine::rectLeftServer[MAX_SERVERS];
int Engine::rectTopServer[MAX_SERVERS];
int Engine::rectRightServer[MAX_SERVERS];
int Engine::rectBottomServer[MAX_SERVERS];
int Engine::rectSizeX[MAX_SERVERS];
int Engine::rectSizeY[MAX_SERVERS];
bool Engine::rectMode = false;
int Engine::numAccelerometerHits = 0;
#if 0
int Engine::screenWidthRT = 2560; // needs to be divisable by RENDERTILE_SIZE
int Engine::screenHeightRT = 1440; // needs to be divisable by RENDERTILE_SIZE
int Engine::screenWidthGL = 2560;
int Engine::screenHeightGL = 1440;
#else
#if 0
int Engine::screenWidthRT = 1920;
int Engine::screenHeightRT = 1080;
int Engine::screenWidthGL = 1920;
int Engine::screenHeightGL = 1080;
#else
#if 1
int Engine::screenWidthRT = 1280;
int Engine::screenHeightRT = 720;
int Engine::screenWidthGL = 1280;
int Engine::screenHeightGL = 720;
#else
#if 0
int Engine::screenWidthRT = 240;
int Engine::screenHeightRT = 240;
int Engine::screenWidthGL = 240;
int Engine::screenHeightGL = 240;
#else
int Engine::screenWidthRT = 512;
int Engine::screenHeightRT = 256;
int Engine::screenWidthGL = 512;
int Engine::screenHeightGL = 256;
#endif
#endif
#endif
#endif
int Engine::serverFrameBuffers = 0;
int Engine::clicksPerSecond = 0;
// keys & mouse
bool Engine::upKey = false;
bool Engine::downKey = false;
bool Engine::leftKey = false;
bool Engine::rightKey = false;
//bool Engine::leftButton = false;
//bool Engine::rightButton = false;
bool Engine::jumpKey = false;
bool Engine::crouchKey = false;
bool Engine::upscale = true;
CVector2 Engine::relativeMouseMotion;
bool Engine::forceScreenResolution = false;
bool Engine::done = false;
int Engine::fontSize = 20;
bool Engine::fullscreen = false;
float Engine::angle = 90.0f;
float Engine::currentTime = 0.0f;
float Engine::currentTimeLastFrame = 0.0f;
float Engine::timeWhenMapWasLoaded = 0.0f;
float Engine::sensitivity = 3.0f;
float Engine::frameTime = 0.0f;
float Engine::framesPerSecond = 0.0f;
char* Engine::startMap = NULL;
char* Engine::serverName[MAX_SERVERS];
int Engine::serverPort[MAX_SERVERS];
int Engine::numServers = 0;
float Engine::frameInterval = 0.3f;
int Engine::numFramesRendered = 0;
float Engine::lastTime = 0.0f; // This will hold the time from the last frame
char Engine::strFrameRate[32];
bool Engine::serverUseETC1 = false;
CVector2 Engine::buttonLeftCenter;
CVector2 Engine::buttonRightCenter;
CVector2 Engine::buttonCenter;
CVector2 Engine::buttonPressed;
char Engine::debugMessage[64];
HeadTrackingMethods Engine::headTrackingMethod = HeadTrackingMethods::OFF;
void Engine::init(){
for(int i = 0; i < MAX_SERVERS; i++){
serverName[i] = NULL;
serverPort[i] = PORT_TO_SERVER;
rectLeftServer[i] = 0;
rectTopServer[i] = 0;
rectRightServer[i] = 0;
rectBottomServer[i] = 0;
rectSizeX[i] = rectSizeY[i] = 0;
}
sprintf(strFrameRate, "0fps");
debugMessage[0] = 0;
parseConfigFile();
buttonPressed = CVector2(-666.6f, -666.6f);
buttonCenter = CVector2(520.0f, 175.0f);
for(int i = 0; i < numServers; i++){
rectSizeX[i] = rectRightServer[i] - rectLeftServer[i] + 1;
rectSizeY[i] = rectTopServer[i] - rectBottomServer[i] + 1;
}
if(numServers >= 1){
// only check server 0
if(rectSizeX[0] > 0 && rectSizeX[0] < Engine::screenWidthRT && rectSizeY[0] > 0 && rectSizeY[0] <= Engine::screenHeightRT){
const int RENDERTILE_SIZE = 4;
int remainingPixelsX = rectSizeX[0] % RENDERTILE_SIZE;
int remainingPixelsY = rectSizeY[0] % RENDERTILE_SIZE;
if(remainingPixelsX == 0 && remainingPixelsY == 0){
Engine::rectMode = true;
for(int i = 0; i < numServers; i++){
cout << "Rendering Rect enabled: X (" << Engine::rectLeftServer[i] << " - " << Engine::rectRightServer[i] << ") Y (" << Engine::rectBottomServer[i] << " - " << Engine::rectTopServer[i] << "). "
<< rectSizeX[i] << "x" << rectSizeY[i] << endl;
}
}
}
}
// seed the randomizer
srand(currentTime);
cout << "Engine initialized." << endl;
}
void Engine::parseConfigFile(){
// instead of parsing a real file we just hardcode everything
forceScreenResolution = false;
fullscreen = false;
serverFrameBuffers = 1;
startMap = (char*)"dan3";
serverName[0] = (char*)"192.168.0.100";
serverPort[0] = 2000;
// config file available?
FILE* in = fopen(CONFIG_FILE, "rb");
if(!in){
cout << "Could not open config file " << CONFIG_FILE << endl;
cout << "Using default values. Trying to create new config file." << endl;
FILE* out = fopen(CONFIG_FILE, "wb");
if(!out)
return;
fprintf(out, "serverUseETC1 true\n");
fprintf(out, "server0 %s %i\n\n", serverName[0], serverPort[0]);
fprintf(out, "#server1 192.168.0.3 2000\n");
fprintf(out, "#server2 192.168.0.4 2000\n");
fprintf(out, "#server3 192.168.0.5 2000\n\n");
// settings for rendering in 1024x1024
fprintf(out, "##### 4 servers\n");
fprintf(out, "#rectLeftServer0 0\n");
fprintf(out, "#rectRightServer0 255\n");
fprintf(out, "#rectBottomServer0 0\n");
fprintf(out, "#rectTopServer0 1023\n\n");
fprintf(out, "#rectLeftServer1 256\n");
fprintf(out, "#rectRightServer1 511\n");
fprintf(out, "#rectBottomServer1 0\n");
fprintf(out, "#rectTopServer1 1023\n\n");
fprintf(out, "#rectLeftServer2 512\n");
fprintf(out, "#rectRightServer2 767\n");
fprintf(out, "#rectBottomServer2 0\n");
fprintf(out, "#rectTopServer2 1023\n\n");
fprintf(out, "#rectLeftServer3 768\n");
fprintf(out, "#rectRightServer3 1023\n");
fprintf(out, "#rectBottomServer3 0\n");
fprintf(out, "#rectTopServer3 1023\n\n");
fprintf(out, "\n##### 2 servers\n");
fprintf(out, "#rectLeftServer0 0\n");
fprintf(out, "#rectRightServer0 511\n");
fprintf(out, "#rectBottomServer0 0\n\n");
fprintf(out, "#rectTopServer0 1023\n");
fprintf(out, "#rectLeftServer1 512\n");
fprintf(out, "#rectRightServer1 1023\n");
fprintf(out, "#rectBottomServer1 0\n\n");
fprintf(out, "#rectTopServer1 1023\n\n");
fprintf(out, "startMap dan3\n");
fclose(out);
return;
}
char* firstWord = NULL;
char* secondWord = NULL;
char* thirdWord = NULL;
bool fileIsOver = false;
int i;
char buffer[1024];
char c = fgetc(in);
while(!feof(in) && !fileIsOver){
for(i = 0; (c != 0x0A) && (i < 1023) && !fileIsOver; i++){
buffer[i] = c;
c = fgetc(in);
if(feof(in))
fileIsOver = true;
}
c = fgetc(in);
if(feof(in))
fileIsOver = true;
buffer[i] = '\0';
CTextParser::eliminateEmptySpaceBeforeContent(buffer);
// don't add empty lines
if(buffer[0] == '\0')
continue;
// don't add comment lines
if(buffer[0] == '/')
continue;
CTextParser::eliminateReturnAtEndOfLine(buffer);
// don't add empty lines (after killing return character)
if(buffer[0] == '\0')
continue;
firstWord = CTextParser::getFirstWord(buffer);
secondWord = CTextParser::getSecondWord(buffer);
thirdWord = CTextParser::getThirdWord(buffer);
if(!secondWord)
continue;
//
// serverUseETC1
//
else if(!strcmp(firstWord, "serverUseETC1")){
if(!strcmp(secondWord, "true")){
serverUseETC1 = true;
} else{
serverUseETC1 = false;
}
}
//
// startMap
//
else if(!strcmp(firstWord, "startMap")){
startMap = new char[strlen(secondWord) + 1];
strcpy(startMap, secondWord);
}
//
// server0
// server1
// ...
//
for(int i = numServers; i < MAX_SERVERS; i++){
char searchString[128];
sprintf(searchString, "server%i", i);
if(!strcmp(firstWord, searchString)){
serverName[i] = new char[strlen(secondWord) + 1];
strcpy(serverName[i], secondWord);
if(thirdWord){
serverPort[i] = atoi(thirdWord);
}
numServers = i + 1;
} else {
break;
}
}
//
// rectLeftServer0, rectLeftServer1, ...
//
for(int i = 0; i < MAX_SERVERS; i++){
char searchString[128];
sprintf(searchString, "rectLeftServer%i", i);
if(!strcmp(firstWord, searchString)){
rectLeftServer[i] = atoi(secondWord);
}
}
//
// rectTopServer0, rectTopServer1, ...
//
for(int i = 0; i < MAX_SERVERS; i++){
char searchString[128];
sprintf(searchString, "rectTopServer%i", i);
if(!strcmp(firstWord, searchString)){
rectTopServer[i] = atoi(secondWord);
}
}
//
// rectRightServer0, rectRightServer1, ...
//
for(int i = 0; i < MAX_SERVERS; i++){
char searchString[128];
sprintf(searchString, "rectRightServer%i", i);
if(!strcmp(firstWord, searchString)){
rectRightServer[i] = atoi(secondWord);
}
}
//
// rectBottomServer0, rectBottomServer1, ...
//
for(int i = 0; i < MAX_SERVERS; i++){
char searchString[128];
sprintf(searchString, "rectBottomServer%i", i);
if(!strcmp(firstWord, searchString)){
rectBottomServer[i] = atoi(secondWord);
}
}
delete [] firstWord;
delete [] secondWord;
}
fclose(in);
// there has been at least one defined on top
if(numServers == 0)
numServers = 1;
}
void Engine::calculateFrameRate(){
float totalCurrentTime = SDL_GetTicks() * 0.001f;
float localCurrentTime = totalCurrentTime - timeWhenMapWasLoaded;
// save old time for stereoscopic rendering where both frames need to have the same time
currentTimeLastFrame = currentTime;
currentTime = localCurrentTime;
frameInterval = currentTime - frameTime;
frameTime = currentTime;
framesPerSecond++;
if( currentTime - lastTime > 1.0f ){
lastTime = currentTime;
sprintf(strFrameRate, "%dfps", int(framesPerSecond));
framesPerSecond = 0;
clicksPerSecond = 0;
}
updateGlobalSinus();
}
void Engine::updateGlobalSinus(){
if(angle > 360.0f)
angle = 0.0f;
angle += frameInterval / 10.0f;
}
// code snippet calculateOrthoMatrix() from http://stackoverflow.com/questions/9690006/glfrustrum-variant-in-opengl-es-2-0-how-fix-blank-screen
// author: Aristarhys
// license: http://creativecommons.org/licenses/by-sa/3.0/
void Engine::calculateOrthoMatrix(float left, float right, float bottom, float top, float near_, float far_, float result[16]){
result[0] = 2.0f / (right - left);
result[1] = 0.0f;
result[2] = 0.0f;
result[3] = 0.0f;
// second column
result[4] = 0.0f;
result[5] = 2.0f / (top - bottom);
result[6] = 0.0f;
result[7] = 0.0f;
// third column
result[8] = 0.0f;
result[9] = 0.0f;
result[10] = -2.0f / (far_ - near_);
result[11] = 0.0f;
// fourth column
result[12] = -(right + left) / (right - left);
result[13] = -(top + bottom) / (top - bottom);
result[14] = -(far_ + near_) / (far_ - near_);
result[15] = 1;
}