-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
632 lines (616 loc) · 22.8 KB
/
Main.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
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
//local classes
#include "audio.h"
#include "button.h"
#include "Texture.h"
#include "timer.h"
#include "particle.h"
#include "chapter.h"
#include "saveGame.h"
#include "pregameui.h"
#include "animations.h"
#include "stage.h"
#include "text.h"
const int TOTAL_STATES = 8;
//gameStates
//gameState = 0 pregameui
//gameState = 1 new game chapter select
//gameState = 2 load game chapter select
//gameState = 3 options
//gameState = 4 credits
//gameState = 5 chapter 1
//gameState = 6 stage 1
//gameState = 7 chapter 2
//gameState = 8 chapter 3
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//The window renderer
SDL_Renderer* renderer = NULL;
//The window we'll be rendering to
SDL_Window* window = NULL;
//savegame handler
saveGame savegame;
audio music;
std::vector<audio> sounds;
audio sound;
//declare chapter1
chapter chapter;
//declare stage
stage stage;
//declare instance of pregame user interface.
pregameui pregameui;
//declare instance of animations
animations animations;
//set up text input
text text;
//if player is not verified they wont progress to ch 3.
bool verified = false;
//declare sound vector & load sounds into it.
void loadSounds();
//render particles to screen
//tracks the state of the game for rendering etc.
int gameState;
//initializes audio,video, etc.
Texture blackGround;
bool fadeOut(int countedFrames, bool fade);
bool fadeIn(int countedFrames,bool fade);
bool fadeIn(int countedFrames, bool fade)
{
Uint32 alpha = 255-(countedFrames%256);
blackGround.setAlpha(alpha);
if(countedFrames%256>254)
{
fade=false;
}
return fade;
}
bool fadeOut(int countedFrames, bool fade)
{
if(countedFrames%256 < 255)
{ //fades in the go homeless title
blackGround.setAlpha(countedFrames);
}
if(countedFrames%256>=255)
{
fade=false;
}
return fade;
}
bool init()
{
//Initialization flag
bool success = true;
gameState = 0;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 )
{
printf( "\n SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
printf( "\n Warning: Linear texture filtering not enabled!" );
}
window = SDL_CreateWindow( "Go Homeless!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE );
if( window == NULL )
{
printf( "\n Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
if( renderer == NULL )
{
printf( "\n Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
//create particles
pregameui.createParticles(renderer);
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf( "\n SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
success = false;
}
}
if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 )
{
printf( "\n SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
if( TTF_Init() == -1 )
{
printf( "\n SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
}
return success;
}
void loadSounds()
{
//prep the vector
for(int i = 0; i < TOTAL_SOUNDS; i++)
{
sounds.push_back(sound);
}
//load the sounds for each point in the vector
for(int i = 0; i < TOTAL_SOUNDS; i++)
{
sounds[i].loadSound(i);
}
}
bool loadMedia()
{
bool success = true;
//load saved game
savegame.readFile();
//pre-game chapter set up.
success = chapter.loadChapters(savegame.data[0],savegame.data[1],savegame.data[2],savegame.data[3],savegame.data[4],savegame.data[5],renderer,success);
//load animations textures
success = animations.setAnimationTextures(renderer);
//sets up pregame ui button names, button textures, and bg textures.
success = pregameui.loadPregameUI(renderer,success);
//loads stage textures, button names, and button textures
success = stage.loadStage(renderer,success);
//load sound effects
loadSounds();
//fade to black background
blackGround.loadFromFile("images/Blackground.png",renderer);
//sets strings for verification page, loads those strings into their textures,loads verification page bg
text.loadText(chapter.font,renderer);
//for debugging
if(success == false)
{
printf("something didn't load right in loadmedia.");
}
return success;
}
void close()
{
//save progress
savegame.writeFile(chapter.currentChapter,chapter.currentPage,chapter.currentScript,chapter.chapter1Complete,chapter.chapter2Complete,chapter.chapter3Complete);
//free chapter resources
chapter.free();
//free pregame ui resouces
pregameui.free();
//free stage resources
stage.free();
//free animation textures
animations.freeAnimationTextures();
//free the background used to fade in / out from black
blackGround.free();
//free text resources
text.free();
//audio destructor frees audio
music.freeAudio();
sound.freeAudio();
while(!sounds.empty()){
sounds.pop_back();
}
//Destroy window
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
window = NULL;
renderer = NULL;
//Quit SDL subsystems
TTF_Quit();
Mix_Quit();
IMG_Quit();
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Main loop flag
bool quit = false;
bool fade=false;
//load the bg music file
music.loadMusic();
music.playMusic();
//The frames per second cap timer
timer capTimer;
//Start counting frames per second
int countedFrames = 0;
//Event handler
SDL_Event e;
//While application is running
while( !quit )
{
//Start cap timer
capTimer.start();
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
if(e.type == SDL_MOUSEBUTTONDOWN)
{
if(gameState == 8)//chapter 3
{
if(!verified)
{
verified = text.verifyNoRobo();
}
else
{
int buttonClicked = 0;
for( int i = 0; i < TOTAL_CHAPTER_BUTTONS; ++i )
{
buttonClicked += chapter.buttons[ i ].handleChapterEvent(chapter.buttons[i].buttonName, &e, window,renderer );
}
chapter.handleChapterButtonPresses(buttonClicked,renderer);
if( chapter.exitChapter || buttonClicked == 1)
{
gameState=0;
music.resetMusic();
chapter.exitChapter = false;
}
}
}
if(gameState == 7)
{//player clicks mouse inside chapter 1 or 2
int buttonClicked = 0;
//buttonClicked takes the sum of all the button checks, 0 is outside of any buttons
for( int i = 0; i < TOTAL_CHAPTER_BUTTONS; ++i )
{
buttonClicked += chapter.buttons[ i ].handleChapterEvent(chapter.buttons[i].buttonName, &e, window,renderer );
}
chapter.handleChapterButtonPresses(buttonClicked,renderer);
if(chapter.exitChapter || buttonClicked == 1)
{//player clicked back button or player completed chapter 2
gameState=0;
music.resetMusic();
chapter.exitChapter = false;
}
}
if(gameState == 5)
{//player clicks mouse inside chapter 1 or 2
int buttonClicked = 0;
//buttonClicked takes the sum of all the button checks, 0 is outside of any buttons
for( int i = 0; i < TOTAL_CHAPTER_BUTTONS; ++i )
{
buttonClicked += chapter.buttons[ i ].handleChapterEvent(chapter.buttons[i].buttonName, &e, window,renderer );
}
chapter.handleChapterButtonPresses(buttonClicked,renderer);
if(chapter.exitChapter || buttonClicked == 1)
{//player clicked back button or player completed chapter 2
gameState=0;
music.resetMusic();
chapter.exitChapter=false;
}
}
//Handle button events when in pregame ui (opening, newgame, loadgame,options,and credits).
if(gameState <5)
{
//chapters get reset here, but I may have the actual chapter 1 button on this screen determine the reset.
//chapter.resetChapters(renderer);
int oldGameState = gameState;
for( int i = 0; i < TOTAL_PREGAME_BUTTONS; ++i )
{
gameState = pregameui.buttons[ i ].handlePGUIEvent(gameState,pregameui.buttons[i].buttonName, &e, window,renderer );
}
if(gameState==1)
{//user clicks the newGame button
chapter.resetChapters(renderer);
}
if(gameState==3)
{//user toggles music on or off
if(pregameui.buttons[10].musicOn)
{
music.musicOn = true;
if(!music.isPlaying())
{
music.playMusic();
}
}
else
{
music.musicOn = false;
if(music.isPlaying())
{
music.stopMusic();
}
}
if(pregameui.buttons[11].voiceOn)
{
chapter.voice.voiceOn = true;
}
else
{
chapter.voice.voiceOn = false;
}
}
if(gameState == 5)
{//user clicks chapter 1 button
if(chapter.currentChapter != 0)
{
chapter.currentChapter = 0;
chapter.resetPages();
}
music.resetChapter1Music();
chapter.loadChapter(renderer);
}
else if(gameState == 7)
{//user clicks chapter 2 button
if(chapter.currentChapter!=1)
{
chapter.currentChapter =1;
chapter.resetPages();
}
music.resetChapter2Music();
chapter.loadChapter(renderer);
}
else if(gameState == 8)
{//user clicks chapter 3 button
if(chapter.currentChapter!=2)
{
chapter.currentChapter =2;
chapter.resetPages();
}
music.resetChapter3Music();
chapter.loadChapter(renderer);
}
if(gameState != oldGameState)
{
int newGameState = gameState;
gameState = oldGameState;
fade=true;
fade=fadeOut(countedFrames,fade);
gameState=newGameState;
}
}
}
//Handle key press
if( e.type == SDL_KEYDOWN )
{
if(gameState==8 && !verified)
{
//Handle backspace
if( e.key.keysym.sym == SDLK_BACKSPACE && text.inputText.str().length() > 0 )
{
text.inputText.str("");
}
//Handle copy
else if( e.key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL )
{
SDL_SetClipboardText( text.inputText.str().c_str() );
}
//Handle paste
else if( e.key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL )
{
text.inputText.str() = SDL_GetClipboardText();
}
}
switch( e.key.keysym.sym )
{
case SDLK_SPACE:
if(gameState ==8)
{
//update script line, page, end chapter
chapter.progress2(renderer);
if(chapter.exitChapter)
{
gameState=0;
chapter.exitChapter = false;
}
}
else if(gameState==7)
{
//update script line, page, end chapter
chapter.progress2(renderer);
if(chapter.exitChapter)
{
gameState=0;
chapter.exitChapter = false;
}
}
else if(gameState==5)
{
//update script line, page, end chapter
chapter.progress2(renderer);
if(chapter.exitChapter)
{
gameState=0;
chapter.exitChapter = false;
}
}
break;
case SDLK_ESCAPE:
if(gameState==0)
{
quit=true;
}
break;
//sound tests
case SDLK_1:
sounds[0].playSound();
break;
case SDLK_2:
sounds[1].playSound();
break;
case SDLK_3:
sounds[2].playSound();
break;
case SDLK_4:
sounds[3].playSound();
break;
case SDLK_h:
//press h to cycle dialog visability
chapter.handleDialogVisability();
break;
case SDLK_9:
//press 9 to play / pause music
music.playMusic();
break;
}
}
else if( gameState == 8 && verified==false && e.type == SDL_TEXTINPUT )
{
//Not copy or pasting
if( !( SDL_GetModState() & KMOD_CTRL && ( e.text.text[ 0 ] == 'c' || e.text.text[ 0 ] == 'C' || e.text.text[ 0 ] == 'v' || e.text.text[ 0 ] == 'V' ) ) )
{
text.inputText << e.text.text;
}
verified = text.verifyNoRobo();
if(verified)
{//somehow currentScript was greater than 0 without this
chapter.currentScript = 0;
}
}
//if wasd are pressed player will be moved.
stage.player1.handleEvent(e);
}
//process player movement
stage.player1.move();
//Clear screen
SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( renderer );
//Title Screen rendering
if(gameState == 0)
{
chapter.chapterTimer.stop();
//renders buttons, bg, title image, and handles particle effect.
pregameui.handleTitleScreenRendering(renderer);
}
else if(gameState == 1)
{//new game chapter select screen
chapter.chapterTimer.stop();
pregameui.handleNewGameScreenRendering(renderer);
}
else if(gameState == 2)
{//load game chapter/stage select screen
chapter.chapterTimer.stop();
//handles the buttons and background rendering
pregameui.handleLoadGameScreenRendering(renderer,chapter.chapter1Complete,chapter.chapter2Complete);
}
else if(gameState == 3)
{
//options screen buttons/bg rendering
pregameui.handleOptionsScreenRendering(renderer);
}
//chapter 1 is in effect
else if(gameState == 5)
{
//Chapter rendering (buttons and backgrounds and text.
chapter.handleRendering(renderer);
//render tao animation
animations.renderTao(renderer);
//render rat animation
animations.renderRat(renderer);
}
//if chapter 2 is in effect
else if(gameState == 7)
{
chapter.handleRendering(renderer);
//rat animation rendered after chapter background, dialog box are rendered.
animations.renderRat(renderer);
if(chapter.currentPage == 3)
{
animations.renderToaster2(renderer);
}
else
{
animations.resetToaster2();
}
if(chapter.currentPage == 4)
{
animations.renderBlackstar(renderer);
}
if(chapter.currentPage == 5)
{
animations.renderPortal(renderer);
}
else
{
animations.resetPortal();
}
}
else if(gameState == 8)//render chapter 3 no robo verification, if passed, render chapter 3
{
if(!verified)
{
text.renderVerification(chapter.font,renderer);
}
else
{
if(chapter.currentPage == 6)
{
animations.renderTrailer(renderer);
}
//Chapter rendering (buttons and backgrounds and text.
chapter.handleRendering(renderer);
//rat is rendered on top of dialog box in lower right hand corner. (after verification)
animations.renderRat(renderer);
}
}
else if(gameState == 4)
{
//render background image and back button
pregameui.handleCreditsScreenRendering(renderer);
//robot animation
animations.renderToaster(renderer);
//animations.renderToaster2(renderer);
}
else if(gameState == 6)
{
//renders bg, stage ui buttons and player
stage.renderStage1(renderer);
}
if(fade)
{
fade = fadeIn(countedFrames,fade);
blackGround.render(0,0,NULL,0.0,NULL,SDL_FLIP_NONE,renderer);
}
//Update screen
SDL_RenderPresent( renderer );
++countedFrames;
if(countedFrames>0 && countedFrames < 255 && gameState==0)
{ //fades in the go homeless title
pregameui.title.setAlpha(countedFrames);
}
//If frame finished early
int frameTicks = capTimer.getTicks();
if( frameTicks < SCREEN_TICK_PER_FRAME )
{
//Wait remaining time
SDL_Delay( SCREEN_TICK_PER_FRAME - frameTicks );
}
//increments frames for animations
animations.progress();
//set script line
chapter.setAutoScript();
}
//Disable text input
SDL_StopTextInput();
}
}
//Free resources and close SDL
close();
return 0;
}