-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
963 lines (829 loc) · 23.9 KB
/
window.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
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
#include "window.h"
#include <limits>
#include <cstdio>
#include <cassert>
#include <string>
static void DrawColoredRect(SDL_Renderer* renderer, const SDL_Rect& r, const SDL_Color& c);
static GridIndex SnakeHelper(SDL_Scancode code, const Game& g);
Window::Window(int windowWidth, int windowHeight) :
mWindowWidth(windowWidth),
mWindowHeight(windowHeight)
{
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0)
{
printf("SDL could not initialize. SDL_Error %s", SDL_GetError());
exit(1);
}
else
{
//Set texture filtering to linear
if ( not SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Warning: Linear texture filtering not enabled!");
}
// Create window
mWindow = SDL_CreateWindow("Snake",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
windowWidth,
windowHeight,
SDL_WINDOW_SHOWN);
if(mWindow == nullptr)
{
printf("Window could not be created. SDL_Error %s", SDL_GetError());
exit(1);
}
else
{
// Check for joystick
if( SDL_NumJoysticks() < 1 )
{
printf("No JoyStick Connected!\n");
}
else
{
// Load joystick
mController = SDL_GameControllerOpen(0);
if ( mController == nullptr )
printf("Unable to open game controller. SDL Error: %s\n", SDL_GetError());
}
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (mRenderer == nullptr)
{
printf("Renderer failed");
exit(1);
}
//Initialize SDL_ttf
if (TTF_Init() > 0)
{
printf("SDL_ttf did not initialize. SDL_ttf Error: %s\n", TTF_GetError());
exit(1);
}
//Open the font
const int fontSize{ 64 };
mFont.mFontP = TTF_OpenFont("major-mono-display-v2-latin-regular.ttf", fontSize);
if (mFont.mFontP == nullptr)
{
printf("Failed to load font! SDL_ttf Error: %s\n", TTF_GetError());
exit(1);
}
// Initialize SDL_mixer
int stereo{ 2 };
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, stereo, 2048) < 0)
{
printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
// Load Music
mMusic = Mix_LoadMUS("Audio/MySong3(bass).mp3");
if(mMusic == nullptr)
{
printf("Music failed to load. SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
//Load sound effects
mEatingSound = Mix_LoadWAV("Audio/eatingFX.wav");
if (mEatingSound == nullptr)
{
printf("Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
mSnareSound = Mix_LoadWAV("Audio/snare.wav");
if (mEatingSound == nullptr)
{
printf("Failed to load snare sound effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
mKickSound = Mix_LoadWAV("Audio/kick01.wav");
if (mEatingSound == nullptr)
{
printf("Failed to load kick sound effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
mHHSound = Mix_LoadWAV("Audio/hihat02.wav");
if (mEatingSound == nullptr)
{
printf("Failed to load hihat sound effect! SDL_mixer Error: %s\n", Mix_GetError());
exit(1);
}
//Initialize renderer color
SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF);
}
UpdateUpdateTimer();
}
}
Window::~Window()
{
// Free controller
SDL_GameControllerClose(mController);
mController = nullptr;
// Free Texture
FreeTexture(mTexture);
mTexture = nullptr;
// Free sound effects
Mix_FreeChunk(mHHSound);
mHHSound = nullptr;
// Free sound effects
Mix_FreeChunk(mKickSound);
mKickSound = nullptr;
// Free sound effects
Mix_FreeChunk(mSnareSound);
mSnareSound = nullptr;
// Free sound effects
Mix_FreeChunk(mEatingSound);
mEatingSound = nullptr;
// Free Music
Mix_FreeMusic(mMusic);
mMusic = nullptr;
//Free font
TTF_CloseFont(mFont.mFontP);
mFont.mFontP = nullptr;
// Deallocate renderer
SDL_DestroyRenderer(mRenderer);
mRenderer = nullptr;
//Destroy window
SDL_DestroyWindow(mWindow);
mWindow = nullptr;
Mix_Quit();
TTF_Quit();
SDL_Quit();
}
Screen
Window::SelectScreen()
{
//Clear screen
SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(mRenderer);
Screen state{ Screen::SELECT };
puts("test");
// std::string options{ "1. Play Normal Snake\n2. Replay Mode\n3. Nothing\n4. Nothing\n5. Exit" };
// Draw start screen
std::string optionOne{ "1. Normal Snake" };
std::string optionTwo{ "2. Replay Mode" };
std::string optionThree{ "3. Drum Mach" };
std::string optionFour{ "4. Nothing" };
std::string optionFive{ "5. Exit" };
//Set rendering space and render to screen
SDL_Rect QuadOne{ 10, 0, mWindowWidth / 2, 100 };
SDL_Rect QuadTwo{ 10, 110, mWindowWidth / 2, 100 };
SDL_Rect QuadThree{ 10, 220, mWindowWidth / 2, 100 };
SDL_Rect QuadFour{ 10, 330, mWindowWidth / 2, 100 };
SDL_Rect QuadFive{ 10, 440, mWindowWidth / 2, 100 };
//Render to screen
StringDrawHelper(optionOne, QuadOne);
StringDrawHelper(optionTwo, QuadTwo);
StringDrawHelper(optionThree, QuadThree);
StringDrawHelper(optionFour, QuadFour);
StringDrawHelper(optionFive, QuadFive);
//Render to screen
SDL_RenderPresent(mRenderer);
while (state == Screen::SELECT)
{
SDL_Event e;
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
state = Screen::EXIT;
}
else if (e.type == SDL_CONTROLLERBUTTONDOWN)
{
switch (e.cbutton.button)
{
// case SDL_CONTROLLER_BUTTON_BACK:
// Reset(g);
// g.mGameState = GameState::RUN;
// break;
// case SDL_CONTROLLER_BUTTON_START:
// g.mGameState = GameState::QUIT;
// break;
};
}
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_1:
state = Screen::PLAY;
break;
case SDLK_2:
state = Screen::REPLAY;
break;
case SDLK_3:
state = Screen::DRUM;
break;
case SDLK_4:
state = Screen::SELECT; // SEQ;
break;
case SDLK_ESCAPE:
case SDLK_5:
state = Screen::EXIT;
break;
};
}
}
}
return state;
}
void
Window::UpdateUpdateTimer(unsigned score)
{
score = { score > 0 ? score : 1 };
mNextUpdate = SDL_GetTicks() + (200 / score);
}
void
Window::PollEvents(Game& g)
{
if (g.mGameState == GameState::RUN or g.mGameState == GameState::PAUSE)
{
RunEventLoop(g);
}
else if (g.mGameState == GameState::START)
{
StartEventLoop(g);
}
else if (g.mGameState == GameState::STOP)
{
StopEventLoop(g);
}
}
static GridIndex
SnakeHelper(SDL_Scancode code, const Game& g)
{
int newHeadIndex{ g.GetSnakeHead() };
int x{ g.GetSnakeHead() % g.mWidthOfGrid };
int y{ g.GetSnakeHead() / g.mWidthOfGrid };
if (code == SDL_SCANCODE_UP and code != SDL_SCANCODE_DOWN)
{
if ((y - 1) >= 0)
{
newHeadIndex -= g.mWidthOfGrid;
}
}
else if (code == SDL_SCANCODE_DOWN and code != SDL_SCANCODE_UP)
{
if ((y + 1) < g.mHeightOfGrid)
newHeadIndex += g.mWidthOfGrid;
}
else if (code == SDL_SCANCODE_LEFT and code != SDL_SCANCODE_RIGHT)
{
if ((x - 1) >= 0)
newHeadIndex -= 1;
}
else if (code == SDL_SCANCODE_RIGHT and code != SDL_SCANCODE_LEFT)
{
if ((x + 1) < g.mWidthOfGrid)
newHeadIndex += 1;
}
return newHeadIndex;
}
GridIndex
Window::HandleInput(Game& g)// might want to to this async
{
int newHeadIndex {g.GetSnakeHead()};
//int entityWidth { mWindowWidth / g.mWidthOfGrid };
//int entityHeight { mWindowHeight / g.mHeightOfGrid};
PollEvents(g); //TODO:: need to refactored to just return event not change the game state
int x{ g.GetSnakeHead() % g.mWidthOfGrid };
int y{ g.GetSnakeHead() / g.mWidthOfGrid };
const Uint8* currentKeyStates = SDL_GetKeyboardState( nullptr );
if( MovedUp(currentKeyStates) )
{
if( (y - 1) >= 0)
{
newHeadIndex -= g.mWidthOfGrid;
mPrevScanCode = SDL_SCANCODE_UP;
}
}
else if( MovedDown(currentKeyStates) )
{
if ((y + 1) < g.mHeightOfGrid)
{
newHeadIndex += g.mWidthOfGrid;
mPrevScanCode = SDL_SCANCODE_DOWN;
}
}
else if( MovedLeft(currentKeyStates) )
{
if ((x - 1) >= 0)
{
newHeadIndex -= 1;
mPrevScanCode = SDL_SCANCODE_LEFT;
}
}
else if( MovedRight(currentKeyStates) )
{
if ((x + 1) < g.mWidthOfGrid)
{
newHeadIndex += 1;
mPrevScanCode = SDL_SCANCODE_RIGHT;
}
}
else
{
newHeadIndex = SnakeHelper(mPrevScanCode, g);
}
return newHeadIndex;
}
void
Window::TryToPlayMusic()
{
if (Mix_PlayingMusic() == 0 and mMusic != nullptr)
Mix_PlayMusic(mMusic, -1);
}
void
Window::TryToPlaySoundFX()
{
if(mEatingSound != nullptr)
Mix_PlayChannel(-1, mEatingSound, 0);
}
void
Window::TryToPlayDrumSound(const FoodType& f)
{
switch (f)
{
case FoodType::BANNANA:
{
if(mSnareSound != nullptr)
Mix_PlayChannel(-1, mSnareSound, 0);
break;
}
case FoodType::COCONUT:
{
if(mKickSound != nullptr)
Mix_PlayChannel(-1, mKickSound, 0);
break;
}
case FoodType::ORANGE:
{
if(mHHSound != nullptr)
Mix_PlayChannel(-1, mHHSound, 0);
break;
}
case FoodType::WATERMELLON:
{
// skip this is for the seq so you can build
// seq with rests
break;
}
case FoodType::NUMOFFOOD:
default:
{}// don't care about this one
};
}
void
Window::Draw(const Game& game)
{
//Clear screen
SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(mRenderer);
// Make score string
std::string score{ "000000" };
std::string tempScore{ std::to_string(game.mScore) };
if (tempScore.size() <= score.size())
score.replace(score.size() - tempScore.size(), tempScore.size(), tempScore);
else
score = "PRO!!";
// Draw game
if (game.mGameState == GameState::RUN)
{
DrawGameHelper(game, score);
}
// Draw pause menu
else if (game.mGameState == GameState::PAUSE)
{
DrawPauseHelper(game);
}
else if (game.mGameState == GameState::START)
{
DrawStartMenu(game);
}
// Draw Game over
else // (game.mGameState == GameState::STOP)
{
DrawStopHelper(game, score);
}
SDL_RenderPresent(mRenderer);
}
bool
Window::LoadTextureFromText(const std::string& text, const SDL_Color& color)
{
//Get rid of preexisting texture
FreeTexture(mTexture);
//Render text surface
SDL_Surface* textSurface{ TTF_RenderText_Solid(mFont.mFontP, text.c_str(), color) };
if (textSurface == NULL)
{
printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
}
else
{
//Create texture from surface pixels
mTexture = SDL_CreateTextureFromSurface(mRenderer, textSurface);
if (mTexture == NULL)
{
printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
}
else
{
//Get image dimensions
mFont.mWidth = textSurface->w;
mFont.mHeight = textSurface->h;
}
//Get rid of old surface
SDL_FreeSurface(textSurface);
}
//Return success
return mTexture != nullptr;
}
void
Window::FreeTexture(SDL_Texture* texture)
{
if (texture != nullptr)
{
SDL_DestroyTexture(texture);
texture = nullptr;
}
}
static void
DrawColoredRect(SDL_Renderer* renderer, const SDL_Rect& rect, const SDL_Color& c)
{
SDL_SetRenderDrawColor(renderer, c.r, c.g, c.g, c.a);
SDL_RenderFillRect(renderer, &rect);
}
void Window::Reset(Game& g)
{
g.Reset();
mPrevScanCode = SDL_Scancode::SDL_SCANCODE_UP;
}
void
Window::DrawStartMenu(const Game& game)
{
// Draw start screen
SDL_Color textColor = { 0xFF, 0xFF, 0xFF, 0xFF };
if (not LoadTextureFromText("SNAKE", textColor))
{
printf("Failed to render text texture!\n");
}
//Set rendering space and render to screen
SDL_Rect renderQuad = { mWindowWidth / 4, mWindowHeight/4, mFont.mWidth, mFont.mHeight };
//Render to screen
SDL_RenderCopy(mRenderer, mTexture, nullptr, &renderQuad);
std::string name = game.mPlayer;
// Draw user name
if (game.mPlayer != "")
{
if (not LoadTextureFromText(name.c_str(), textColor))
{
printf("Failed to render text texture!\n");
}
//Set rendering space and render to screen
renderQuad = { (mWindowWidth / 4), (mWindowHeight / 4) + mFont.mHeight, mFont.mWidth, mFont.mHeight };
//Render to screen
SDL_RenderCopy(mRenderer, mTexture, nullptr, &renderQuad);
}
}
void
Window::DrawGameHelper(const Game& game, const std::string& score)
{
//Top left corner viewport
SDL_Rect topViewport{ 0, 0, mWindowWidth, mWindowHeight / 8 };
SDL_RenderSetViewport(mRenderer, &topViewport);
// Draw user name and score
SDL_Color textColor = { 0xFF, 0xFF, 0xFF, 0xFF };
if (not LoadTextureFromText("PLAYER: "+ game.mPlayer +" SCORE: " + score + " ", textColor))
{
printf("Failed to render text texture!\n");
}
//Set rendering space and render to screen
SDL_Rect renderQuad = { 0, 0, topViewport.w, topViewport.h };
//Render to screen
SDL_RenderCopy(mRenderer, mTexture, nullptr, &renderQuad);
//Bottom viewport
SDL_Rect bottomViewport{ 0, mWindowHeight / 8, mWindowWidth, mWindowHeight - (mWindowHeight / 8) };
SDL_RenderSetViewport(mRenderer, &bottomViewport);
int entityWidth{ bottomViewport.w / game.mWidthOfGrid };
int entityHeight{ bottomViewport.h / game.mHeightOfGrid };
// Draw fruit
SDL_Rect fruitRect{ 0, 0, entityWidth, entityHeight };
// Orange, White, Yellow, Green
SDL_Color color[] = { { 255, 165, 0, 255 }, {255, 255, 255, 255}, {255, 215, 0, 255}, {10, 255, 10, 255} };
for (auto f : game.mFood)
{
fruitRect.x = (f.mIndex % game.mWidthOfGrid) * entityWidth;
fruitRect.y = (f.mIndex / game.mHeightOfGrid) * entityHeight;
DrawColoredRect(mRenderer, fruitRect, color[f.mType]);
}
// Rect reminder {x, y, w, h}
SDL_Rect snakeRect{ 0, 0, entityWidth, entityHeight };
//Draw RED snake
SDL_SetRenderDrawColor(mRenderer, 0xFF, 0x00, 0x00, 0xFF);
game.mSnake.foreach(
[&](int snakePart) {
snakeRect.x = (snakePart % game.mWidthOfGrid) * entityWidth;
snakeRect.y = (snakePart / game.mHeightOfGrid) * entityHeight;
SDL_RenderFillRect(mRenderer, &snakeRect);
}
);
// ReDraw the snake head green for debugging
SDL_SetRenderDrawColor(mRenderer, 0x00, 0xFF, 0x00, 0xFF);
snakeRect.x = (game.GetSnakeHead() % game.mWidthOfGrid) * entityWidth;
snakeRect.y = (game.GetSnakeHead() / game.mHeightOfGrid) * entityHeight;
SDL_RenderFillRect(mRenderer, &snakeRect);
//Draw Dividing line Top
SDL_SetRenderDrawColor(mRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderDrawLine(mRenderer, 0, 0, bottomViewport.w, 0);
SDL_RenderDrawLine(mRenderer, 0, 0 + 1, bottomViewport.w, 0 + 1);
//Draw Dividing line Bottom
//SDL_SetRenderDrawColor(mRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderDrawLine(mRenderer, 0, (game.mHeightOfGrid * entityHeight), bottomViewport.w,
(game.mHeightOfGrid * entityHeight));
SDL_RenderDrawLine(mRenderer, 0, (game.mHeightOfGrid * entityHeight) - 1, bottomViewport.w,
(game.mHeightOfGrid * entityHeight) - 1);
//Draw Dividing line Right
//SDL_SetRenderDrawColor(mRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderDrawLine(mRenderer, 0, (game.mHeightOfGrid * entityHeight),
(game.mWidthOfGrid * entityWidth) - 1, (game.mHeightOfGrid * entityHeight));
SDL_RenderDrawLine(mRenderer, 0, (game.mHeightOfGrid * entityHeight),
(game.mWidthOfGrid * entityWidth) - 1, (game.mHeightOfGrid * entityHeight));
//Draw Dividing line Left
//SDL_SetRenderDrawColor(mRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderDrawLine(mRenderer, 0, 0, 0, (game.mHeightOfGrid * entityHeight));
SDL_RenderDrawLine(mRenderer, (game.mWidthOfGrid * entityWidth) - 1, 0,
(game.mWidthOfGrid * entityWidth) - 1, (game.mHeightOfGrid * entityHeight));
//Debug stuff
bool DEBUG{ false };
if ( DEBUG )
{
SDL_SetRenderDrawColor(mRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
for (int i = 0; i < game.mWidthOfGrid; ++i)
{
SDL_RenderDrawLine(mRenderer, i * entityWidth, 0, i * entityWidth, bottomViewport.h);
}
for (int i = 1; i < game.mHeightOfGrid; ++i)
{
SDL_RenderDrawLine(mRenderer, 0, i * entityHeight, bottomViewport.w, i * entityHeight);
}
printf("%d ", game.GetSnakeHead());
}
}
void
Window::DrawPauseHelper(const Game& game)
{
SDL_Color textColor = { 0xFF, 0xFF, 0xFF, 0xFF };
if (not LoadTextureFromText("PAUSED ", textColor))
{
printf("Failed to render text texture!\n");
}
//Set rendering space and render to screen
SDL_Rect renderQuad = { mWindowWidth / 2, mWindowHeight / 2, mFont.mWidth, mFont.mHeight };
//Render to screen
SDL_RenderCopy(mRenderer, mTexture, nullptr, &renderQuad);
}
void
Window::DrawStopHelper(const Game& game, const std::string& score)
{
SDL_Color textColor = { 0xFF, 0xFF, 0xFF, 0xFF };
if (not LoadTextureFromText("GAME OVER!", textColor))
{
printf("Failed to render text texture!\n");
}
//Set rendering space and render to screen
SDL_Rect renderQuad = { mWindowWidth / 8, mWindowHeight / 2, mFont.mWidth, mFont.mHeight };
//Render to screen
SDL_RenderCopy(mRenderer, mTexture, nullptr, &renderQuad);
if (not LoadTextureFromText("Score: " + score, textColor))
{
printf("Failed to render text texture!\n");
}
//Set rendering space and render to screen
renderQuad = { mWindowWidth / 8, (mWindowHeight / 2) + mFont.mHeight, mFont.mWidth, mFont.mHeight };
SDL_RenderCopy(mRenderer, mTexture, nullptr, &renderQuad);
// TODO:: need to add high score display
}
void
Window::RunEventLoop(Game& g)
{
SDL_Event e;
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
g.mGameState = GameState::QUIT;
}
else if (e.type == SDL_CONTROLLERBUTTONDOWN)
{
switch (e.cbutton.button)
{
case SDL_CONTROLLER_BUTTON_BACK:
g.mGameState = GameState::QUIT;
break;
case SDL_CONTROLLER_BUTTON_START:
// should change to pause
if (g.mGameState != GameState::PAUSE)
g.mGameState = GameState::PAUSE;
else
g.mGameState = GameState::RUN;
break;
};
}
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_0:
if (g.mWidthOfGrid <= 500 and g.mHeightOfGrid <= 500)
{
g.mWidthOfGrid *= 2;
g.mHeightOfGrid *= 2;
g.UpdateSnake(g.GetSnakeHead());
}
break;
case SDLK_MINUS:
if (g.mWidthOfGrid > 2 and g.mHeightOfGrid > 2)
{
g.mWidthOfGrid /= 2;
g.mHeightOfGrid /= 2;
g.UpdateSnake(g.GetSnakeHead() / 2);
}
break;
case SDLK_SPACE:
// should change to pause
if (g.mGameState != GameState::PAUSE)
g.mGameState = GameState::PAUSE;
else
g.mGameState = GameState::RUN;
break;
case SDLK_ESCAPE:
g.mGameState = GameState::STOP;
break;
};
}
}
}
void
Window::StartEventLoop(Game& g)
{
SDL_Event e;
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_TEXTINPUT)
{
//Not copy or pasting
if (g.mPlayer.size() <= 3 and not ((e.text.text[0] == 'c' or
e.text.text[0] == 'C') and (e.text.text[0] == 'v' or
e.text.text[0] == 'V') && SDL_GetModState() & KMOD_CTRL))
{
//Append character
g.mPlayer += e.text.text;
//renderText = true;
}
}
//User requests quit
if (e.type == SDL_QUIT)
{
g.mGameState = GameState::QUIT;
}
else if (e.type == SDL_KEYDOWN)
{
//Special text input event
// Special key input
if (e.type == SDL_KEYDOWN)
{
//Handle backspace
if (e.key.keysym.sym == SDLK_BACKSPACE and g.mPlayer.length() > 0)
{
//lop off character
g.mPlayer.pop_back();
}
//Handle copy
else if (e.key.keysym.sym == SDLK_c and SDL_GetModState() & KMOD_CTRL)
{
SDL_SetClipboardText(g.mPlayer.c_str());
}
//Handle paste
else if (e.key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL)
{
g.mPlayer = SDL_GetClipboardText();
}
else if (e.key.keysym.sym == SDLK_ESCAPE)
{
g.mGameState = GameState::QUIT;
}
else if (e.key.keysym.sym == SDLK_RETURN)
{
g.mGameState = GameState::RUN;
}
}
}
}
}
void
Window::StopEventLoop(Game& g)
{
SDL_Event e;
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
g.mGameState = GameState::QUIT;
}
else if (e.type == SDL_CONTROLLERBUTTONDOWN)
{
switch (e.cbutton.button)
{
case SDL_CONTROLLER_BUTTON_BACK:
Reset(g);
g.mGameState = GameState::RUN;
break;
case SDL_CONTROLLER_BUTTON_START:
g.mGameState = GameState::QUIT;
break;
};
}
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_ESCAPE:
g.mGameState = GameState::QUIT;
break;
case SDLK_r:
Reset(g);
g.mGameState = GameState::RUN;
break;
};
}
}
}
void
Window::DrawReplaySelect(const std::vector<ReplayMode>& games, int index)
{
index += 1; // this is need to make the graphics draw the selector in the right spot 0 * height = 0
//Clear screen
SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(mRenderer);
int selectionIndicator{ 50 };
int i{ 1 };
for (const ReplayMode& game : games)
{
// Draw user name and score
SDL_Color textColor = { 0xFF, 0xFF, 0xFF, 0xFF };
if (not LoadTextureFromText(game.mPlayer + " " + game.mDate + " " + std::to_string(game.mFinalScore), textColor))
{
printf("Failed to render text texture!\n");
}
//Set rendering space and render to screen
SDL_Rect renderQuad{ selectionIndicator, selectionIndicator * i, mWindowWidth - selectionIndicator, mFont.mHeight };
//Render to screen
SDL_RenderCopy(mRenderer, mTexture, nullptr, &renderQuad);
i += 1;
}
// Draw indicator
SDL_Rect renderQuad{ 0, selectionIndicator * index, selectionIndicator, selectionIndicator };
DrawColoredRect(mRenderer, renderQuad, { 0xFF, 0xFF, 0xFF, 0xFF });
SDL_RenderPresent(mRenderer);
}
bool
Window::MovedUp(const Uint8* currentKeyStates) const
{
const Uint8 currentJoyStates{ SDL_GameControllerGetButton(mController, SDL_CONTROLLER_BUTTON_DPAD_UP) };
//printf("%d, ", currentJoyStates); // this worked!!!!!!!!!!!!!!!!!!!!!!!!
return (currentKeyStates[ SDL_SCANCODE_UP ] or currentJoyStates) and mPrevScanCode != SDL_SCANCODE_DOWN;
}
bool
Window::MovedDown(const Uint8* currentKeyStates) const
{
const Uint8 currentJoyStates{ SDL_GameControllerGetButton(mController, SDL_CONTROLLER_BUTTON_DPAD_DOWN) };
return (currentKeyStates[ SDL_SCANCODE_DOWN ] or currentJoyStates) and mPrevScanCode != SDL_SCANCODE_UP;
}
bool
Window::MovedLeft(const Uint8* currentKeyStates) const
{
const Uint8 currentJoyStates{ SDL_GameControllerGetButton(mController, SDL_CONTROLLER_BUTTON_DPAD_LEFT) };
return (currentKeyStates[ SDL_SCANCODE_LEFT ] or currentJoyStates) and mPrevScanCode != SDL_SCANCODE_RIGHT;
}
bool
Window::MovedRight(const Uint8* currentKeyStates) const
{
const Uint8 currentJoyStates{ SDL_GameControllerGetButton(mController, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) };
return (currentKeyStates[ SDL_SCANCODE_RIGHT ] or currentJoyStates) and mPrevScanCode != SDL_SCANCODE_LEFT;
}
void
Window::StringDrawHelper(std::string& s, const SDL_Rect& rect)
{
SDL_Color textColor{ 0xff, 0xFF, 0xFF, 0xFF };
if (not LoadTextureFromText(s.c_str(), textColor))
{
printf("Failed to render text texture!\n");
}
//Render to screen
SDL_RenderCopy(mRenderer, mTexture, nullptr, &rect);
}