forked from ManalHasan/OOP-Project-Manal-Moiz-Naaseh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameLoop.cpp
574 lines (530 loc) · 18.7 KB
/
GameLoop.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
#include "GameLoop.hpp"
//constructor --Player class pointers assigned to base class birds
GameLoop::GameLoop() : window(nullptr), renderer(nullptr), GameState(false), PauseState(false), startState(false), p(new BirdB()), y(new BirdY()), o(new BirdO())
{ //want birds to start from middle of y axis of screen and a fixed x axis
p->setSource(0, 0, 65, 55);
p->setDest(250, Height / 2, 65, 55);
y->setSource(0, 0, 65, 55);
y->setDest(250, Height / 2, 65, 55);
o->setSource(0, 0, 65, 55);
o->setDest(250, Height / 2, 65, 55);
//the base ground
ground1.setSource(0, 0, 336, 112);
ground1.setDest(0, 520, 336, 112);
ground2.setSource(0, 0, 336, 112);
ground2.setDest(0, 520, 336, 112);
//obstacles that will be appearing from above and below
Pipe_Above1.setSource(0, 0, 52, 320);
Pipe_Above1.setDest(0, -200, 52, 320);
Pipe_Below1.setSource(0, 0, 52, 320);
Pipe_Below1.setDest(0, 350, 52, 320);
Pipe_Above2.setSource(0, 0, 52, 320);
Pipe_Above2.setDest(0, -200, 52, 320);
Pipe_Below2.setSource(0, 0, 52, 290);
Pipe_Below2.setDest(0, 350, 52, 290);
Pipe_Above3.setSource(0, 0, 52, 320);
Pipe_Above3.setDest(0, -200, 52, 320);
Pipe_Below3.setSource(0, 0, 52, 320);
Pipe_Below3.setDest(0, 350, 52, 320);
}
//mutator of startstate to change it in between our main gameloop
void GameLoop::setStartState(const bool x)
{
startState = x;
}
//following are accessors of gamestate, pausestate, and start state
bool GameLoop::getGameState() const
{
return GameState;
}
bool GameLoop::getPauseState() const
{
return PauseState;
}
bool GameLoop::getStartState() const
{
return startState;
}
//Initializing sdl and making window, also creating textures of the objects(only once in the beginning)
void GameLoop::Initialize()
{
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("Flappy Bird", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width, Height, SDL_WINDOW_RESIZABLE);
if (window)
{
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer)
{
// if Main menu start game clicked only then do the below so for that getsamestate func in main menu class
cout << "Succeeded!" << endl;
GameState = true;
p->CreateTexture("image/Frame-1 (1).png", renderer);
p->createTexture1("image/Frame-2 (1).png", renderer);
p->createTexture2("image/Frame-3 (1).png", renderer);
y->CreateTexture("image/frameY-1.png", renderer);
y->createTexture1("image/frameY-2.png", renderer);
o->CreateTexture("image/frameO-1.png", renderer);
o->createTexture1("image/frameO-2.png", renderer);
o->createTexture2("image/frameO-3.png", renderer);
o->createTexture3("image/frameO-4.png", renderer);
b.CreateTexture("image/bg1.png", renderer);
ground1.CreateTexture("image/Tile (1).png", renderer);
ground2.CreateTexture("image/Tile (1).png", renderer);
Pipe_Above1.CreateTexture("Image/spike 1.png", renderer);
Pipe_Below1.CreateTexture("Image/brick_3.png", renderer);
Pipe_Above2.CreateTexture("Image/brick_3 (1).png", renderer);
Pipe_Below2.CreateTexture("Image/spike 2 (1).png", renderer);
Pipe_Above3.CreateTexture("Image/brick_3 (1).png", renderer);
Pipe_Below3.CreateTexture("Image/spike B.png", renderer);
}
else
{
cout << "Renderer not created" << endl;
}
}
else
{
cout << "Window not created" << endl;
}
}
// background music
void GameLoop::bgMusic()
{ //referring to methods of soundManager
sound.initializeSDL();
sound.loadMedia();
}
// Main menu
void GameLoop::MainMenu()
{ //referring to methods of Mainmenu class
menu.Initialize(renderer);
while (!menu.getClicked())//if start game is not clicked keep on rendering until user quits
{
if (menu.EventHandling(event1) == -1)
{
GameState = false;
break;
}
SDL_RenderClear(renderer);
menu.Render(renderer);
SDL_RenderPresent(renderer);
}
}
// Instructions
void GameLoop::Instructions()
{ //referring to methods of Instructions class
ins.Initialize(renderer);
while (!ins.getNext())//if arrow is not clicked keep on rendering until user quits
{
if (ins.EventHandling(event1) == -1)
{
GameState = false;
break;
}
SDL_RenderClear(renderer);
ins.Render(renderer);
SDL_RenderPresent(renderer);
}
}
// selct your bird page
void GameLoop::SelectBird()
{ //referring to methods of SelectBird class
sb.Initialize(renderer);
while (!sb.getChosenB())//if any bird is not chosen keep on rendering until user quits
{
if (sb.EventHandling(event1) == -1)
{
GameState = false;
break;
}
SDL_RenderClear(renderer);
sb.Render(renderer);
SDL_RenderPresent(renderer);
}
}
// pause functionality
void GameLoop::Paused()
{ //referring to methods of PauseScreen class
PScreen.Initialize(renderer);
SDL_RenderClear(renderer);
PScreen.Render(renderer);
SDL_RenderPresent(renderer);
}
// End game with score display
void GameLoop::Endgame()
{
ew.Initialize(renderer);
//displaying score at the end screen
//Initialize TTF and font
TTF_Init();
//path will be changed accordingly
TTF_Font *font = TTF_OpenFont("C:\\Users\\A450L\\Downloads\\OOP-Project-Manal-Moiz-Naaseh-main\\OOP-Project-Manal-Moiz-Naaseh-main\\font3.ttf", 25);
SDL_Color text_color = {0, 0, 0};
SDL_Texture *scoreTexture = nullptr;
SDL_Surface *scoreSurface = nullptr;
SDL_Texture *highscoreTexture = nullptr;
SDL_Surface *highscoreSurface = nullptr;
while (!ew.getRestart())
{
if (ew.EventHandling(event1) == -1)
{
GameState = false;
// sound.closeSDL();
break;
}
//rendering code for end window
SDL_RenderClear(renderer);
ew.Render(renderer);
// Render and update the score
score1 = score.getScore() / 50;
score1 = s / 50;
//score.setHighscore(score1); ////
score=score1;
int score2 = score.getHighschore();
char scoreText[50];
char highscoreText[50];
sprintf(scoreText, " %d", score1);
sprintf(highscoreText, " %d", score2);
if (scoreTexture)
{
SDL_DestroyTexture(scoreTexture);
scoreTexture = nullptr;
}
if (highscoreTexture)
{
SDL_DestroyTexture(highscoreTexture);
highscoreTexture = nullptr;
}
scoreSurface = TTF_RenderText_Solid(font, scoreText, text_color);
scoreTexture = SDL_CreateTextureFromSurface(renderer, scoreSurface);
highscoreSurface = TTF_RenderText_Solid(font, highscoreText, text_color);
highscoreTexture = SDL_CreateTextureFromSurface(renderer, highscoreSurface);
int texW = 0, texH = 0;
SDL_QueryTexture(scoreTexture, NULL, NULL, &texW, &texH);
SDL_Rect scoreRect = {400, 280, texW, texH};
SDL_RenderCopy(renderer, scoreTexture, NULL, &scoreRect);
SDL_QueryTexture(highscoreTexture, NULL, NULL, &texW, &texH);
SDL_Rect highscoreRect = {400, 350, texW, texH};
SDL_RenderCopy(renderer, highscoreTexture, NULL, &highscoreRect);
SDL_FreeSurface(scoreSurface);
SDL_FreeSurface(highscoreSurface);
SDL_RenderPresent(renderer);
}
// // if restart clicked
if (ew.getRestart())
{
GameState = true;
startState = true;
s=0;
ew.setRestart(false);
Reset();//reset every thing to initial condition-Reset() defined below
}
if (scoreTexture)
{
SDL_DestroyTexture(scoreTexture);
}
if (highscoreTexture)
{
SDL_DestroyTexture(highscoreTexture);
}
//closing sdl font display
TTF_CloseFont(font);
TTF_Quit();
}
//will be used before user starts the game
void GameLoop::SetHighscore()
{
//score.setHighscore(0);
score=0;
}
// keyboard connectivity
void GameLoop::Event()
{
SDL_PollEvent(&event1);
if (event1.type == SDL_QUIT)
{
GameState = false;
}
if (event1.type == SDL_KEYDOWN)
{
if (event1.key.keysym.sym == SDLK_UP) // when up pressed
{
cout << "Pressed" << endl;
if (sb.getBird() == 1)
{
p->Gravity();
}
else if (sb.getBird() == 2)
{
y->Gravity();
}
else if (sb.getBird() == 3)
{
o->Gravity();
}
}
else if (event1.key.keysym.sym == SDLK_SPACE) // when space bar pressed
{
PauseState = !PauseState;
}
}
}
// updating x/y axis values of objects
void GameLoop::Update()
{
timer++;
bool flag1 = false, flag2 = false;
ground1.GroundUpdate1();
ground2.GroundUpdate2();
//the pipes that exit the screen are being updated to reappear from right side of screen but with different orientation(height)
if (timer > 100)
{
flag1 = Pipe_Above1.Pipe_Above1Update(variance1, s);
flag2 = Pipe_Below1.Pipe_Below1Update(variance1, s);
if (flag1 && flag2)
{
srand(SDL_GetTicks());
variance1 = rand() % 201 - 100;
Pipe_Above1.Pipe_Above1Update(variance1, s);
Pipe_Below1.Pipe_Below1Update(variance1, s);
}
flag1 = Pipe_Above2.Pipe_Above2Update(variance2, s);
flag2 = Pipe_Below2.Pipe_Below2Update(variance2, s);
if (flag1 && flag2)
{
srand(SDL_GetTicks());
variance2 = rand() % 201 - 100;
Pipe_Above2.Pipe_Above2Update(variance2, s);
Pipe_Below2.Pipe_Below2Update(variance2, s);
}
flag1 = Pipe_Above3.Pipe_Above3Update(variance3, s);
flag1 = Pipe_Below3.Pipe_Below3Update(variance3, s);
if (flag1 && flag2)
{
srand(SDL_GetTicks());
variance3 = rand() % 201 - 100;
Pipe_Above3.Pipe_Above3Update(variance3, s);
Pipe_Below3.Pipe_Below3Update(variance3, s);
}
}
//updating the movement of birds to give them the flapping effect and apply gravity to them
if (sb.getBird() == 1)
{
p->Update();
}
else if (sb.getBird() == 2)
{
y->Update();
}
else if (sb.getBird() == 3)
{
o->Update();
}
//operator overloading
s+=score++;
std::cout<<s<<std::endl;
//to check for collision throughout the runtime of game
CollisionDetection();
}
// Collison check either with the obsracles or the ground
void GameLoop::CollisionDetection()
{
if (sb.getBird() == 1)
{ //checks for collison of blue bird bird with obstacles
if (CollisionManager::checkCollision(&p->getDest(), &Pipe_Above1.getDest()) || CollisionManager::checkCollision(&p->getDest(), &Pipe_Below1.getDest()) ||
CollisionManager::checkCollision(&p->getDest(), &Pipe_Above2.getDest()) || CollisionManager::checkCollision(&p->getDest(), &Pipe_Below2.getDest()) ||
CollisionManager::checkCollision(&p->getDest(), &Pipe_Above3.getDest()) || CollisionManager::checkCollision(&p->getDest(), &Pipe_Below3.getDest()))
{
sound.loadMedia1(); // collision sound
Reset();//reset every value to initial state
GameState = false;//through this the game will exit the main loop
}
//checks for collison of blue bird bird with ground
if (CollisionManager::checkCollision(&p->getDest(), &ground1.getDest()))
{
sound.loadMedia1();
Reset();
GameState = false;
}
if (CollisionManager::checkCollision(&p->getDest(), &ground2.getDest()))
{
sound.loadMedia1();
Reset();
GameState = false;
}
}
//for yellow bird
else if (sb.getBird() == 2)
{
if (CollisionManager::checkCollision(&y->getDest(), &Pipe_Above1.getDest()) || CollisionManager::checkCollision(&y->getDest(), &Pipe_Below1.getDest()) ||
CollisionManager::checkCollision(&y->getDest(), &Pipe_Above2.getDest()) || CollisionManager::checkCollision(&y->getDest(), &Pipe_Below2.getDest()) ||
CollisionManager::checkCollision(&y->getDest(), &Pipe_Above3.getDest()) || CollisionManager::checkCollision(&y->getDest(), &Pipe_Below3.getDest()))
{
sound.loadMedia1();
Reset();
GameState = false;
}
if (CollisionManager::checkCollision(&y->getDest(), &ground1.getDest()))
{
sound.loadMedia1();
Reset();
GameState = false;
}
if (CollisionManager::checkCollision(&y->getDest(), &ground2.getDest()))
{
sound.loadMedia1();
Reset();
GameState = false;
}
}
//for the owl
else if (sb.getBird() == 3)
{
if (CollisionManager::checkCollision(&o->getDest(), &Pipe_Above1.getDest()) || CollisionManager::checkCollision(&o->getDest(), &Pipe_Below1.getDest()) ||
CollisionManager::checkCollision(&o->getDest(), &Pipe_Above2.getDest()) || CollisionManager::checkCollision(&o->getDest(), &Pipe_Below2.getDest()) ||
CollisionManager::checkCollision(&o->getDest(), &Pipe_Above3.getDest()) || CollisionManager::checkCollision(&o->getDest(), &Pipe_Below3.getDest()))
{
sound.loadMedia1();
Reset();
GameState = false;
}
if (CollisionManager::checkCollision(&o->getDest(), &ground1.getDest()))
{
sound.loadMedia1();
Reset();
GameState = false;
}
if (CollisionManager::checkCollision(&o->getDest(), &ground2.getDest()))
{
sound.loadMedia1();
Reset();
GameState = false;
}
}
}
// reseting all the values, when game restarted
void GameLoop::Reset()
{
variance1 = rand() % 201 - 100;
variance2 = rand() % 201 - 100;
variance3 = rand() % 201 - 100;
p->Reset();
o->Reset();
y->Reset();
Pipe_Above1.Reset();
Pipe_Above2.Reset();
Pipe_Above3.Reset();
Pipe_Below1.Reset();
Pipe_Below2.Reset();
Pipe_Below3.Reset();
}
void GameLoop::Render() {
t1++;
//clear any rendering before this
SDL_RenderClear(renderer);
//background render
b.Render(renderer);
if (t1>100){
//rendering of objects
Pipe_Above1.Render(renderer);
Pipe_Below1.Render(renderer);
Pipe_Above2.Render(renderer);
Pipe_Below2.Render(renderer);
Pipe_Above3.Render(renderer);
Pipe_Below3.Render(renderer);
ground1.GroundRender(renderer);
ground2.GroundRender(renderer);
}
if (sb.getBird()==1){
p->Render(renderer);
}
else if (sb.getBird()==2){
y->Render(renderer);
}
else if (sb.getBird()==3){
o->Render(renderer);
}
//displaying the current score during game
TTF_Init();
TTF_Font *font = TTF_OpenFont("C:\\Users\\A450L\\Downloads\\OOP-Project-Manal-Moiz-Naaseh-main\\OOP-Project-Manal-Moiz-Naaseh-main\\font3.ttf", 55);
SDL_Color text_color = {0, 0, 0};
SDL_Texture *scoreTexture = nullptr;
SDL_Surface *scoreSurface = nullptr;
score1 = s / 50;
//score.setHighscore(score1);////
score=score1;
char scoreText[50];
sprintf(scoreText, " %d", score1);
if (scoreTexture)
{
SDL_DestroyTexture(scoreTexture);
scoreTexture = nullptr;
}
scoreSurface = TTF_RenderText_Solid(font, scoreText, text_color);
scoreTexture = SDL_CreateTextureFromSurface(renderer, scoreSurface);
int texW = 0, texH = 0;
SDL_QueryTexture(scoreTexture, NULL, NULL, &texW, &texH);
SDL_Rect scoreRect = {350, 180, texW, texH};
SDL_RenderCopy(renderer, scoreTexture, NULL, &scoreRect);
SDL_FreeSurface(scoreSurface);
SDL_DestroyTexture(scoreTexture);
TTF_CloseFont(font);
SDL_RenderPresent(renderer);
}
// Exiting the window
void GameLoop::Clear()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
// destructor for dealocating dynamic memeory of player type objects
GameLoop::~GameLoop()
{
sound.closeSDL();
delete p;
delete y;
delete o;
p = nullptr;
y = nullptr;
o = nullptr;
}
//Rule of 3
//copy constructor
GameLoop::GameLoop(const GameLoop& other) : window(other.window), renderer(other.renderer),
GameState(other.GameState), PauseState(other.PauseState),
startState(other.startState), p(new BirdB()),
y(new BirdY()), o(new BirdO()),
ground1(other.ground1), ground2(other.ground2),
Pipe_Above1(other.Pipe_Above1), Pipe_Below1(other.Pipe_Below1),
Pipe_Above2(other.Pipe_Above2), Pipe_Below2(other.Pipe_Below2),
Pipe_Above3(other.Pipe_Above3), Pipe_Below3(other.Pipe_Below3) {}
//assignment operator overloading
GameLoop &GameLoop::operator=(const GameLoop &other) {
if (this != &other) {
// Copy data from the source object
window = other.window;
renderer = other.renderer;
GameState = other.GameState;
PauseState = other.PauseState;
startState = other.startState;
// Delete existing objects
delete p;
delete y;
delete o;
// Create new objects and copy data
p = new BirdB();
y = new BirdY();
o = new BirdO();
ground1 = other.ground1;
ground2 = other.ground2;
Pipe_Above1 = other.Pipe_Above1;
Pipe_Below1 = other.Pipe_Below1;
Pipe_Above2 = other.Pipe_Above2;
Pipe_Below2 = other.Pipe_Below2;
Pipe_Above3 = other.Pipe_Above3;
Pipe_Below3 = other.Pipe_Below3;
}
return *this;
}
void GameLoop::operator=(const bool x){
startState=x;
}