-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
509 lines (376 loc) · 11.1 KB
/
Source.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
#pragma once
#include <iostream>
#include <cmath>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <sstream>
#include <string>
#include <fstream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#include "TextureAPI.h"
#include "Timer.h"
#include "Character.h"
#include "Enemy.h"
#include "Enemies.h"
#include "EnemyBullet.h"
#include "GlobalResource.h"
using namespace std;
int main(int argc, char* argv[]) {
srand(time(NULL));
SDL_Window * window = NULL;
SDL_Renderer *renderer = NULL;
const int SCREEN_WIDTH = GlobalResource::SCREEN_WIDTH;
const int SCREEN_HEIGHT = GlobalResource::SCREEN_HEIGHT;
/*Initialize */
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
cout << "\nCan not initialize SDL"<<SDL_GetError();
}
else {
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
cout << "\nWarning: Linear texture filtering not enabled!";
}
else {
window = SDL_CreateWindow("Shooting star", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,SCREEN_HEIGHT, SDL_WINDOW_SHOWN);//Create window
if (window == NULL) {
cout << "\nCould no init window " << SDL_GetError();
}
else {
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
cout << "\nCould not init renderer"<<SDL_GetError();
}
else {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags)) {
cout << "\nSDL_image could not initialize! SDL_image Error: " << IMG_GetError();
}
if (TTF_Init() == -1) {
cout << "\nSDL_ttf could not initialize! SDL_ttf Error: " << TTF_GetError();
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) < 0) {
cout << "\nSDL_mixer could not initialize!" << Mix_GetError();
}
}
}
}
}
/*Load media*/
// Load highscore
ifstream highScoreFileInput("Resource/highscore.txt");
long highScore;
highScoreFileInput >> highScore;
highScoreFileInput.close();
//Load fps
TextureAPI fpsTexture;
//Load music
Mix_Music *backgroundMusic = Mix_LoadMUS("Resource/Sound/shootingStar.mp3");
Mix_Music *menuMusic = Mix_LoadMUS("Resource/Sound/undertale.mp3");
Mix_Chunk *fireSound = Mix_LoadWAV("Resource/Sound/shot.wav");
//Load background
TextureAPI background;
background.loadFromFile(renderer, "Resource/Image/background.png");
//Load Character
Character mainCharacter;
mainCharacter.loadFromFile(renderer, "Resource/Image/main-character.png");
//Load enemy
Enemies enemies;
enemies.loadFromFile(renderer, "Resource/Image/enemy.png");
//Load bullet
EnemyBullet charBullet;
charBullet.loadFromFile(renderer, "Resource/Image/bullet.png");
EnemyBullet enemyBullets;
enemyBullets.loadFromFile(renderer, "Resource/Image/greenbullet.png");
//load font
TTF_Font *gugi;
gugi = TTF_OpenFont("Resource/Font/Gugi.ttf",40);
TTF_Font *gugi20;
gugi20 = TTF_OpenFont("Resource/Font/Gugi.ttf", 20);
//Score texture
TextureAPI highScoreTexture;
TextureAPI scoreTexture;
TextureAPI scoreBackground;
scoreBackground.loadFromFile(renderer, "Resource/Image/Nop.png");
//Game over texture
TextureAPI gameOverTexture;
stringstream gameOverText;
gameOverText.str("GAME OVER");
gameOverTexture.loadFromRenderedText(renderer, gameOverText.str(), { 255,255,255,255 }, gugi);
TextureAPI restartTexture;
stringstream restartText;
restartText.str("Press R to restart");
restartTexture.loadFromRenderedText(renderer, restartText.str(), { 255,255,255,255 }, gugi20);
//Load menu
TextureAPI menuTexture;
menuTexture.loadFromFile(renderer, "Resource/Image/menu.png");
/*Main loop*/
int scrollingOffset = 0;//this make the screen scroll forever
unsigned int stage = 0;
bool isGameOver = false;
double level = 4;
stringstream scoreText;
stringstream highScoreText;
stringstream fpsText;
ofstream highScoreFileOutput;
long score = 0;
Timer gameTimer;
Timer createEnemyTimer;
Timer frameTimer;
Timer scrollingTimer;
Timer characterShootTimer;
Timer enemyShootTimer;
int bulletCounter = 0;
bool quit = false;
SDL_Event e;
//Menu
bool isInMenu = true;
bool isInGame = false;
int menuCurrentState = 1;
SDL_Rect menuClip;
while (!quit) {
//Menu
if(isInMenu) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = true;
}
if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_UP:
menuCurrentState--;
if (menuCurrentState < 1) {
menuCurrentState = 1;
}
break;
case SDLK_DOWN:
menuCurrentState++;
if (menuCurrentState > 2) {
menuCurrentState = 2;
}
break;
case SDLK_RETURN:
switch (menuCurrentState) {
case 1:
isInMenu = false;
isInGame = true;
gameTimer.start();
createEnemyTimer.start();
frameTimer.start();
scrollingTimer.start();
characterShootTimer.start();
enemyShootTimer.start();
break;
case 2:
isInMenu = false;
isInGame = false;
quit = true;
break;
}
break;
default:
break;
}
}
}
switch (menuCurrentState) {
case 1:
menuClip.x = 0;
menuClip.y = 0;
menuClip.w = 800;
menuClip.h = 600;
break;
case 2:
menuClip.x = 0;
menuClip.y = 0;
menuClip.w = 800;
menuClip.h = 600;
default:
menuClip.x = 800;
menuClip.y = 0;
menuClip.w = 800;
menuClip.h = 600;
break;
}
//Clear screen
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
// render menu
menuTexture.render(renderer, 0, 0, &menuClip);
//Update screen
SDL_RenderPresent(renderer);
if (frameTimer.getTicks() < 1000 / 65) {
SDL_Delay(1000 / 65 - frameTimer.getTicks());
}
}
//Game loop
if (isInGame) {
while (SDL_PollEvent(&e)) {
//Handle event
if (e.type == SDL_QUIT) {
quit = true;
}
mainCharacter.handleEvent(e);
if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_ESCAPE:
isInGame = false;
isInMenu = true;
Mix_HaltMusic();
}
}
if (isGameOver) {
if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_r) {
isGameOver = false;
gameTimer.start();
createEnemyTimer.start();
frameTimer.start();
scrollingTimer.start();
characterShootTimer.start();
enemyShootTimer.start();
level = 4;//Reset level
score = 0;
mainCharacter.setDefaultPlace(); //set main character to defalt place
enemies.clear(); //delete all enemy on screen
charBullet.clear();
enemyBullets.clear();
}
}
}
}
//Computing somthing
// Collision
if (enemies.isCollision(mainCharacter.getX() + 22, mainCharacter.getY() + 36, 4) || enemyBullets.isCollision(mainCharacter.getX() + 22, mainCharacter.getY() + 36, 4)) {
isGameOver = true;
Mix_HaltMusic();
gameTimer.stop();
createEnemyTimer.stop();
frameTimer.stop();
scrollingTimer.stop();
characterShootTimer.stop();
enemyShootTimer.stop();
}
if (!isGameOver) {
for (unsigned int i = 0; i < enemies.getEnemies().size(); i++) {
Enemy thisEnemy = enemies.getEnemies()[i];
for (unsigned int j = 0; j < charBullet.getBullets().size(); j++) {
if (charBullet.getBullets()[j].isCollision(thisEnemy.getX() + 24, thisEnemy.getY() + 24, 24)) {
score += 100;
enemies.deleteEnemy(i);
charBullet.deleteBullet(j);
break;
}
}
}
//Character shoot
if (characterShootTimer.getTicks() > 200) {
mainCharacter.createBullet(charBullet);
characterShootTimer.start();//reset Timer
}
mainCharacter.moveBullets(frameTimer.getTicks() / 1000.0);
// Score
if (score > highScore) {
highScore = score;
highScoreFileOutput.open("Resource/highscore.txt");
highScoreFileOutput << highScore;
highScoreFileOutput.close();
}
highScoreText.str("");
highScoreText << "High score: " << highScore;
highScoreTexture.loadFromRenderedText(renderer, highScoreText.str(), { 255,255,255,255 }, gugi);
scoreText.str("");
scoreText << "Your score: " << score;
scoreTexture.loadFromRenderedText(renderer, scoreText.str(), { 255,255,255,255 }, gugi);
//Create enemy
if (gameTimer.getTicks() > 300) {
stage = 1;
}
if (stage == 1) {
if (createEnemyTimer.getTicks() > 1000) {
enemies.createEnemy((rand() % 8) * 50, -GlobalResource::ENEMY_HEIGHT, GlobalResource::ENEMY_VEL, 270);
createEnemyTimer.start();
}
}
//Enemy shoot
if (enemyShootTimer.getTicks() > 1000) {
for (unsigned int index = 0; index < enemies.getEnemies().size(); index++) {
for (int i = 0; i < level; i++) {
double angle = i * (360 / level);
enemies.getEnemies()[index].createBullet(enemyBullets, 0, 0, angle);
}
}
level += 0.5;
bulletCounter++;
enemyShootTimer.start();
}
//Move
mainCharacter.move(frameTimer.getTicks() / 1000.0);
enemies.moveEnemy(frameTimer.getTicks() / 1000.0);
enemyBullets.moveBullets(GlobalResource::ENEMY_BULLET_VEL, frameTimer.getTicks() / 1000.0);
charBullet.moveBullets(GlobalResource::BULLET_VEL, frameTimer.getTicks() / 1000.0);
//FPS
fpsText.str("");
fpsText << "FPS: " << 1000.0 / frameTimer.getTicks();
fpsTexture.loadFromRenderedText(renderer, fpsText.str(), { 255,255,255,255 }, gugi);
frameTimer.start();
//Play music
if (Mix_PlayingMusic() == 0&& isInGame) {
//Play the music
Mix_PlayMusic(backgroundMusic, -1);
}
//Scroll screen
if (scrollingTimer.getTicks() > 16) {
scrollingOffset++;
scrollingTimer.start(); //Reset the timer
}
if (scrollingOffset > background.getHeight()) {
scrollingOffset = 0;
}
}
//Clear screen
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
//Render here
background.render(renderer, 0, scrollingOffset);
background.render(renderer, 0, scrollingOffset - background.getHeight());
mainCharacter.render(renderer);
enemies.render(renderer);
enemyBullets.render(renderer);
charBullet.render(renderer);
scoreBackground.render(renderer, 400, 0);
fpsTexture.render(renderer, 410, 30);
highScoreTexture.render(renderer, 410, 350);
scoreTexture.render(renderer, 410, 400);
if (isGameOver) {
gameOverTexture.render(renderer, 90, GlobalResource::MAIN_AREA_HEIGHT / 2);
restartTexture.render(renderer, 90, GlobalResource::MAIN_AREA_HEIGHT / 2 + 50);
}
//Update screen
SDL_RenderPresent(renderer);
if (frameTimer.getTicks() < 1000 / 65) {
SDL_Delay(1000 / 65 - frameTimer.getTicks());
}
}
}
/*Quit*/
background.free();
mainCharacter.free();
scoreTexture.free();
highScoreTexture.free();
SDL_DestroyRenderer(renderer);
renderer = NULL;
SDL_DestroyWindow(window);
window = NULL;
Mix_FreeMusic(backgroundMusic);
backgroundMusic = NULL;
Mix_FreeChunk(fireSound);
fireSound = NULL;
Mix_Quit();
TTF_Quit();
IMG_Quit();
SDL_Quit();
return 0;
}