/
main.c
848 lines (692 loc) · 26.7 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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*
TODO:
- Restart music at correct position after game over
Gameplay possibilities:
- Water
- Jump refreshers (Like boshy)
- Spikes that switch on jump (Like boshy)
- On off platforms (Like megaman)
- Switches that can move / destroy entities
- High bounce pads
- Conveyor belts
- Pendulum movement
- Rotating lines of entities (Like the fireballs in super mario bros)
- Growing and shrinking entities
- Entity that shoots other entities
- Arced projectiles (like axes in castlevania)
- Sine wave movement pattern (Like madusas in castlevania)
- Falling and rising platforms (Like in IWBTG)
Spike boss
- Boss falls from top of screen and land on ground
- Spikes fall randomly from the top of the screen
- spikes rise from the ground with gaps for player to stand in (warning signs above where spikes will appear)
- Boss moves back and forward across the room with different movement styles
Straight forward, sin wave up and down, patterns of popping up and down
- Boss flies up into middle of screen and fires spikes in touhou style patterns
constant stream of of spikes in a circle
aimed stream of spikes
spikes that gradually change angle as they fly out
- Boss moves to lower corner of screen and fires formations of spikes across the ground
that have to be dodged: A nerfed gate, big diamond, etc
*/
#define NO_MUSIC
#define PI 3.14159265359
#define MAX_ENTITIES 2048
#define MAP_WIDTH 30
#define MAP_HEIGHT 17
#define GRID_SIZE 32
#include <ctype.h>
#include "engine/engine.h"
#include "controller.h"
#include "entity.h"
#include "defaultMap.h"
#include "menu.h"
#include "editor.h"
#include "player.h"
#include "main.h"
#include "collision.h"
#include "script.c"
#include "menu.c"
#include "editor.c"
#include "controller.c"
#include "player.c"
#include "entity.c"
#include "lighting.c"
Script* levelGetScriptAtPosition(Level* l, int x, int y)
{
for(int i = 0; i < MAX_SCRIPTS_PER_LEVEL; ++i)
if(l->scripts[i].inUse && l->scripts[i].position.x == x
&& l->scripts[i].position.y == y)
return &l->scripts[i];
return 0;
}
Script* levelAddScript(Level* l, int x, int y)
{
for(int i = 0; i < MAX_SCRIPTS_PER_LEVEL; ++i)
if(!l->scripts[i].inUse)
{
l->scripts[i].position.x = x;
l->scripts[i].position.y = y;
l->scripts[i].inUse = true;
l->scripts[i].text[0] = '\0';
return &l->scripts[i];
}
printf("Error: Out of room for scripts!");
return 0;
}
void levelRemoveScript(Level* l, int x, int y)
{
for(int i = 0; i < MAX_SCRIPTS_PER_LEVEL; ++i)
if(l->scripts[i].inUse && l->scripts[i].position.x == x
&& l->scripts[i].position.y == y)
l->scripts[i].inUse = false;
}
void textInputEditString(TextInput* ti, char* string, int stringMaxLength)
{
ti->text = string;
ti->textMaxLength = stringMaxLength;
ti->cursorPosition = strlen(string);
ti->active = true;
}
void textInputInit(TextInput* ti)
{
ti->text = 0;
ti->textMaxLength = 0;
ti->length = 0;
ti->active = false;
ti->position.x = 256;
ti->position.y = 128;
ti->backspaceFrames = 0;
ti->leftFrames = 0;
ti->rightFrames = 0;
ti->cursorPosition = 0;
}
// TODO: Optimize and make safe
void stringInsert(char* destString, char* string, int position, int maxSize)
{
char buffer[maxSize];
strncpy(buffer, destString, position);
buffer[position] = '\0';
strcat(buffer, string);
strcat(buffer, destString + position);
strcpy(destString, buffer);
}
// TODO: Optimize and make safe
void stringRemoveCharacter(char* string, int position, int maxSize)
{
char buffer[maxSize];
strncpy(buffer, string, position - 1);
buffer[position - 1] = '\0';
strcat(buffer, string + position);
strcpy(string, buffer);
}
void textInputUpdate(TextInput* ti, Iwbtg* iw)
{
Game* g = &iw->game;
if(ti->active)
{
// Force upper case on all input
char* text = ti->text;
int inputLength = strlen(g->input.text);
if(inputLength + strlen(g->input.text) < ti->textMaxLength-1 && inputLength > 0)
{
stringInsert(text, g->input.text, ti->cursorPosition, SCRIPT_MAX_LENGTH);
ti->cursorPosition += inputLength;
}
if(g->input.keysPressed[SDLK_LEFT & 255])
{
ti->cursorPosition--;
ti->leftFrames = 0;
}
if(g->input.keys[SDLK_LEFT & 255])
{
ti->leftFrames++;
if(ti->leftFrames > 25)
{
ti->cursorPosition--;
}
}
if(g->input.keysPressed[SDLK_RIGHT & 255])
{
ti->cursorPosition++;
ti->rightFrames = 0;
}
if(g->input.keys[SDLK_RIGHT & 255])
{
ti->rightFrames++;
if(ti->rightFrames > 25)
{
ti->cursorPosition++;
}
}
ti->cursorPosition = min(max(ti->cursorPosition, 0), strlen(text));
if(g->input.keysPressed[SDLK_BACKSPACE & 255])
{
int length = strlen(text);
if(length > 0)
{
stringRemoveCharacter(text, ti->cursorPosition, SCRIPT_MAX_LENGTH);
ti->cursorPosition--;
}
ti->backspaceFrames = 0;
}
if(g->input.keys[SDLK_BACKSPACE & 255])
{
ti->backspaceFrames++;
if(ti->backspaceFrames > 25)
{
int length = strlen(text);
if(length > 0)
{
stringRemoveCharacter(text, ti->cursorPosition, SCRIPT_MAX_LENGTH);
ti->cursorPosition--;
}
}
}
if(g->input.keysPressed[SDLK_RETURN & 255])
{
if(strlen("\n") + strlen(g->input.text) < ti->textMaxLength-1)
{
stringInsert(text, "\n", ti->cursorPosition, SCRIPT_MAX_LENGTH);
ti->cursorPosition++;
}
}
if(g->input.keysPressed[SDLK_ESCAPE & 255])
ti->active = false;
// Stop any input being interpreted by the game
g->input.frameVoid = true;
}
}
void textInputDraw(TextInput* ti, Iwbtg* iw)
{
if(ti->active)
{
int padding = 8;
Vector2f size = getTextSize(&iw->fontSmall, ti->text);
ti->position.x = (iw->game.size.x / 2) - (size.x / 2) - padding;
ti->position.y = (iw->game.size.y / 2) - (size.y / 2) - padding;
rectangleDraw(&iw->game, ti->position.x - padding, ti->position.y - padding,
size.x + (padding * 2), size.y + (padding * 2) - 2, 0.2, 0.1, 0.3, 0.8);
drawText(&iw->game, &iw->fontSmall, ti->text, ti->position.x, ti->position.y);
Vector2f cursorPosition = getTextCharPosition(&iw->fontSmall, ti->text, ti->cursorPosition);
rectangleDraw(&iw->game, ti->position.x + cursorPosition.x, ti->position.y + cursorPosition.y, 2, 16, 1.0, 1.0, 1.0, 1.0);
}
}
char* getCurrentMapName(Iwbtg* iw, char* string, int stringLength)
{
snprintf(string, stringLength, "assets/%dx%d.map", iw->room.x, iw->room.y);
return string;
}
Entity* createParticle(Iwbtg* iw, Texture* texture, float x, float y,
float hSpeed, float vSpeed, float life)
{
Entity* e = createEntity(iw, EntityType_particle, x, y);
e->velocity.x = hSpeed;
e->velocity.y = vSpeed;
e->animationTimer = life;
e->spin = 1;
e->friction = 2;
e->spinFriction = 0.05;
e->depth = -10;
spriteInit(&e->sprite, texture, texture->size.x, texture->size.y);
e->sprite.additiveBlend = true;
return e;
}
void loadRoomFile(Iwbtg* iw, char* name)
{
char roomFile[255];
snprintf(roomFile, 255, "assets/%s.rm", name);
ScriptState roomInfoScript = loadAndParseScript(roomFile);
if(scriptHasValue(&roomInfoScript, "blockTexture"))
{
Texture* blockTexture = assetsGetTexture(&iw->game, scriptReadString(&roomInfoScript, "blockTexture", "no block texture"));
if(blockTexture)
iw->blockTexture = blockTexture;
}
char buffer[128];
for(int i = 0; i < MAX_BACKGROUNDS_PER_LEVEL; ++i)
{
snprintf(buffer, 128, "background%d", i);
if(scriptHasValue(&roomInfoScript, buffer))
{
iw->backgrounds[i].texture = assetsGetTexture(&iw->game, scriptReadString(&roomInfoScript, buffer, "background name missing!?"));
iw->backgrounds[i].enabled = true;
snprintf(buffer, 128, "background%dhSpeed", i);
iw->backgrounds[i].speed.x = (float)scriptReadNumber(&roomInfoScript, buffer, 0);snprintf(buffer, 128, "background%dvSpeed", i);
iw->backgrounds[i].speed.y = (float)scriptReadNumber(&roomInfoScript, buffer, 0);
}
}
}
void loadMap(Iwbtg* iw, char* file)
{
iw->boss = 0;
Background bgClear = {0};
for(int i = 0; i < MAX_BACKGROUNDS_PER_LEVEL; ++i)
iw->backgrounds[i] = bgClear;
FILE* f;
if((f = fopen(file, "rb")))
{
fread(iw->level.entities.data, sizeof(int) * MAP_WIDTH * MAP_HEIGHT, 1, f);
fread(iw->level.controllers.data, sizeof(int) * MAP_WIDTH * MAP_HEIGHT, 1, f);
int scriptCount;
fread(&scriptCount, sizeof(int), 1, f);
for(int i = 0; i < MAX_SCRIPTS_PER_LEVEL; ++i)
{
iw->level.scripts[i].text[0] = '\0';
iw->level.scripts[i].inUse = false;
}
for(int i = 0; i < scriptCount; ++i)
{
fread(&iw->level.scripts[i], sizeof(Script), 1, f);
}
fread(&iw->level.propertiesScript[0], MAX_LEVEL_PROPERTIES_SCRIPT_LENGTH, 1, f);
memset(iw->level.ground.data, 0, sizeof(int) * MAP_WIDTH * MAP_HEIGHT);
memset(iw->entityMap, 0, sizeof(Entity*) * MAP_WIDTH * MAP_HEIGHT);
fclose(f);
}
else
{
memcpy(iw->level.entities.data, defaultMap, sizeof(int) * MAP_WIDTH * MAP_HEIGHT);
memset(iw->level.ground.data, 0, sizeof(int) * MAP_WIDTH * MAP_HEIGHT);
memset(iw->level.controllers.data, 0, sizeof(int) * MAP_WIDTH * MAP_HEIGHT);
memset(iw->entityMap, 0, sizeof(Entity*) * MAP_WIDTH * MAP_HEIGHT);
iw->level.propertiesScript[0] = '\0';
for(int i = 0; i < MAX_SCRIPTS_PER_LEVEL; ++i)
{
iw->level.scripts[i].text[0] = '\0';
iw->level.scripts[i].inUse = false;
}
iw->editor.enabled = true;
}
for(int i = 0; i < MAX_BACKGROUNDS_PER_LEVEL; ++i)
iw->backgrounds[i].enabled = false;
ScriptState roomProperties = parseScript(iw->level.propertiesScript);
if(scriptHasValue(&roomProperties, "roomtype"))
{
loadRoomFile(iw, scriptReadString(&roomProperties, "roomtype", "notfound"));
}
musicPlay(assetsGetMusic(&iw->game, "forestMusic"), 0.7, &iw->game);
destroyAllEntities(iw);
for(int t = 0; t < iw->level.entities.height; ++t)
for(int i = 0; i < iw->level.entities.width; ++i)
{
int index = i + (t * iw->level.entities.width);
int typeIndex = iw->level.entities.data[index] - 1;
Entity* e = createEntityFromTypeIndex(iw, typeIndex, i * GRID_SIZE, t * GRID_SIZE);
if(e)
{
typeIndex = iw->level.controllers.data[index] - 1;
entitySetControllerFromTypeIndex(e, typeIndex, iw);
}
}
for(int t = 0; t < iw->level.entities.height; ++t)
for(int i = 0; i < iw->level.entities.width; ++i)
{
Entity* e = iw->entityMap[i][t];
if(e && e->controller->type == ControllerType_chain)
resolveChain(e, iw);
}
}
void saveMap(Iwbtg* iw, char* file)
{
FILE* f = fopen(file, "wb");
fwrite(iw->level.entities.data, sizeof(int) * MAP_WIDTH * MAP_HEIGHT, 1, f);
fwrite(iw->level.controllers.data, sizeof(int) * MAP_WIDTH * MAP_HEIGHT, 1, f);
int scriptCount = 0;
for(int i = 0; i < MAX_SCRIPTS_PER_LEVEL; ++i)
if(iw->level.scripts[i].inUse)
scriptCount++;
fwrite(&scriptCount, sizeof(int), 1, f);
for(int i = 0; i < MAX_SCRIPTS_PER_LEVEL; ++i)
if(iw->level.scripts[i].inUse)
{
// TODO: Write only the characters that are actually used.
fwrite(&iw->level.scripts[i], sizeof(Script), 1, f);
}
fwrite(&iw->level.propertiesScript[0], MAX_LEVEL_PROPERTIES_SCRIPT_LENGTH, 1, f);
fclose(f);
}
void loadGame(Iwbtg* iw)
{
Player* p = &iw->player;
char saveFileName[128];
snprintf(saveFileName, 128, "save%d.sav", iw->activeSaveSlot);
iw->saveState.room.x = -1;
iw->saveState.room.y = 0;
bool newGame = true;
FILE* f;
if((f = fopen(saveFileName, "r")))
{
newGame = false;
fread(&iw->saveState, sizeof(SaveState), 1, f);
fclose(f);
}
p->dead = false;
iw->room = iw->saveState.room;
p->velocity.x = p->velocity.y = 0;
iw->state = GameState_inGame;
iw->time = 0;
char buffer[128];
loadMap(iw, getCurrentMapName(iw, buffer, 128));
if(!newGame)
p->position = iw->saveState.playerPosition;
}
void saveGame(Iwbtg* iw, bool position)
{
if(position)
{
iw->saveState.playerPosition = iw->player.position;
iw->saveState.room = iw->room;
}
char saveFileName[128];
snprintf(saveFileName, 128, "save%d.sav", iw->activeSaveSlot);
// Save
FILE* f = fopen(saveFileName, "w");
fwrite(&iw->saveState, sizeof(SaveState), 1, f);
fclose(f);
}
void menuFunctionStart(MenuItem* mi, void* data)
{
Iwbtg* iw = (Iwbtg*)data;
iw->activeMenu = &iw->loadMenu;
}
void menuFunctionQuit(MenuItem* mi, void* data)
{
Iwbtg* iw = (Iwbtg*)data;
iw->game.running = false;
}
void menuFunctionLoadSave(MenuItem* mi, void* data)
{
Iwbtg* iw = (Iwbtg*)data;
iw->activeSaveSlot = mi->id + 1;
iw->state = GameState_inGame;
loadGame(iw);
}
void iwbtgInit(Iwbtg* iw)
{
textInputInit(&iw->textInput);
fontInit(&iw->game.font, assetsGetTexture(&iw->game, "font"), 24, 32, "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,.!?\"'/\\<>()=:-");
fontSetAllLetterWidth(&iw->game.font, 20);
fontInit(&iw->fontSmall, assetsGetTexture(&iw->game, "fontSmall"), 12, 16,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,.!?\"'/\\<>()=:-");
fontSetAllLetterWidth(&iw->fontSmall, 8);
editorInit(iw);
iw->blockTexture = assetsGetTexture(&iw->game, "block");
iw->titleTexture = assetsGetTexture(&iw->game, "title");
MenuItem* mi;
iw->frameCount = 0;
// MAIN MENU
menuInit(&iw->mainMenu, iw->game.size.x / 2, 540 - 230, iw);
iw->mainMenu.spacing.y = 15;
mi = menuAddItem(&iw->mainMenu, MenuItemType_button, "START", iw);
mi->function = menuFunctionStart;
mi->functionData = (void*) iw;
menuAddItem(&iw->mainMenu, MenuItemType_button, "OPTIONS", iw);
mi = menuAddItem(&iw->mainMenu, MenuItemType_button, "QUIT", iw);
mi->function = menuFunctionQuit;
mi->functionData = (void*) iw;
// SAVE MENU
menuInit(&iw->loadMenu, 240, 540 - 180, iw);
iw->loadMenu.spacing.x = 80;
iw->loadMenu.orientation = MenuOrientation_horizontal;
mi = menuAddItem(&iw->loadMenu, MenuItemType_button, "SLOT 1", iw);
mi->function = menuFunctionLoadSave;
mi->functionData = (void*) iw;
mi = menuAddItem(&iw->loadMenu, MenuItemType_button, "SLOT 2", iw);
mi->function = menuFunctionLoadSave;
mi->functionData = (void*) iw;
mi = menuAddItem(&iw->loadMenu, MenuItemType_button, "SLOT 3", iw);
mi->function = menuFunctionLoadSave;
mi->functionData = (void*) iw;
iw->activeMenu = &iw->mainMenu;
Texture* gameOverTexture = assetsGetTexture(&iw->game, "gameOver");
spriteInit(&iw->gameOverSprite, assetsGetTexture(&iw->game, "gameOver"),
gameOverTexture->size.x, gameOverTexture->size.y);
playerInit(&iw->player, 64, 128 + 32, iw);
iw->saveState.playerPosition = iw->player.position;
Vector2i startingRoom = {-1, 0};
iw->room = iw->saveState.room = startingRoom;
musicPlayOnce(assetsGetMusic(&iw->game, "menuMusic"), 0.5, &iw->game);
iw->blockTexture = assetsGetTexture(&iw->game, "stage1BlockTiles");
}
void iwbtgLoad(Iwbtg* iw)
{
Game* g = &iw->game;
assetsLoadTexture(g, "assets/kid.png", "kid"); //kid_silhouette
assetsLoadTexture(g, "assets/block.png", "block");
assetsLoadTexture(g, "assets/objects.png", "objects");
assetsLoadTexture(g, "assets/spike.png", "spike");
assetsLoadTexture(g, "assets/save.png", "save");
assetsLoadTexture(g, "assets/player_death_particle.png", "playerDeathParticle");
assetsLoadTexture(g, "assets/game_over.png", "gameOver");
assetsLoadTexture(g, "assets/warp.png", "warp");
assetsLoadTexture(g, "assets/player_bullet.png", "playerBullet");
assetsLoadTexture(g, "assets/font.png", "font");
assetsLoadTexture(g, "assets/title.png", "title");
assetsLoadTexture(g, "assets/fruit.png", "fruit");
assetsLoadTexture(g, "assets/controllers.png", "controllers");
assetsLoadTexture(g, "assets/moving_platform.png", "movingPlatform");
assetsLoadTexture(g, "assets/font_small.png", "fontSmall");
assetsLoadTexture(g, "assets/gl_test.png", "glTest");
assetsLoadTexture(g, "assets/boss1.png", "boss");
assetsLoadTexture(g, "assets/boss_health_bar.png", "bossHealthBar");
assetsLoadTexture(g, "assets/block_tiles.png", "stage1BlockTiles");
assetsLoadTexture(g, "assets/stage1_back1.png", "stage1Back1");
assetsLoadTexture(g, "assets/stage1_back2.png", "stage1Back2");
assetsLoadTexture(g, "assets/stage1_back3.png", "stage1Back3");
assetsLoadTexture(g, "assets/block_tiles2.png", "stage2BlockTiles");
assetsLoadTexture(g, "assets/stage2_back1.png", "stage2Back1");
assetsLoadTexture(g, "assets/stage2_back2.png", "stage2Back2");
assetsLoadTexture(g, "assets/stage2_back3.png", "stage2Back3");
assetsLoadTexture(g, "assets/mountains.png", "mountains");
assetsLoadTexture(g, "assets/night_sky.png", "nightSky");
assetsLoadTexture(g, "assets/tower.png", "tower");
assetsLoadTexture(g, "assets/vine.png", "vine");
assetsLoadTexture(g, "assets/jump_refresher.png", "jumpRefresher");
assetsLoadTexture(g, "assets/vignette.png", "vignette");
assetsLoadTexture(g, "assets/boss2.png", "boss2");
assetsLoadSound(g, "assets/jump.wav", "jump");
assetsLoadSound(g, "assets/double_jump.wav", "doubleJump");
assetsLoadSound(g, "assets/shoot.wav", "shoot");
assetsLoadSound(g, "assets/death.wav", "death");
assetsLoadSound(g, "assets/save.wav", "saved");
assetsLoadSound(g, "assets/trap.wav", "trap");
assetsLoadSound(g, "assets/boss_hit.wav", "bossHit");
assetsLoadSound(g, "assets/boss_shoot.wav", "bossShoot");
assetsLoadMusic(g, "assets/forest_music.ogg", "forestMusic");
assetsLoadMusic(g, "assets/menu_music.ogg", "menuMusic");
assetsLoadMusic(g, "assets/apple_boss.ogg", "appleBossMusic");
assetsLoadMusic(g, "assets/boss_defeated.ogg", "bossDefeatedMusic");
assetsLoadMusic(g, "assets/game_over.ogg", "gameOverMusic");
}
void iwbtgUpdate(Iwbtg* iw)
{
Game* g = &iw->game;
float dt = (float)1 / 50;
iw->time += dt;
iw->frameCount++;
textInputUpdate(&iw->textInput, iw);
if(checkKeyPressed(g, KEY_FULLSCREEN_TOGGLE))
gameFullscreenToggle(g);
if(iw->state == GameState_menu)
{
updateMenu(iw->activeMenu, iw, dt);
if(checkKeyPressed(g, KEY_MENU))
{
if(iw->activeMenu != &iw->mainMenu)
iw->activeMenu = &iw->mainMenu;
else
iw->game.running = false;
}
} else if(iw->state == GameState_inGame || iw->state == GameState_gameOver)
{
if(iw->state == GameState_gameOver)
{
iw->gameOverTimer += dt;
}
if(checkKeyPressed(g, KEY_SAVE_LEVEL))
{
char buffer[128];
saveMap(iw, getCurrentMapName(iw, buffer, 128));
}
if(checkKeyPressed(g, KEY_LOAD_LEVEL))
{
char buffer[128];
loadMap(iw, getCurrentMapName(iw, buffer, 128));
}
if(checkKeyPressed(g, KEY_MENU) && !iw->editor.enabled)
{
iw->state = GameState_menu;
iw->activeMenu = &iw->mainMenu;
musicStop(&iw->game);
musicPlayOnce(assetsGetMusic(&iw->game, "menuMusic"), 1.0, &iw->game);
}
if(!iw->editor.enabled)
playerUpdate(&iw->player, iw);
editorUpdate(iw);
iw->entityDrawCount = 0;
if(!iw->editor.enabled)
{
for(int i = 0; i < MAX_ENTITIES; ++i)
{
Entity* e = &iw->entities[i];
if(e->active)
{
iw->entityDrawOrder[iw->entityDrawCount++] = e;
entityUpdate(e, iw, dt);
controllerUpdate(e->controller, e, iw, dt);
}
}
}
}
}
int compareEntitiesByDepth(const void* a, const void* b)
{
Entity* ea = *((Entity**)a);
Entity* eb = *((Entity**)b);
return (ea->depth < eb->depth) - (ea->depth > eb->depth);
}
void iwbtgDraw(Iwbtg* iw)
{
Game* g = &iw->game;
renderBegin(g);
for(int i = 0; i < MAX_BACKGROUNDS_PER_LEVEL; ++i)
{
Background* b = &iw->backgrounds[i];
if(b->enabled)
{
int offsetX = (int)(iw->time * b->speed.x) % b->texture->size.x;
int offsetY = (int)(iw->time * b->speed.y) % b->texture->size.y;
textureDraw(g, b->texture,
b->position.x + offsetX,
b->position.y + offsetY);
// TODO: Remove this hack and implement looping properly
if(b->speed.x > 0)
textureDraw(g, b->texture,
b->position.x + offsetX - b->texture->size.x,
b->position.y + offsetY);
if(b->speed.y > 0)
textureDraw(g, b->texture,
b->position.x + offsetX,
b->position.y + offsetY - b->texture->size.x);
if(b->speed.x > 0 && b->speed.y > 0)
textureDraw(g, b->texture,
b->position.x + offsetX - b->texture->size.x,
b->position.y + offsetY - b->texture->size.x);
}
}
if(iw->state == GameState_menu)
{
int ox = (iw->game.size.x / 2) - (iw->titleTexture->size.x / 2);
int oy = 80;
textureDraw(&iw->game, iw->titleTexture, ox, oy);
drawMenu(iw->activeMenu, iw);
}
if(iw->state == GameState_inGame || iw->state == GameState_gameOver)
{
qsort(iw->entityDrawOrder, iw->entityDrawCount, sizeof(Entity*), compareEntitiesByDepth);
if(!iw->editor.enabled)
for(int i = 0; i < iw->entityDrawCount; ++i)
{
Entity* e = iw->entityDrawOrder[i];
entityDraw(e, iw);
}
Player* p = &iw->player;
if(!p->dead)
spriteDraw(g, &iw->player.sprite, iw->player.position.x, iw->player.position.y);
textureDraw(g, assetsGetTexture(g, "vignette"), 0, 0);
char roomText[128];
snprintf(roomText, 128, "%03d", 1 - iw->room.y);
drawText(&iw->game, 0, roomText, 8, 8);
editorDraw(iw);
if(iw->state == GameState_gameOver)
{
int cx = (g->size.x - iw->gameOverSprite.texture->size.x) / 2;
int cy = (g->size.y - iw->gameOverSprite.texture->size.y) / 2;
Sprite* s = &iw->gameOverSprite;
if(iw->gameOverTimer > 0.5)
{
float t = iw->gameOverTimer - 0.5;
//t *= t;
s->alpha = t * 4;
s->scale.x = max(4 - (t*16), 1);
s->scale.y = max(2 - (t*4), 1);
rectangleDraw(g, 0, 0, g->size.x, g->size.y, 0.15, 0.15, 0.3, t * 0.3);
spriteDraw(g, s, cx, cy);
}
}
else
{
if(iw->boss && iw->boss->controller->boss.triggered)
{
ControllerBoss* bc = &iw->boss->controller->boss;
textureDrawExt(g, assetsGetTexture(g, "bossHealthBar"),
24, 16, 912, 32,
0, 0, 912, 32,
0, 0, 1, 1, 0, 1,
0, makeColor(1, 1, 1, 1));
float hp = (float)bc->health / bc->maxHealth;
textureDrawExt(g, assetsGetTexture(g, "bossHealthBar"),
24, 16, 2 + (908 * hp), 32,
0, 32, 2 + (908 * hp), 32,
0, 0, 1, 1, 0, 1,
0, makeColor(1, 1, 1, 1));
}
}
//rectangleDraw(g, iw->debugDrawPosition.x, iw->debugDrawPosition.y, 4, 4, 1, 0, 1, 1);
}
// Lighting test stuff
WallGeometry wg;
wallGeometryInit(&wg);
wallGeometryCalculate(&wg, &iw->level);
wallGeometryDraw(&wg, g);
// End of lighting test stuff
textInputDraw(&iw->textInput, iw);
renderEnd(g);
}
#undef main
int main(int argc, char** argv)
{
Iwbtg* iwbtg = new(Iwbtg);
Game* game = &iwbtg->game;
gameInit(game, "iwbtg", 960, 540, 1);
iwbtgLoad(iwbtg);
iwbtgInit(iwbtg);
loadRoomFile(iwbtg, "menu");
//loadMap(iwbtg, "assets/1.map");
while(iwbtg->game.running)
{
double frameTime = (double)1000.0 / 50.0;
int updateTime = SDL_GetTicks();
game->frameTimer += updateTime - game->lastUpdateTime;
while(game->frameTimer >= frameTime)
{
gameHandleEvents(&iwbtg->game);
iwbtgUpdate(iwbtg);
game->frameTimer -= frameTime;
}
iwbtgDraw(iwbtg);
memoryPoolClear(&game->frameMemory);
game->lastUpdateTime = updateTime;
}
}