-
Notifications
You must be signed in to change notification settings - Fork 1
/
levelselect.cpp
548 lines (473 loc) · 15.2 KB
/
levelselect.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//Version 0.0
//Mathias Gartner
//Levelauswahl
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include "button.h"
#include "definitions.h"
#include "filesystemutility.h"
#include "globals.h"
#include "graphics.h"
#include "highscore.h"
#include "levelclass.h"
#include "levelselect.h"
#include "logger.h"
bool strEndsWith(char* string, const char* suffix);
LEVELSELECT* LEVELSELECT::_instance = NULL;
LEVELSELECT::LEVELSELECT()
{
if (_instance != NULL)
{
delete _instance;
_instance = NULL;
}
_instance = this;
currentLevel = NULL;
currentDirectory = NULL;
char levelPath[MAX_PATH];
GetDirectoryPath(levelPath, "");
if (FILESYSTEMUTILITY::EnumerateDirectories(levelPath, &directoryCount, &addDirectoryListEntry))
{
SwitchDirectory(0);
}
else
{
error("LEVELSELECT::ctor()", "FATAL: FILESYSTEMUTILITY::EnumerateDirectories() failed");
exit(1);
}
}
LEVELSELECT::~LEVELSELECT()
{
FreeLevelList(currentLevel);
FreeDirectoryList(currentDirectory);
}
int LEVELSELECT::Select()
{
const char noDirectoryName[] = "[kein Verzeichnis]";
BUTTON prevDirButton ({{10, windY - 100}, {90, windY - 50}}, 0, 3, 10, CYAN, "<", 20, YELLOW);
BUTTON nextDirButton ({{windX - 100, windY - 100}, {windX - 10, windY - 50}}, 0, 3, 10, CYAN, ">", 20, YELLOW);
BUTTON dirCaption ({{100, windY - 100}, {windX - 110, windY - 50}}, 3, 3, 10, CYAN, "(Directory)", 20, YELLOW);
BUTTON prevButton ({{10, windY - 180}, {90, windY - 130}}, 0, 3, 10, CYAN, "<", 20, YELLOW);
BUTTON nextButton ({{windX - 100, windY - 180}, {windX - 10, windY - 130}}, 0, 3, 10, CYAN, ">", 20, YELLOW);
BUTTON selectButton ({{windX - 200, 10}, {windX - 10, 60}}, 0, 3, 10, CYAN, "Spiel starten", 20, YELLOW);
BUTTON cancelButton ({{10, 10}, {190, 60}}, 0, 3, 10, CYAN, "Zurück", 20, YELLOW);
BUTTON levelCaption ({{100, windY - 180}, {windX - 110, windY - 130}}, 3, 3, 10, CYAN, "(Levelname)", 20, YELLOW);
AREA highscoreOutput {{100, 140}, {windX / 2 - 100, windY - 240}};
prevDirButton.assignKeyboardButton(0,'Q');
nextDirButton.assignKeyboardButton(0,'E');
prevButton .assignKeyboardButton(0,GLFW_KEY_LEFT ,'A');
nextButton .assignKeyboardButton(0,GLFW_KEY_RIGHT,'D');
levelCaption .assignKeyboardButton(0,'S');
cancelButton .assignKeyboardButton(0,GLFW_KEY_ESC);
selectButton .assignKeyboardButton(0,GLFW_KEY_ENTER,GLFW_KEY_SPACE);
ReloadHighscore();
prepare_GameLoop();
do
{
prepare_graphics();
char levelText[MAX_PATH];
if (GetLevel() != NULL)
sprintf(levelText, "%s (%d/%d)", GetLevel()->name, GetLevel()->index + 1, GetLevelCount());
else
strcpy(levelText, "[Kein Level vorhanden]");
levelCaption.setText(levelText);
const char* directoryName;
if (strlen(GetDirectory()->name) > 0)
directoryName = GetDirectory()->name;
else
directoryName = noDirectoryName;
char directoryText[strlen(directoryName) + 9];
sprintf(directoryText, "%s (%d/%d)", directoryName, GetDirectory()->index + 1, GetDirectoryCount());
dirCaption.setText(directoryText);
//handle clicks
if (cancelButton.clicked())
break;
else if (selectButton.clicked() && isInputValid())
{
currentLevel->score.setTimesPlayed(currentLevel->score.getTimesPlayed() + 1);
return true;
}
else if (prevButton.clicked() || levelCaption.clicked() == 2)
SwitchLevel(-1);
else if (nextButton.clicked() || levelCaption.clicked() == 1)
SwitchLevel(1);
else if (prevDirButton.clicked() || dirCaption.clicked() == 2)
SwitchDirectory(-1);
else if (nextDirButton.clicked() || dirCaption.clicked() == 1)
SwitchDirectory(1);
//output
prevDirButton.print();
nextDirButton.print();
dirCaption.print();
prevButton.print();
nextButton.print();
selectButton.print();
cancelButton.print();
levelCaption.print();
if (currentLevel != NULL)
{
drawBox({{windX/2 + 80, 140}, {windX - 110, windY - 240}}, 10, 3, WHITE);
currentLevel->level->printPreview();
TIME time = currentLevel->score.getTime();
drawBox(highscoreOutput,10,3,WHITE);
normalFont.printf({highscoreOutput.a.x + 20, highscoreOutput.b.y - 40}, taLEFT, "Highscore");
normalFont.printf({highscoreOutput.a.x + 20, highscoreOutput.b.y - 80}, taLEFT, "Züge: %d", currentLevel->score.getMoves());
normalFont.printf({highscoreOutput.a.x + 20, highscoreOutput.b.y - 120}, taLEFT, "Zeit: %02d:%02d:%05.2f", time.Hours, time.Minutes, time.Seconds);
normalFont.printf({highscoreOutput.a.x + 20, highscoreOutput.b.y - 160}, taLEFT, "%d mal gespielt", currentLevel->score.getTimesPlayed());
}
} while (complete_graphics());
logger(true, "LEVELSELECT: Cancelled, Returning false.");
return false;
}
bool LEVELSELECT::isInputValid()
{
return (currentLevel != NULL);
}
bool LEVELSELECT::NextLevelAvailable()
{
return (GetLevel() != NULL && GetLevel()->next != NULL);
}
bool LEVELSELECT::PrevLevelAvailable()
{
return (GetLevel() != NULL && GetLevel()->prev != NULL);
}
bool LEVELSELECT::SelectLevel(int index)
{
bool found = false;
LEVELFILE* level = GetLevel();
if (level == NULL)
return false;
//get first level
while (level->prev != NULL)
{
level = level->prev;
}
for (LEVELFILE* p = level; p != NULL; p = p->next)
{
if (p->index == index)
{
found = true;
currentLevel = p;
break;
}
}
ReloadHighscore();
return found;
}
bool LEVELSELECT::SwitchLevel(int offset)
{
bool success = true;
if (offset<0)
{
for (int i=0; i>offset; i--)
{
if (currentLevel != NULL && currentLevel->prev != NULL)
{
currentLevel = currentLevel->prev;
}
else
{
success = false;
break;
}
}
}
else if (offset>0)
{
for (int i=0; i<offset; i++)
{
if (currentLevel != NULL && currentLevel->next != NULL)
{
currentLevel = currentLevel->next;
}
else
{
success = false;
break;
}
}
}
ReloadHighscore();
return success;
}
bool LEVELSELECT::NextDirectoryAvailable()
{
return (GetDirectory() != NULL && GetDirectory()->next != NULL);
}
bool LEVELSELECT::PrevDirectoryAvailable()
{
return (GetDirectory() != NULL && GetDirectory()->prev != NULL);
}
bool LEVELSELECT::SelectDirectory(int index)
{
bool found = false;
LEVELDIRECTORY* directory = GetDirectory();
if (directory == NULL)
return false;
//get first directory
while (directory->prev != NULL)
{
directory = directory->prev;
}
for (LEVELDIRECTORY* p = directory; p != NULL; p = p->next)
{
if (p->index == index)
{
found = true;
currentDirectory = p;
break;
}
}
if (found)
LoadDirectory();
return found;
}
bool LEVELSELECT::SwitchDirectory(int offset)
{
bool success = true;
if (offset<0)
{
for (int i=0; i>offset; i--)
{
if (currentDirectory != NULL && currentDirectory->prev != NULL)
{
currentDirectory = currentDirectory->prev;
}
else
{
success = false;
break;
}
}
}
else if (offset>0)
{
for (int i=0; i<offset; i++)
{
if (currentDirectory != NULL && currentDirectory->next != NULL)
{
currentDirectory = currentDirectory->next;
}
else
{
success = false;
break;
}
}
}
if (success)
{
LoadDirectory();
}
return success;
}
//Loads current directory (including all levels it contains)
void LEVELSELECT::LoadDirectory()
{
char levelPath[MAX_PATH];
GetDirectoryPath(&levelPath[0], "");
if (strlen(levelPath)+strlen(currentDirectory->name)>=MAX_PATH)
{
error("LEVELSELECT::SwitchDirectory", "FATAL: Path too long.");
exit (1);
}
strcat(levelPath, currentDirectory->name);
levelCount = 0;
FreeLevelList(currentLevel);
currentLevel = NULL;
if (FILESYSTEMUTILITY::EnumerateFiles(levelPath, &levelCount, &addLevelListEntry))
{
SwitchLevel(0);
}
else
{
logger(true, "WARN: No levels available (Got false from FILESYSTEMUTILITY::EnumerateFiles)");
}
}
void LEVELSELECT::ReloadHighscore()
{
if (currentLevel != NULL)
currentLevel->score = HIGHSCORE(currentLevel->name, currentLevel->directory->name);
}
void LEVELSELECT::GetLevelPath(char* levelPath, const LEVELFILE* level)
{
if (strlen(level->directory->path) + strlen(level->name) + 4 >= MAX_PATH)
{
error ("LEVELSELECT::GetLevelPath", "FATAL: Path too long (longer than %d). (Path: %s%s.lvl)", level->directory->path, level->name);
exit (1);
}
else
{
sprintf(levelPath, "%s%s.lvl", level->directory->path, level->name);
}
}
bool LEVELSELECT::GetDirectoryPath(char* directoryPath, const char* directoryName)
{
const char basePath[] = "\\daten\\level\\";
if (GetCurrentDirectory(MAX_PATH, directoryPath) != 0)
{
if (strlen(directoryPath) + strlen(basePath) + strlen(directoryName) >= MAX_PATH)
{
error("LEVELSELECT::GetDirectoryName", "Path too long (longer than %d). (Path: %s%s%s)", MAX_PATH, directoryPath, basePath, directoryName);
}
else
{
strcat(directoryPath, basePath);
if (strlen(directoryName) > 0)
{
strcat(directoryPath, directoryName);
strcat(directoryPath, "\\");
}
return true;
}
}
else
{
error("LEVELSELECT::GetDirectoryName", "Couldn't get CurrentDirectory");
}
exit (1); //Only valid paths will be returned. If anything goes wrong -> exit
return false;
}
bool LEVELSELECT::addLevelListEntry(char* levelFileName)
{
if (strlen(levelFileName)+1 > MAX_PATH)
{
error("LEVELSELECT::addLevelListEntry", "Parameter levelFileName too long. Max length is MAX_PATH (%d).", MAX_PATH);
return false;
}
if (!strEndsWith(levelFileName, ".lvl"))
{
logger(true, "WARNING: Unknown file in level directory %s: %s", GetCurrent()->GetDirectory()->name, levelFileName);
return false;
}
LEVELFILE *lastLevel = GetCurrent()->GetLevel();
LEVELFILE *newLevel;
//get last level
if (lastLevel != NULL)
{
while (lastLevel->next != NULL)
{
lastLevel = lastLevel->next;
}
}
newLevel = (LEVELFILE*)malloc(sizeof(LEVELFILE));
newLevel->prev = lastLevel;
newLevel->next = NULL;
if (lastLevel != NULL)
{
lastLevel->next = newLevel;
newLevel->index = lastLevel->index+1;
}
else
{
newLevel->index = 0;
}
strcpy(newLevel->name, levelFileName);
newLevel->name[strlen(newLevel->name)-4] = '\0'; //cut off fileextension (to get levelname)
newLevel->directory = GetCurrent()->currentDirectory;
GetCurrent()->GetLevelPath(newLevel->path, newLevel);
newLevel->score = HIGHSCORE(newLevel->name, newLevel->directory->name);
newLevel->level = new LEVEL({(windX/2+160), 160}, 17, newLevel->path, true);
if (GetCurrent()->GetLevel() == NULL)
GetCurrent()->currentLevel = newLevel;
return true;
}
bool LEVELSELECT::addDirectoryListEntry(char* directoryName)
{
if (strlen(directoryName)+1 > MAX_PATH)
{
error("LEVELSELECT::addLevelListEntry", "Parameter directoryName too long. Max length is MAX_PATH (%d).", MAX_PATH);
return false;
}
LEVELDIRECTORY *lastDirectory = GetCurrent()->GetDirectory();
LEVELDIRECTORY *newDirectory;
//get last directory
if (lastDirectory != NULL)
{
while (lastDirectory->next != NULL)
{
lastDirectory = lastDirectory->next;
}
}
newDirectory = (LEVELDIRECTORY*)malloc(sizeof(LEVELDIRECTORY));
newDirectory->prev = lastDirectory;
newDirectory->next = NULL;
if (lastDirectory != NULL)
{
lastDirectory->next = newDirectory;
newDirectory->index = lastDirectory->index+1;
}
else
{
newDirectory->index = 0;
}
if (strcmp(directoryName, "..")==0)
{
strcpy(newDirectory->name, "");
}
else
{
strcpy(newDirectory->name, directoryName);
}
GetCurrent()->GetDirectoryPath(newDirectory->path, newDirectory->name);
if (GetCurrent()->GetDirectory() == NULL)
GetCurrent()->currentDirectory = newDirectory;
return true;
}
//Frees hole list. listEntry can be any element of the target list. listEntry can be NULL.
void LEVELSELECT::FreeLevelList(LEVELFILE* listEntry)
{
if (listEntry != NULL)
{
LEVELFILE* level = listEntry;
//get first level
while (level->prev != NULL)
{
level = level->prev;
}
//delete all
while (level != NULL)
{
level->score.Save();
delete level->level;
LEVELFILE* freeLevel = level;
level = level->next;
free (freeLevel);
if (level != NULL)
level->prev = NULL;
}
}
}
//Frees hole list. listEntry can be any element of the target list. listEntry can be NULL.
void LEVELSELECT::FreeDirectoryList(LEVELDIRECTORY* listEntry)
{
if (listEntry != NULL)
{
LEVELDIRECTORY* directory = listEntry;
//get first directory
while (directory->prev != NULL)
{
directory = directory->prev;
}
//delete all
while (directory != NULL)
{
LEVELDIRECTORY* freeDirectory = directory;
directory = directory->next;
free (freeDirectory);
if (directory != NULL)
directory->prev = NULL;
}
}
}
bool strEndsWith(char* string, const char* suffix)
{
int stringLength, suffixLength;
stringLength = strlen(string);
suffixLength = strlen(suffix);
if(stringLength < suffixLength)
return false;
return strncmp(string + stringLength - suffixLength, suffix, suffixLength) == 0;
}