-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
1473 lines (1273 loc) · 38.2 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
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
//The game ONslaught
//(c) Liam Girard 2014
//
//This program allows the user to play the game ONslaught
#include <SDL.h>
//#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <windows.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
class Sprite
{
public:
//Initializes variables
Sprite();
//Deallocates memory
~Sprite();
//Loads image at specified path
bool loadFromFile( std::string path );
//Function to load files from specified path
void LoadBitmap(Sprite &s, const std::string& name);
//Deallocates sprite
void free();
//Renders texture at given point
void render( int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE );
//Gets image dimensions
int getWidth();
int getHeight();
private:
//The actual hardware texture
SDL_Texture* mSprite;
//Image dimensions
int mWidth;
int mHeight;
};
class Score
{
public:
static const int WIDTH = 40;
static const int HEIGHT = 53;
//Initializes the variables
Score();
//Shows the dot on the screen
void render(int number);
void setxy(int inputx, int inputy);
private:
Sprite numbers[10];
int x, y;
};
Score::Score()
{
numbers[0].LoadBitmap(numbers[0],"0.bmp");
numbers[1].LoadBitmap(numbers[1],"1.bmp");
numbers[2].LoadBitmap(numbers[2],"2.bmp");
numbers[3].LoadBitmap(numbers[3],"3.bmp");
numbers[4].LoadBitmap(numbers[4],"4.bmp");
numbers[5].LoadBitmap(numbers[5],"5.bmp");
numbers[6].LoadBitmap(numbers[6],"6.bmp");
numbers[7].LoadBitmap(numbers[7],"7.bmp");
numbers[8].LoadBitmap(numbers[8],"8.bmp");
numbers[9].LoadBitmap(numbers[9],"9.bmp");
}
void Score::setxy(int inputx,int inputy)
{
x = inputx;
y = inputy;
}
void Score::render(int number)
{
numbers[number].render(x,y);
}
class ScoreBoard
{
public:
static const int WIDTH = 160;
static const int HEIGHT = 480;
//Initializes the variables
ScoreBoard();
//Shows the dot on the screen
void render();
private:
//Sprite for the ScoreBoard
Sprite sprite;
//The X and Y offsets of the dot
int x, y;
};
ScoreBoard::ScoreBoard()
{
sprite.LoadBitmap(sprite,"Scoreboard.bmp");
x=480;
y=0;
}
void ScoreBoard::render()
{
sprite.render(x,y);
}
class PowerUp
{
public:
static const int WIDTH = 32;
static const int HEIGHT = 32;
//Initializes the variables
PowerUp();
int i;
bool exist = false;
//Shows the dot on the screen
void render();
void setxy(int inputx, int inputy);
private:
//Sprite for the PowerUp
Sprite shield;
Sprite speed;
Sprite life;
Sprite pulse;
int x, y;
};
PowerUp::PowerUp()
{
shield.LoadBitmap(shield,"PowerUpShield.bmp");
speed.LoadBitmap(speed,"PowerUpSpeed.bmp");
life.LoadBitmap(life,"PowerUpLife.bmp");
pulse.LoadBitmap(pulse,"PowerUpPulse.bmp");
}
void PowerUp::setxy(int inputx, int inputy)
{
x = inputx;
y = inputy;
}
void PowerUp::render()
{
switch(i)
{
case 1:
shield.render(x,y);
break;
case 2:
speed.render(x,y);
break;
case 3:
life.render(x,y);
break;
case 4:
pulse.render(x,y);
break;
}
}
class Menus
{
public:
static const int WIDTH = 160;
static const int HEIGHT = 480;
//Initializes the variables
Menus();
//Shows the dot on the screen
void render(int menu);
private:
//Sprite for the Menus
Sprite gameOver;
Sprite victory;
Sprite menuStart;
Sprite menuQuit;
};
Menus::Menus()
{
gameOver.LoadBitmap(gameOver,"gameOver.bmp");
victory.LoadBitmap(victory,"win.bmp");
menuStart.LoadBitmap(menuStart,"menuStart.bmp");
menuQuit.LoadBitmap(menuQuit,"menuQuit.bmp");
}
void Menus::render(int menu)
{
if (menu == 0)
{
gameOver.render(0,0);
}
else if (menu == 1)
{
victory.render(0,0);
}
else if (menu == 2)
{
menuStart.render(0,0);
}
else if (menu == 3)
{
menuQuit.render(0,0);
}
}
class Life
{
public:
static const int WIDTH = 40;
static const int HEIGHT = 45;
//Initializes the variables
Life();
//Shows the dot on the screen
void render();
void setxy(int inputx, int inputy);
private:
//Sprite for the Life
Sprite sprite;
//The X and Y offsets of the dot
int x, y;
};
Life::Life()
{
sprite.LoadBitmap(sprite,"heart.bmp");
}
void Life::render()
{
sprite.render(x,y);
}
void Life::setxy(int inputx,int inputy)
{
x = inputx;
y = inputy;
}
class Enemy
{
public:
static const int WIDTH = 22;
static const int HEIGHT = 20;
//Initializes the variables
Enemy();
//Moves the dot
void move();
//Shows the dot on the screen
void render();
bool alive = true;
void setxy(int inputx, int inputy);
int getX();
int getY();
private:
//Sprite for the enemy
Sprite sprite;
//The X and Y offsets of the dot
int x, y;
};
Enemy::Enemy()
{
sprite.LoadBitmap(sprite,"enemy.bmp");
}
void Enemy:: setxy(int inputx, int inputy)
{
x = inputx;
y = inputy;
}
void Enemy::render()
{
sprite.render(x,y);
}
void Enemy::move()
{
y = y + 1;
}
int Enemy::getX()
{
return x;
}
int Enemy::getY()
{
return y;
}
class bullet
{
public:
int WIDTH = 4;
int HEIGHT = 17;
//Initializes the variables
bullet();
//Moves the dot
void move();
//Checks input
void check();
//Shows the dot on the screen
void render();
bool inFlight = false;
bool enemy = false;
bool pulse = false;
bool sideGun = false;
void setxy(int inputx, int inputy);
int getX();
int getY();
private:
//Sprite for the bullet
Sprite normal;
//Sprite for the bullet
Sprite enemyBullet;
//Sprite for the bullet
Sprite pulseGun;
//The X and Y offsets of the dot
int x, y;
};
bullet::bullet()
{
normal.LoadBitmap(normal,"bullet.bmp");
enemyBullet.LoadBitmap(enemyBullet,"EnemyBullet.bmp");
pulseGun.LoadBitmap(pulseGun,"pulseBullet.bmp");
}
void bullet::render()
{
if (pulse)
{
pulseGun.render(x,y);
}
else if (!enemy)
{
normal.render(x,y);
}
else if (enemy)
{
enemyBullet.render(x,y);
}
}
void bullet:: setxy(int inputx, int inputy)
{
x = inputx;
y = inputy;
}
void bullet::move()
{
if (pulse)
{
y = y - 8;
}
else if (!enemy)
{
y = y - 3;
}
else if (enemy)
{
y = y + 6;
}
}
int bullet::getX()
{
return x;
}
int bullet::getY()
{
return y;
}
class Player
{
public:
static const int WIDTH = 32;
static const int HEIGHT = 32;
//Initializes the variables
Player();
//Takes key presses and adjusts the dot's velocity
void handleEvent( SDL_Event& e );
//Moves the dot
void move();
//Checks input
void check();
void setxy(int inputx, int inputy);
//Get X
int getX();
//Get Y
int getY();
//Check if shield is active
bool shieldOn = false;
//Check if speed is active
bool speed = false;
//Check if they can fire a bullet
bool Bullet = false;
//Shows the dot on the screen
void render();
private:
//Sprite for the Player
Sprite normal;
//Sprite for the Player
Sprite noBullet;
//Sprite for the Player
Sprite normalS;
//Sprite for the Player
Sprite noBulletS;
//The X and Y offsets of the dot
int x, y;
//The velocity of the dot
int vx, vy;
};
Player::Player()
{
//Initialize the offsets
x = SCREEN_WIDTH/4;
y = SCREEN_HEIGHT - 35;
//Initialize the velocity
vx = 0;
vy = 0;
//Load dot texture
normal.LoadBitmap(normal,"player.bmp");
//Load dot texture
noBullet.LoadBitmap(noBullet,"playerNoBullet.bmp");
//Load dot texture
normalS.LoadBitmap(normalS,"playerShield.bmp");
//Load dot texture
noBulletS.LoadBitmap(noBulletS,"playerShieldNoBullet.bmp");
}
void Player::check()
{
const Uint8 *state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_UP] && state[SDL_SCANCODE_RIGHT])
{
vy = -5;
vx = 5;
}
else if (state[SDL_SCANCODE_UP] && state[SDL_SCANCODE_LEFT])
{
vy = -5;
vx = -5;
}
else if (state[SDL_SCANCODE_DOWN] && state[SDL_SCANCODE_RIGHT])
{
vy = 5;
vx = 5;
}
else if (state[SDL_SCANCODE_DOWN] && state[SDL_SCANCODE_LEFT])
{
vy = 5;
vx = -5;
}
else if (state[SDL_SCANCODE_UP])
{
vy = -5;
vx = 0;
}
else if (state[SDL_SCANCODE_DOWN])
{
vy = 5;
vx = 0;
}
else if (state[SDL_SCANCODE_RIGHT])
{
vx = 5;
vy = 0;
}
else if (state[SDL_SCANCODE_LEFT])
{
vx = -5;
vy = 0;
}
else
{
vy = 0;
vx = 0;
}
if (speed)
{
if (vx == -5)
{
vx = vx - 2;
}
else if (vx == 5)
{
vx = vx + 2;
}
if (vy == -5)
{
vy = vy - 2;
}
else if (vy == 5)
{
vy = vy + 2;
}
}
}
void Player::handleEvent( SDL_Event& e )
{
/*
//If a key was pressed
if( e.type == SDL_KEYDOWN / *&& e.key.repeat == 0* / )
{
//Adjust the velocity
switch( e.key.keysym.sym )
{
case SDLK_UP: vy = -10; vx = 0; break;
case SDLK_DOWN: vy = 10; vx = 0; break;
case SDLK_LEFT: vx = -10; vy = 0; break;
case SDLK_RIGHT: vx = 10; vy = 0; break;
case SDLK_LEFT and SDLK_UP: vx = -10; vy = 10; break;
case SDLK_LEFT and SDLK_DOWN: vx = -10; vy = -10; break;
case SDLK_RIGHT and SDLK_UP: vx = 10; vy = 10; break;
case SDLK_RIGHT and SDLK_DOWN: vx = 10; vy = -10; break;
}
}*/
}
void Player::move()
{
//Move the dot left or right
x += vx;
//If the dot went too far to the left or right
if ( x < 0 )
{
vx = 0;
x = 0;
}
else if( x + normal.getWidth() > SCREEN_WIDTH - SCREEN_WIDTH/4 )
{
vx = 0;
x = SCREEN_WIDTH - SCREEN_WIDTH/4 - normal.getWidth();
}
//Move the dot up or down
y += vy;
//If the dot went too far up or down
if ( y < 0 )
{
vy = 0;
y = 0;
}
else if( y + normal.getHeight() > SCREEN_HEIGHT )
{
vy = 0;
y = SCREEN_HEIGHT - normal.getHeight();
}
}
void Player::render()
{
if (shieldOn)
{
if (Bullet)
{
noBulletS.render(x,y);
}
else
{
normalS.render(x,y);
}
}
else
{
if (Bullet)
{
noBullet.render(x,y);
}
else
{
normal.render(x,y);
}
}
}
void Player::setxy(int inputx, int inputy)
{
x = inputx;
y = inputy;
}
int Player::getX()
{
return x;
}
int Player::getY()
{
return y;
}
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The window renderer
SDL_Renderer* gRenderer = NULL;
Sprite::Sprite()
{
//Initialize
mSprite = NULL;
mWidth = 0;
mHeight = 0;
}
Sprite::~Sprite()
{
//Deallocate
free();
}
bool Sprite::loadFromFile( std::string path )
{
//Get rid of preexisting texture
free();
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = SDL_LoadBMP( path.c_str() );
if( loadedSurface == NULL )
{
printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), SDL_GetError() );
}
else
{
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
if( newTexture == NULL )
{
printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
}
else
{
//Get image dimensions
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
}
//Return success
mSprite = newTexture;
return mSprite != NULL;
}
void Sprite::LoadBitmap(Sprite &s, const std::string& name)
{
if( !s.loadFromFile( name ) )
{
printf( "Failed to load dot texture!\n" );
}
}
void Sprite::free()
{
//Free texture if it exists
if( mSprite != NULL )
{
SDL_DestroyTexture( mSprite );
mSprite = NULL;
mWidth = 0;
mHeight = 0;
}
}
void Sprite::render( int x, int y, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip )
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
//Set clip rendering dimensions
if( clip != NULL )
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
//Render to screen
SDL_RenderCopyEx( gRenderer, mSprite, clip, &renderQuad, angle, center, flip );
}
int Sprite::getWidth()
{
return mWidth;
}
int Sprite::getHeight()
{
return mHeight;
}
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "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( "Warning: Linear texture filtering not enabled!" );
}
//Create window
gWindow = SDL_CreateWindow( "ONslaught", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create vsynced renderer for window
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
if( gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
}
}
}
return success;
}
void close()
{
//Free loaded images
//Destroy window
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main( int argc, char* args[] )
{
int totalBullets = 0;
bool once = true;
int score[3];
int level[2];
int totalEnemies = 5;
int coordinate, column;
int layer = 0;
int counter = 1;
int lives = 3;
int menu = 2;
bool openMenu = true;
bool restarted = false;
int powerX, powerY;
bool shield = false;
int quitPowerUp = 0;
level [0]=1;
level [1]=0;
//For random bullet timing
int random;
srand(time(0));
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//The Player that will be moving around on the screen
Player user;
bullet MyBullet [20];
Enemy enemies [95];
bullet EnemyBullet [95];
Score points[5];
ScoreBoard board;
Life hearts[3];
Menus endGame;
PowerUp powers;
while (openMenu)
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
if( e.type == SDL_KEYDOWN )
{
switch( e.key.keysym.sym )
{
case SDLK_DOWN:
if (menu == 3)
{
menu = 2;
}
else
{
menu = 3;
}
break;
case SDLK_UP:
if (menu == 3)
{
menu = 2;
}
else
{
menu = 3;
}
break;
case SDLK_SPACE:
if (menu == 3)
{
quit = true;
}
openMenu = false;
break;
}
}
}
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
endGame.render(menu);
//Update screen
SDL_RenderPresent( gRenderer );
}
openMenu = true;
for (int x = 0; x < 3; x++)
{
score[x]=0;
}
//Spawn enemies
for (int f = 0; f < totalEnemies; f++)
{
random = rand()%SCREEN_HEIGHT;
column = column+1;
coordinate = column*32;
if (coordinate>=436)
{
layer = SCREEN_HEIGHT * counter;
counter++;
column = 0;
coordinate = column*32;
}
enemies[f].setxy(coordinate,(-3*random)-layer);
}
counter = 0;
column = 0;
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
//Handle input for the Player
//user.handleEvent( e );
if( e.type == SDL_KEYDOWN )
{
switch( e.key.keysym.sym )
{
case SDLK_SPACE:
if (totalBullets < 7)
{
MyBullet[totalBullets].inFlight = true;
MyBullet[totalBullets].setxy(user.getX()+14,user.getY());
totalBullets++;
}
break;
}
}
}
if (user.speed)
{
quitPowerUp++;
if (quitPowerUp == 500)