-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
1942 lines (1455 loc) · 56.1 KB
/
game.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
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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef ____game__
#define ____game__
struct Game{
Game(){
quit = false;
level = -1;
left_right = false;
buttons_status[0] = false;
buttons_status[1] = false;
buttons_status[2] = false;
buttons_status[3] = false;
buttons_status[4] = false;
unlocks[0] = true;
for(int i = 1; i < 10; i++)
{
unlocks[i] = false;
}
menu_pointer = 0;
}
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
void level_one();
void start_menu();
void controls_menu();
void check_event();
void initiate(LTexture* textures[][4]);
void render_buttons(bool, bool, bool, bool, bool);
void fly_away_animation();
void approach_planet(int);
void enter_zone();
void enter_hole(Ship&, Black_Hole*);
void upgrades_menu(LTexture* [][4]);
void restart_game();
void clear_rocks();
void clear_holes();
//The window we'll be rendering to
SDL_Window* gWindow;
//The window renderer
SDL_Renderer* gRenderer;
//Screen dimensions
SDL_Rect gScreenRect;
SDL_Event e;
SDL_Point touchLocation;
//audio variables
//The music that will be played
Mix_Music *gMusic;
//The sound effects that will be used
TTF_Font *gFont;
//font texture
LTexture gTextTexture;
LTexture level_num_texture;
LTexture message_red;
// SHORTCODE: TEXTURES DECLARATION
LTexture space_700;
LTexture ship1_a, ship1_b, ship1_c, ship1_d;
LTexture ship2_a, ship2_b, ship2_c, ship2_d;
LTexture ship3_a, ship3_b, ship3_c, ship3_d;
LTexture ship4_a, ship4_b, ship4_c, ship4_d;
LTexture ship5_a, ship5_b, ship5_c, ship5_d;
LTexture rock_100, rock_66, rock_33, rock_11;
LTexture yellow_bullet, blue_bullet, purple_bullet, red_bullet;
LTexture blue_laser_bullet;
LTexture brown_planet, blue_planet, green_planet;
LTexture black_hole;
//stringstream to hold level number for rendering
std::stringstream level_num;
Ship ship;
std::vector<Rock*> rocks_1;
std::vector<Black_Hole*> holes;
bool quit;
int level;
int progress;
double multipos[10][2];
bool buttons_status[5];
bool unlocks[10];
bool left_right;
int menu_pointer;
SDL_Rect button;
SDL_Rect message_box;
};
bool Game::init()
{
// SHORTCODE: INITIALIZE SDL
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 )
{
SDL_Log( "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" ) )
{
SDL_Log( "Warning: Linear texture filtering not enabled!" );
}
//Get device display mode
SDL_DisplayMode displayMode;
if( SDL_GetCurrentDisplayMode( 0, &displayMode ) == 0 )
{
gScreenRect.w = displayMode.w;
gScreenRect.h = displayMode.h;
button.w = displayMode.w/4;
button.h = displayMode.h/10;
}
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, gScreenRect.w, gScreenRect.h, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
SDL_Log( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
if( gRenderer == NULL )
{
SDL_Log( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
SDL_Log( "SDL_image could not initialize! %s\n", IMG_GetError() );
success = false;
}
//Initialize SDL_mixer
if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 )
{
printf( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
if( TTF_Init() == -1 )
{
printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
}
}
}
return success;
}
bool Game::loadMedia()
{
// SHORTCODE: LOAD MEDIA
level_num_texture.setWindows(gWindow, gRenderer);
gTextTexture.setWindows(gWindow, gRenderer);
message_red.setWindows(gWindow, gRenderer);
space_700.setWindows(gWindow, gRenderer);
ship1_a.setWindows(gWindow, gRenderer);
ship1_b.setWindows(gWindow, gRenderer);
ship1_c.setWindows(gWindow, gRenderer);
ship1_d.setWindows(gWindow, gRenderer);
ship2_a.setWindows(gWindow, gRenderer);
ship2_b.setWindows(gWindow, gRenderer);
ship2_c.setWindows(gWindow, gRenderer);
ship2_d.setWindows(gWindow, gRenderer);
ship3_a.setWindows(gWindow, gRenderer);
ship3_b.setWindows(gWindow, gRenderer);
ship3_c.setWindows(gWindow, gRenderer);
ship3_d.setWindows(gWindow, gRenderer);
ship4_a.setWindows(gWindow, gRenderer);
ship4_b.setWindows(gWindow, gRenderer);
ship4_c.setWindows(gWindow, gRenderer);
ship4_d.setWindows(gWindow, gRenderer);
ship5_a.setWindows(gWindow, gRenderer);
ship5_b.setWindows(gWindow, gRenderer);
ship5_c.setWindows(gWindow, gRenderer);
ship5_d.setWindows(gWindow, gRenderer);
rock_100.setWindows(gWindow, gRenderer);
rock_66.setWindows(gWindow, gRenderer);
rock_33.setWindows(gWindow, gRenderer);
rock_11.setWindows(gWindow, gRenderer);
yellow_bullet.setWindows(gWindow, gRenderer);
red_bullet.setWindows(gWindow, gRenderer);
blue_bullet.setWindows(gWindow, gRenderer);
purple_bullet.setWindows(gWindow, gRenderer);
blue_laser_bullet.setWindows(gWindow, gRenderer);
brown_planet.setWindows(gWindow, gRenderer);
green_planet.setWindows(gWindow, gRenderer);
blue_planet.setWindows(gWindow, gRenderer);
black_hole.setWindows(gWindow, gRenderer);
//Loading success flag
bool success = true;
//Load splash textures
if( !space_700.loadFromFile( "images/space_700x700.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship1_a.loadFromFile( "images/cyan_ship_a.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship1_b.loadFromFile( "images/cyan_ship_b.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship1_c.loadFromFile( "images/cyan_ship_c.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship1_d.loadFromFile( "images/cyan_ship_d.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship2_a.loadFromFile( "images/red_ship_a.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship2_b.loadFromFile( "images/red_ship_b.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship2_c.loadFromFile( "images/red_ship_c.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship2_d.loadFromFile( "images/red_ship_d.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship3_a.loadFromFile( "images/purple_ship_a.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship3_b.loadFromFile( "images/purple_ship_b.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship3_c.loadFromFile( "images/purple_ship_c.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship3_d.loadFromFile( "images/purple_ship_d.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship4_a.loadFromFile( "images/blue_ship_a.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship4_b.loadFromFile( "images/blue_ship_b.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship4_c.loadFromFile( "images/blue_ship_c.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship4_d.loadFromFile( "images/blue_ship_d.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship5_a.loadFromFile( "images/green_ship_a.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship5_b.loadFromFile( "images/green_ship_b.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship5_c.loadFromFile( "images/green_ship_c.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !ship5_d.loadFromFile( "images/green_ship_d.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !rock_100.loadFromFile( "images/rock_100.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !rock_66.loadFromFile( "images/rock_66.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !rock_33.loadFromFile( "images/rock_33.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !rock_11.loadFromFile( "images/rock_11.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !yellow_bullet.loadFromFile( "images/bullet_yellow.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !red_bullet.loadFromFile( "images/bullet_red.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !blue_laser_bullet.loadFromFile( "images/bullet_long_blue.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !blue_bullet.loadFromFile( "images/bullet_blue.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !purple_bullet.loadFromFile( "images/bullet_purple.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !brown_planet.loadFromFile( "images/brown_planet.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !blue_planet.loadFromFile( "images/blue_planet.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !green_planet.loadFromFile( "images/green_planet.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
if( !black_hole.loadFromFile( "images/black_hole3.png" ) )
{
SDL_Log( "Failed to load touch up texture!\n" );
success = false;
}
space_700.mWidth = gScreenRect.w;
space_700.mHeight = gScreenRect.h;
ship1_a.mWidth *= 3;
ship1_a.mHeight *= 3;
ship1_b.mWidth *= 3;
ship1_b.mHeight *= 3;
ship1_c.mWidth *= 3;
ship1_c.mHeight *= 3;
ship1_d.mWidth *= 3;
ship1_d.mHeight *= 3;
ship2_a.mWidth *= 3;
ship2_a.mHeight *= 3;
ship2_b.mWidth *= 3;
ship2_b.mHeight *= 3;
ship2_c.mWidth *= 3;
ship2_c.mHeight *= 3;
ship2_d.mWidth *= 3;
ship2_d.mHeight *= 3;
ship3_a.mWidth *= 3;
ship3_a.mHeight *= 3;
ship3_b.mWidth *= 3;
ship3_b.mHeight *= 3;
ship3_c.mWidth *= 3;
ship3_c.mHeight *= 3;
ship3_d.mWidth *= 3;
ship3_d.mHeight *= 3;
ship4_a.mWidth *= 3;
ship4_a.mHeight *= 3;
ship4_b.mWidth *= 3;
ship4_b.mHeight *= 3;
ship4_c.mWidth *= 3;
ship4_c.mHeight *= 3;
ship4_d.mWidth *= 3;
ship4_d.mHeight *= 3;
ship5_a.mWidth *= 3;
ship5_a.mHeight *= 3;
ship5_b.mWidth *= 3;
ship5_b.mHeight *= 3;
ship5_c.mWidth *= 3;
ship5_c.mHeight *= 3;
ship5_d.mWidth *= 3;
ship5_d.mHeight *= 3;
rock_100.mWidth *= 3;
rock_100.mHeight *= 3;
rock_66.mWidth *= 3;
rock_66.mHeight *= 3;
rock_33.mWidth *= 3;
rock_33.mHeight *= 3;
rock_11.mWidth *= 3;
rock_11.mHeight *= 3;
yellow_bullet.mWidth *= 3;
yellow_bullet.mHeight *= 3;
red_bullet.mWidth *= 3;
red_bullet.mHeight *= 3;
purple_bullet.mWidth *= 3;
purple_bullet.mHeight *= 3;
blue_bullet.mWidth *= 3;
blue_bullet.mHeight *= 3;
blue_laser_bullet.mWidth *= 3;
blue_laser_bullet.mHeight *= 3;
black_hole.mWidth /= 2;
black_hole.mHeight /= 2;
//loading audio media
//Load music
gMusic = Mix_LoadMUS( "sounds/arcade-music-loop.wav" );
//loading 8bit font
gFont = TTF_OpenFont( "text/font.ttf", 80 );
gTextTexture.font = gFont;
message_red.font = gFont;
level_num_texture.font = gFont;
if( gFont == NULL )
{
printf( "Failed to load font! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
else
{
//Render text
SDL_Color textColor = { 255, 255, 255 };
if( !gTextTexture.loadFromRenderedText( "deep space", textColor ) )
{
printf( "Failed to render text texture!\n" );
success = false;
}
if( !message_red.loadFromRenderedText( "collected red crystal", textColor ) )
{
printf( "Failed to render text texture!\n" );
success = false;
}
if( !level_num_texture.loadFromRenderedText( "1", textColor ) )
{
printf( "Failed to render text texture!\n" );
success = false;
}
}
return success;
}
void Game::close()
{
//Destroy window
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}
void Game::level_one()
{
// SHORTCODE: LEVEL_ONE
render_buttons(true, true, true, true, true);
level_num_texture.render( ( gScreenRect.w - level_num_texture.mWidth ), 0 );
//rendering button word
SDL_Color textColor = { 255, 255, 255 };
gTextTexture.loadFromRenderedText( "upgrades", textColor );
gTextTexture.mWidth /= 3;
gTextTexture.mHeight /= 3;
gTextTexture.render( gScreenRect.w / 40, (gScreenRect.h/20 - gTextTexture.mHeight/2));
ship.handleEvent(buttons_status);
int m = ship.move(rocks_1);
if(m == -2)
{
enter_hole(ship, holes[0]);
clear_holes();
ship.clear_bullets();
return;
}
else if(m != -1)
{
rocks_1.erase(rocks_1.begin() + m);
}
ship.render();
int idx = (int)ship.bullets.size() - 1;
for(int j = (int)rocks_1.size() - 1; j >= 0; j--)
{
rocks_1[j]->move(gScreenRect.w, gScreenRect.h);
}
for(int i = idx; i >= 0 ; i--)
{
int move = ship.bullets[i]->move(rocks_1, gScreenRect.w, gScreenRect.h);
if(move == -1) ship.bullets[i]->render();
else if(move == -2)
{
delete ship.bullets[i];
ship.bullets.erase(ship.bullets.begin() + i);
}
else
{
if(rocks_1[move]->size == 100)
{
rocks_1.push_back(new Rock(rocks_1[move]->mPosX + 50, rocks_1[move]->mPosY + 50, ship.bullets[i]->degrees+45, 66) );
rocks_1.back()->set_texture(&rock_100, &rock_66, &rock_33, &rock_11);
rocks_1.push_back(new Rock(rocks_1[move]->mPosX - 50, rocks_1[move]->mPosY - 50, ship.bullets[i]->degrees, 66));
rocks_1.back()->set_texture(&rock_100, &rock_66, &rock_33, &rock_11);
rocks_1.push_back(new Rock(rocks_1[move]->mPosX + 50, rocks_1[move]->mPosY - 50, ship.bullets[i]->degrees-45, 66));
rocks_1.back()->set_texture(&rock_100, &rock_66, &rock_33, &rock_11);
}
else if(rocks_1[move]->size == 66)
{
rocks_1.push_back(new Rock(rocks_1[move]->mPosX + 33, rocks_1[move]->mPosY + 33, ship.bullets[i]->degrees-90, 33));
rocks_1.back()->set_texture(&rock_100, &rock_66, &rock_33, &rock_11);
rocks_1.push_back(new Rock(rocks_1[move]->mPosX - 30, rocks_1[move]->mPosY - 30, ship.bullets[i]->degrees+90, 33));
rocks_1.back()->set_texture(&rock_100, &rock_66, &rock_33, &rock_11);
}
else if(rocks_1[move]->size == 33)
{
rocks_1.push_back(new Rock(rocks_1[move]->mPosX + 12, rocks_1[move]->mPosY + 12, ship.bullets[i]->degrees+45, 11));
rocks_1.back()->set_texture(&rock_100, &rock_66, &rock_33, &rock_11);
rocks_1.push_back(new Rock(rocks_1[move]->mPosX + 15, rocks_1[move]->mPosY - 15, ship.bullets[i]->degrees, 11));
rocks_1.back()->set_texture(&rock_100, &rock_66, &rock_33, &rock_11);
rocks_1.push_back(new Rock(rocks_1[move]->mPosX - 17, rocks_1[move]->mPosY + 17, ship.bullets[i]->degrees-115, 11));
rocks_1.back()->set_texture(&rock_100, &rock_66, &rock_33, &rock_11);
}
delete rocks_1[move];
rocks_1.erase(rocks_1.begin() + move);
delete ship.bullets[i];
ship.bullets.erase(ship.bullets.begin() + i);
}
}
for(int i = (int)rocks_1.size() - 1; i >= 0; i--)
{
rocks_1[i]->render();
}
for(int l = 0; l < (int)holes.size(); l++)
{
holes[l]->render();
if(holes[l]->move(ship) == 1)
{
enter_hole(ship, holes[l]);
clear_holes();
ship.clear_bullets();
break;
}
}
if(rocks_1.size() == 0)
{
progress++;
level++;
for(int i = 0; i < level - 1; i++)
{
rocks_1.push_back(new Rock(gScreenRect.w/5 * i , gScreenRect.h/5 * i, 45*i*(-1*i), 100));
rocks_1[i]->set_texture(&rock_100, &rock_66, &rock_33, &rock_11);
}
level_num.str( "" );
level_num << level;
SDL_Color textColor = { 255, 255, 255 };
level_num_texture.loadFromRenderedText( level_num.str().c_str(), textColor );
}
if(buttons_status[4]) {
progress = level;
level = -3;
buttons_status[4] = false;
SDL_Delay( 150 );
}
}
void Game::start_menu()
{
// SHORTCODE: START MENU
render_buttons(false, true, false, true, false);
SDL_Color textColor = { 255, 255, 255 };
gTextTexture.loadFromRenderedText( "deep space", textColor );
gTextTexture.render( ( gScreenRect.w - gTextTexture.getWidth() ) / 2, ( gScreenRect.h - gTextTexture.getHeight() ) / 2 );
gTextTexture.loadFromRenderedText( "controls", textColor );
gTextTexture.mWidth /= 3;
gTextTexture.mHeight /= 3;
gTextTexture.render( gScreenRect.w - gScreenRect.w/8 - gTextTexture.mWidth/2, (gScreenRect.h - gScreenRect.h/20) - gTextTexture.mHeight/2);
gTextTexture.loadFromRenderedText( "play", textColor );
gTextTexture.mWidth /= 3;
gTextTexture.mHeight /= 3;
gTextTexture.render( gScreenRect.w / 8 - gTextTexture.mWidth/2, (gScreenRect.h - gScreenRect.h/20) - gTextTexture.mHeight/2);
if(buttons_status[1]) level++;
if(buttons_status[3]) level--; // switch level to CONTROLS menu
}
void Game::controls_menu()
{
render_buttons(true, true, true, true, true );
SDL_Color textColor = { 255, 255, 255 };
gTextTexture.loadFromRenderedText( "play", textColor );
gTextTexture.mWidth /= 3;
gTextTexture.mHeight /= 3;
gTextTexture.render( gScreenRect.w / 8 - gTextTexture.mWidth/2, (gScreenRect.h/20 - gTextTexture.mHeight/2));
if(buttons_status[0])
{
std::stringstream ss;
ss << "charge shot";
gTextTexture.loadFromRenderedText( ss.str().c_str(), textColor );
gTextTexture.render( ( gScreenRect.w - gTextTexture.getWidth() ) / 2, ( gScreenRect.h - gTextTexture.getHeight() ) / 2 );
}
else if(buttons_status[1] && buttons_status[3])
{
std::stringstream ss;
ss << "ignition";
gTextTexture.loadFromRenderedText( ss.str().c_str(), textColor );
gTextTexture.render( ( gScreenRect.w - gTextTexture.getWidth() ) / 2, ( gScreenRect.h - gTextTexture.getHeight() ) / 2 );
}
else if(buttons_status[1])
{
std::stringstream ss;
ss << "rotate left";
gTextTexture.loadFromRenderedText( ss.str().c_str(), textColor );
gTextTexture.render( ( gScreenRect.w - gTextTexture.getWidth() ) / 2, ( gScreenRect.h - gTextTexture.getHeight() ) / 2 );
int h_ = gTextTexture.getHeight();
ss.str("");
ss << "left + right = ignition";
gTextTexture.loadFromRenderedText( ss.str().c_str(), textColor );
gTextTexture.mWidth /= 3;
gTextTexture.mHeight /= 3;
gTextTexture.render( ( gScreenRect.w - gTextTexture.getWidth() ) / 2, (( gScreenRect.h - gTextTexture.getHeight() ) / 2) + h_ );
}
else if(buttons_status[2])
{
std::stringstream ss;
ss << "fire";
gTextTexture.loadFromRenderedText( ss.str().c_str(), textColor );
gTextTexture.render( ( gScreenRect.w - gTextTexture.getWidth() ) / 2, ( gScreenRect.h - gTextTexture.getHeight() ) / 2 );
}
else if(buttons_status[3])
{
std::stringstream ss;
ss << "rotate right";
gTextTexture.loadFromRenderedText( ss.str().c_str(), textColor );
gTextTexture.render( ( gScreenRect.w - gTextTexture.getWidth() ) / 2, ( gScreenRect.h - gTextTexture.getHeight() ) / 2 );
int h_ = gTextTexture.getHeight();
ss.str("");
ss << "left + right = ignition";
gTextTexture.loadFromRenderedText( ss.str().c_str(), textColor );
gTextTexture.mWidth /= 3;
gTextTexture.mHeight /= 3;
gTextTexture.render( ( gScreenRect.w - gTextTexture.getWidth() ) / 2, (( gScreenRect.h - gTextTexture.getHeight() ) / 2) + h_ );
}
if(buttons_status[4])
{
// play
level = 0;
buttons_status[4] = false;
SDL_Delay( 500 );
}
}
void Game::check_event()
{
// SHORTCODE: TOUCH SCREEN
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
//Window event
else if( e.type == SDL_WINDOWEVENT )
{
//Window resize/orientation change
if( e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED )
{
//Get screen dimensions
gScreenRect.w = e.window.data1;
gScreenRect.h = e.window.data2;
//Update screen
SDL_RenderPresent( gRenderer );
}
}
//Touch down
else if( e.type == SDL_MULTIGESTURE )
{
multipos[0][0] = -10;
multipos[0][1] = -10;
multipos[1][0] = -10;
multipos[1][1] = -10;
SDL_TouchID device=SDL_GetTouchDevice(0);
for(int i=0;i<SDL_GetNumTouchFingers(device);++i)
{
SDL_Finger *finger = SDL_GetTouchFinger(device,i);
if(finger!=NULL)
{
multipos[i][0]=finger->x * gScreenRect.w;
multipos[i][1]=finger->y * gScreenRect.h;
}
}
if(multipos[0][0] > multipos[1][0] && multipos[1][0] != -10) // set multipos[0] to be left finger
{
double leftx, lefty, rightx, righty;
rightx = multipos[0][0];
righty = multipos[0][1];
leftx = multipos[1][0];
lefty = multipos[1][1];
multipos[0][0] = leftx;
multipos[0][1] = lefty;
multipos[1][0] = rightx;
multipos[1][1] = righty;
}
//left FINGER 1
if(multipos[0][0] >= 0 && multipos[0][0] <= gScreenRect.w / 4)
{
if(multipos[0][1] >= gScreenRect.h - (gScreenRect.h/10)*2.5
&& multipos[0][1] <= gScreenRect.h - (gScreenRect.h/10)*2.5 + (gScreenRect.h/10))
{
buttons_status[0] = true;
}
else if(multipos[0][1] >= gScreenRect.h - (gScreenRect.h/10)
&& multipos[0][1] <= gScreenRect.h)
{
buttons_status[1] = true;
}
else if(multipos[0][1] >= 0 && multipos[0][1] <= gScreenRect.h / 10)
{
buttons_status[4] = true;
}
else
{
//turn left buttons off
buttons_status[0] = false;
buttons_status[1] = false;
buttons_status[4] = false;
}
}
else
{
//turn left buttons off
buttons_status[0] = false;
buttons_status[1] = false;
buttons_status[4] = false;
}
//right finger
if(multipos[1][0] != -10){
if(multipos[1][0] >= gScreenRect.w - gScreenRect.w / 4 && multipos[1][0] <= gScreenRect.w)
{
if(multipos[1][1] >= gScreenRect.h - (gScreenRect.h/10)*2.5
&& multipos[1][1] <= gScreenRect.h - (gScreenRect.h/10)*2.5 + (gScreenRect.h/10))
{
buttons_status[2] = true;
}
else if(multipos[1][1] >= gScreenRect.h - (gScreenRect.h/10)
&& multipos[1][1] <= gScreenRect.h)
{
buttons_status[3] = true;
}
else
{
buttons_status[2] = false;
buttons_status[3] = false;
}
}
else
{
buttons_status[2] = false;
buttons_status[3] = false;
}
}
}
else if( e.type == SDL_FINGERDOWN )
{
touchLocation.x = e.tfinger.x * gScreenRect.w;
touchLocation.y = e.tfinger.y * gScreenRect.h;
if(touchLocation.x >= 0 && touchLocation.x <= gScreenRect.w / 4)
{
if(touchLocation.y >= gScreenRect.h - (gScreenRect.h/10)*2.5
&& touchLocation.y <= gScreenRect.h - (gScreenRect.h/10)*2.5 + (gScreenRect.h/10))
{
buttons_status[0] = true;
}