-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroids.cpp
853 lines (789 loc) · 26.2 KB
/
asteroids.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
#include "asteroids.h"
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
Texture spritemap;
RawTexture starfield;
SDL_Rect sprites[19];
Timer frame_timer;
Timer movement_timer;
Timer fps_timer;
std::stringstream scoreText;
std::stringstream statusText;
std::stringstream fpsText;
Texture t_score;
Texture t_status;
Texture t_fps;
struct GameState {
int state;
int score;
int ships;
};
GameState gs;
bool check_collision(Circle a, SDL_Rect b) {
int colx, coly;
if (a.x < b.x) {
colx = b.x;
} else if (a.x > b.x + b.w) {
colx = b.x + b.w;
} else {
colx = a.x;
}
if (a.y < b.y) {
coly = b.y;
} else if (a.y > b.y + b.h) {
coly = b.y + b.h;
} else {
coly = a.y;
}
if (dist_sqr(a,V2(colx,coly)) < a.r*a.r) {
return true;
}
return false;
}
bool check_collision(Circle a, Circle b) {
int total_rad_sqr = a.r + b.r;
total_rad_sqr = total_rad_sqr * total_rad_sqr;
if (dist_sqr(a,b) < (total_rad_sqr)) {
return true;
}
return false;
}
bool check_collision(SDL_Rect a, SDL_Rect b) {
// assume collision [separating axis text]
int leftA, leftB, rightA, rightB, topA, topB, bottomA, bottomB;
//sides of a
leftA = a.x;
rightA = a.x + a.w;
topA = a.y;
bottomA = a.y + a.h;
//sides of b
leftB = b.x;
rightB = b.x + b.w;
topB = b.y;
bottomB = b.y + b.h;
//check for collisions!
if (bottomA <= topB) return false;
if (topA >= bottomB) return false;
if (rightA <= leftB) return false;
if (leftA >= rightB) return false;
//none of the sides from a are outside b
return true;
}
class Asteroid {
public:
Asteroid(v2 spawn_position, int spawn_size, int spawn_distance);
// ~Asteroid();
void move(float delta);
void render(v2 camera);
void set_hp(int size);
// SDL_Rect collider;
Circle collider;
bool hit(int damage);
v2 get_position();
int get_size();
int get_hp();
int update_hp(int damage);
private:
int width;
int height;
v2 position;
int speed;
double angle;
double angular_velocity;
int size;
int hp;
};
Asteroid::Asteroid(v2 spawn_position, int spawn_size = 0, int spawn_distance = 0) {
if (spawn_size > 0) {
size = spawn_size;
} else {
size = (rand() % 13) + 6; //AST_M0;
}
set_hp(size);
width = sprites[size].w;
height = sprites[size].h;
int x_sign = (rand() % 2 ? -1 : 1);
int y_sign = (rand() % 2 ? -1 : 1);
if (spawn_distance > 0) {
position = {
spawn_position.x + ((rand() % spawn_distance)*x_sign + (spawn_distance/2)*x_sign),
spawn_position.y + ((rand() % spawn_distance)*y_sign + (spawn_distance/2)*y_sign)
};
} else {
position = {
spawn_position.x + ((rand() % 100)*x_sign + 50*x_sign),
spawn_position.y + ((rand() % 100)*y_sign + 50*y_sign)
};
}
speed = rand() % 20; // pixels/second
angle = rand() % 360;
angular_velocity = rand() % 90; // degrees /second
// collider = {position.x-width/2, position.y-height/2, width, height};
collider = {position.x, position.y, width/2};
}
void Asteroid::set_hp(int size) {
switch (size) {
case AST_T0:
case AST_T1:
case AST_T2:
case AST_T3:
hp = 1;
break;
case AST_S0:
case AST_S1:
case AST_S2:
case AST_S3:
hp = 2;
break;
case AST_M0:
case AST_M1:
case AST_M2:
case AST_M3:
hp = 3;
break;
case AST_L0:
hp = 4;
break;
}
}
bool Asteroid::hit(int damage) {
hp -= damage;
bool dead = false;
if (hp <= 0) {
dead = true;
}
return dead;
}
v2 Asteroid::get_position() {
return position;
}
int Asteroid::get_size() {
return size;
}
int Asteroid::get_hp() {
return hp;
}
int Asteroid::update_hp(int damage) {
hp -= damage;
return hp;
}
void Asteroid::move(float delta) {
position.x += speed * delta;
position.y += speed * delta;
angle += angular_velocity * delta;
// collider.x = position.x-width/2;
// collider.y = position.y-height/2;
collider.x = position.x;
collider.y = position.y;
}
void Asteroid::render(v2 camera) {
spritemap.render(position.x-(width/2)-camera.x, position.y-(height/2)-camera.y, &sprites[size], angle);
}
class Bullet {
public:
static const int width = 5;
static const int height = 5;
static const int speed = 640.0; //pixels / second
Bullet(v2 ship_position, double ship_angle);
// void handle_event(SDL_Event &event);
void move(float delta, std::vector<Asteroid> &asteroids);
void render(v2 camera);
float get_time_to_death();
SDL_Rect collider;
private:
v2 position;
double angle;
float time_to_death;
};
Bullet::Bullet(v2 ship_position, double ship_angle) {
position = {ship_position.x, ship_position.y};
angle = ship_angle;
time_to_death = 1.0;
collider = {position.x-width/2, position.y-height/2, width, height};
}
void Bullet::move(float delta, std::vector<Asteroid> &asteroids) {
position.x += cos((angle+45.0)*PI/180.0) * speed * delta;
position.y += sin((angle+45.0)*PI/180.0) * speed * delta;
time_to_death -= delta;
collider.x = position.x-width/2;
collider.y = position.y-height/2;
std::vector<Asteroid> holding;
// check for collision with asteroids
std::vector<Asteroid>::iterator astr;
for (astr = asteroids.begin(); astr != asteroids.end();) {
if(check_collision(astr->collider, collider)) {
time_to_death = 0.0;
if(astr->hit(1)) { //damage of bullet, returns bool when destroyed
// spawn new asteroids at that position
v2 pos = astr->get_position();
int size = astr->get_size();
if (size == 18) {
gs.score += 100;
//medium
for (int i = 0; i < 4; i++) {
holding.push_back(Asteroid(pos, (rand() % 4) + 14, 10));
}
} else if (size > 13 && size < 18) {
gs.score += 50;
//small
for (int i = 0; i < 4; i++) {
holding.push_back(Asteroid(pos, (rand() % 4) + 10, 10));
}
} else if (size > 9 && size < 14) {
gs.score += 25;
//tiny
for (int i = 0; i < 4; i++) {
holding.push_back(Asteroid(pos, (rand() % 4) + 6, 10));
}
} else {
gs.score += 10;
}
astr = asteroids.erase(astr);
} else {
++astr;
}
} else {
++astr;
}
}
std::vector<Asteroid>::iterator hold = holding.begin();
while (hold != holding.end()) {
asteroids.push_back(*hold);
hold = holding.erase(hold);
}
}
void Bullet::render(v2 camera) {
spritemap.render(position.x-(width/2)-camera.x, position.y-(height/2)-camera.y, &sprites[BULLET], angle);
}
float Bullet::get_time_to_death() {
return time_to_death;
}
class Ship {
public:
static const int width = 32; // pixels
static const int height = 32;
static const int thrust_width = 28; // pixels
static const int thrust_height = 28;
static const int thrust_speed = 320.0; //pixels / second
static const int rotation_speed = 360.0; //degrees / second
Ship();
void handle_event(SDL_Event &event);
void move(float delta, std::vector<Asteroid> &asteroids);
void render(v2 camera);
v2 get_position();
int get_lives();
int get_hp();
std::vector<Bullet> shots;
// SDL_Rect collider;
Circle collider;
void fire();
private:
v2 position;
double angle;
double angular_velocity;
int frame;
bool thruster;
int hp;
int lives;
};
Ship::Ship() {
position = {LEVEL_WIDTH/2,LEVEL_HEIGHT/2};
angle = 0.0; //clockwise, -> is 0, angle starts at 45 degrees offset from 0
angular_velocity = 0.0;
frame = 0;
thruster = false;
hp = 3;
// collider = {position.x-width/2, position.y-height/2, width, height};
collider = {position.x, position.y, width/2};
lives = 3;
}
void Ship::handle_event(SDL_Event &event) {
if (event.type == SDL_KEYDOWN) { // && event.key.repeat == 0) {
switch (event.key.keysym.sym) {
case SDLK_UP:
angular_velocity = -rotation_speed;
break;
case SDLK_DOWN:
angular_velocity = rotation_speed;
break;
case SDLK_LEFT:
thruster = true;
break;
case SDLK_RIGHT:
break;
case SDLK_SPACE:
fire();
break;
default:
break;
}
} else if (event.type == SDL_KEYUP) { // && event.key.repeat == 0) {
switch (event.key.keysym.sym) {
case SDLK_UP:
angular_velocity = 0.0;
break;
case SDLK_DOWN:
angular_velocity = 0.0;
break;
case SDLK_LEFT:
thruster = false;
break;
case SDLK_RIGHT:
break;
case SDLK_SPACE:
break;
default:
break;
}
}
}
void Ship::move(float delta, std::vector<Asteroid> &asteroids) {
v2 position_o = position;
// position += velocity;
angle += angular_velocity * delta;
if (thruster) { //clockwise, -> is 0, angle starts at 45 degrees offset from 0
position.x += cos((angle+45.0)*PI/180.0) * thrust_speed * delta;
position.y += sin((angle+45.0)*PI/180.0) * thrust_speed * delta;
}
// collider.x = position.x-width/2;
// collider.y = position.y-height/2;
collider.x = position.x;
collider.y = position.y;
// check for collision with asteroids
std::vector<Asteroid>::iterator astr;
for (astr = asteroids.begin(); astr != asteroids.end();) {
if(check_collision(astr->collider, collider)) {
position = position_o;
collider.x = position.x;
collider.y = position.y;
thruster = false;
hp -= astr->get_hp();
if (hp <= 0) {
lives -= 1;
hp = 3;
// teleport to start position
position = {LEVEL_WIDTH/2, LEVEL_HEIGHT/2};
collider = {position.x, position.y, width/2};
if (lives <= 0) {
gs.state = OVER;
}
}
}
++astr;
}
// move bullets
// check expiration
std::vector<Bullet>::iterator itr;
for (itr = shots.begin(); itr != shots.end();) {
itr->move(delta, asteroids);
if (itr->get_time_to_death() <= 0) {
itr = shots.erase(itr);
} else {
++itr;
}
}
}
void Ship::render(v2 camera) {
spritemap.render(position.x-(width/2)-camera.x, position.y-(height/2)-camera.y, &sprites[SHIP], angle);
if (thruster) {
//animate thruster
SDL_Point rot_pos = {thrust_width+2,thrust_height+2};
spritemap.render(position.x-(thrust_width+2)-camera.x, position.y-(thrust_height+2)-camera.y, &sprites[frame / 4], angle, &rot_pos);
frame++;
if (frame / 4 >= ANIMATION_FRAMES) { // loop animation
frame = THRUST0;
}
}
// render bullets
for ( auto &i : shots) {
i.render(camera);
}
}
v2 Ship::get_position() {
return position;
}
int Ship::get_lives() {
return lives;
}
int Ship::get_hp() {
return hp;
}
void Ship::fire() {
shots.push_back(Bullet(position, angle));
}
bool init() {
bool success = true;
if (SDL_Init(SDL_INIT_VIDEO) != 0){
logSDLError(std::cout, "SDL_Init");
success = false;
} else {
if (TTF_Init() != 0){ //Future: do we need to call IMG_Init, it seems to work without it...is speed a factor? IMG_GetError might be needed
logSDLError(std::cout, "TTF_Init");
success = false;
} else {
window = SDL_CreateWindow("Asteroids", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr){
logSDLError(std::cout, "SDL_CreateWindow");
success = false;
} else {
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr){
logSDLError(std::cout, "SDL_CreateRenderer");
success = false;
}
}
}
}
srand(rdtsc()); //seed random nicely
return success;
}
bool load() {
bool success = true;
int pixel_format = 0;
if (!spritemap.load_from_file("res/asteroids.png")) {
success = false;
} else {
// set standard alpha blending
// spritemap.set_blend_mode(SDL_BLENDMODE_BLEND);
// spritemap.set_alpha(255);
}
if (!starfield.initialize(LEVEL_WIDTH, LEVEL_HEIGHT)) {
success = false;
} else {
// generate starfield
if (!starfield.lock_texture()) {
std::cout << "Unable to lock starfield texture" << std::endl;
} else {
int format = SDL_GetWindowPixelFormat(window);
SDL_PixelFormat* mapping_format = SDL_AllocFormat(format);
int* pixels = static_cast<int*>(starfield.get_pixels());
int pixel_count = (starfield.get_pitch() / 4) * starfield.get_height();
int black = SDL_MapRGBA(mapping_format, 0x00, 0x00, 0x00, 0xFF);
int white = SDL_MapRGBA(mapping_format, 0xFF, 0xFF, 0xFF, 0xFF);
int star;
// fill starfield with color
for (int i = 0; i < pixel_count; i++) {
star = static_cast<int>(rand() % 1000);
if (star > 998) {
pixels[i] = white;
} else {
pixels[i] = black;
}
}
starfield.unlock_texture();
SDL_FreeFormat(mapping_format);
}
}
font = TTF_OpenFont("res/cc.ttf", 12);
if (font == nullptr){
logSDLError(std::cout, "TTF_OpenFont");
success = false;
}
if (!t_score.load_from_rendered_text("_")) {
success = false;
}
if (!t_status.load_from_rendered_text("_")) {
success = false;
}
if (!t_fps.load_from_rendered_text("_", RED_)) {
success = false;
}
sprites[THRUST0].x = 32;
sprites[THRUST0].y = 0;
sprites[THRUST0].w = 28;
sprites[THRUST0].h = 28;
sprites[THRUST1].x = 60;
sprites[THRUST1].y = 0;
sprites[THRUST1].w = 28;
sprites[THRUST1].h = 28;
sprites[THRUST2].x = 88;
sprites[THRUST2].y = 0;
sprites[THRUST2].w = 28;
sprites[THRUST2].h = 28;
sprites[THRUST3].x = 116;
sprites[THRUST3].y = 0;
sprites[THRUST3].w = 28;
sprites[THRUST3].h = 28;
sprites[BULLET].x = 144;
sprites[BULLET].y = 0;
sprites[BULLET].w = 5;
sprites[BULLET].h = 5;
sprites[SHIP].x = 0;
sprites[SHIP].y = 0;
sprites[SHIP].w = 32;
sprites[SHIP].h = 32;
sprites[AST_T0].x = 0;
sprites[AST_T0].y = 64;
sprites[AST_T0].w = 16;
sprites[AST_T0].h = 16;
sprites[AST_T1].x = 16;
sprites[AST_T1].y = 64;
sprites[AST_T1].w = 16;
sprites[AST_T1].h = 16;
sprites[AST_T2].x = 32;
sprites[AST_T2].y = 64;
sprites[AST_T2].w = 16;
sprites[AST_T2].h = 16;
sprites[AST_T3].x = 48;
sprites[AST_T3].y = 64;
sprites[AST_T3].w = 16;
sprites[AST_T3].h = 16;
sprites[AST_S0].x = 0;
sprites[AST_S0].y = 32;
sprites[AST_S0].w = 32;
sprites[AST_S0].h = 32;
sprites[AST_S1].x = 32;
sprites[AST_S1].y = 32;
sprites[AST_S1].w = 32;
sprites[AST_S1].h = 32;
sprites[AST_S2].x = 64;
sprites[AST_S2].y = 32;
sprites[AST_S2].w = 32;
sprites[AST_S2].h = 32;
sprites[AST_S3].x = 64;
sprites[AST_S3].y = 64;
sprites[AST_S3].w = 32;
sprites[AST_S3].h = 32;
sprites[AST_M0].x = 0;
sprites[AST_M0].y = 96;
sprites[AST_M0].w = 64;
sprites[AST_M0].h = 64;
sprites[AST_M1].x = 64;
sprites[AST_M1].y = 96;
sprites[AST_M1].w = 64;
sprites[AST_M1].h = 64;
sprites[AST_M2].x = 128;
sprites[AST_M2].y = 96;
sprites[AST_M2].w = 64;
sprites[AST_M2].h = 64;
sprites[AST_M3].x = 96;
sprites[AST_M3].y = 32;
sprites[AST_M3].w = 64;
sprites[AST_M3].h = 64;
sprites[AST_L0].x = 192;
sprites[AST_L0].y = 32;
sprites[AST_L0].w = 128;
sprites[AST_L0].h = 128;
return success;
}
bool close() {
spritemap.free();
t_score.free();
t_status.free();
t_fps.free();
if (font != nullptr) { TTF_CloseFont(font); font = NULL; }
if (renderer != nullptr) { SDL_DestroyRenderer(renderer); renderer = NULL; }
if (window != nullptr) { SDL_DestroyWindow(window); window = NULL; }
TTF_Quit();
SDL_Quit();
return true;
}
void render_status(Ship &ship) {
// score
scoreText.str("");
scoreText << "Score: " << gs.score << " HP: " << ship.get_hp() << "/3 Lives: " << ship.get_lives();
t_score.load_from_rendered_text(scoreText.str());
t_score.render((SCREEN_WIDTH/2)-(t_score.get_width()/2), SCREEN_HEIGHT-(t_score.get_height()));
statusText.str("");
switch (gs.state) {
case START:
statusText << "ASTEROIDS";
break;
case PAUSE:
statusText << "PAUSED";
break;
case PLAY:
statusText << " ";
break;
case OVER:
statusText << "G A M E O V E R";
break;
}
t_status.load_from_rendered_text(statusText.str());
t_status.render((SCREEN_WIDTH/2)-(t_status.get_width()/2), SCREEN_HEIGHT/2);
}
void render_scene(bool debug = false) {
}
int main(int argc, char **argv) {
if (!init()) {
std::cout << "Initialization Failed" << std::endl;
} else {
if (!load()) {
std::cout << "Loading Failed" << std::endl;
} else {
bool quit = false;
bool fps_on = false;
bool collision_debug = false;
int frame_count = 0;
int frame_ticks = 0;
int delta_ticks = 0;
float avgFPS = 0.0;
Ship ship;
v2 ship_position;
ship_position = ship.get_position();
SDL_Rect camera = {ship_position.x - (SCREEN_WIDTH / 2), ship_position.y - (SCREEN_HEIGHT / 2), SCREEN_WIDTH, SCREEN_HEIGHT};
std::vector<Asteroid> asteroids;
//starting initialization
gs.state = START;
fps_timer.start();
while (!quit) {
frame_timer.start();
SDL_Event event;
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
quit = true;
}
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE ) {
quit = true;
}
if (event.type == SDL_KEYDOWN && event.key.repeat == 0) {
switch (event.key.keysym.sym) {
case SDLK_1:
fps_on = !fps_on;
break;
case SDLK_2:
collision_debug = !collision_debug;
break;
case SDLK_8:
break;
case SDLK_9:
break;
case SDLK_0:
asteroids.push_back(Asteroid(ship.get_position()));
break;
case SDLK_KP_ENTER:
case SDLK_RETURN:
switch (gs.state) {
case START:
gs.state = PLAY;
break;
case PAUSE:
gs.state = PLAY;
break;
case PLAY:
gs.state = PAUSE;
break;
case OVER:
ship = Ship();
ship_position = ship.get_position();
camera.x = ship_position.x - (SCREEN_WIDTH / 2);
camera.y = ship_position.y - (SCREEN_HEIGHT / 2);
asteroids.clear();
gs.state = START;
break;
default:
break;
}
break;
}
}
ship.handle_event(event);
}
delta_ticks = movement_timer.get_ticks();
float delta = delta_ticks / 1000.0;
switch (gs.state) {
case START:
break;
case PAUSE:
// don't move stuff
break;
case PLAY:
// move things with the delta
ship.move(delta, asteroids); //and bullets
ship_position = ship.get_position();
// move asteroids
for ( auto &a : asteroids) {
a.move(delta);
}
camera.x = ship_position.x - (SCREEN_WIDTH / 2);
camera.y = ship_position.y - (SCREEN_HEIGHT / 2);
// keep the camera in level bounds
if(camera.x < 0) {
camera.x = 0;
}
if(camera.y < 0) {
camera.y = 0;
}
if(camera.x > LEVEL_WIDTH - camera.w) {
camera.x = LEVEL_WIDTH - camera.w;
}
if(camera.y > LEVEL_HEIGHT - camera.h) {
camera.y = LEVEL_HEIGHT - camera.h;
}
// stop moving things with the delta
break;
case OVER:
break;
default:
break;
}
movement_timer.start();
SDL_SetRenderDrawColor(renderer, 0x0, 0x0, 0x0, 0xFF); //black
SDL_RenderClear(renderer);
// render things
// render_scene();
starfield.render(0, 0, &camera);
if (gs.state == PLAY) {
ship.render(V2(camera.x, camera.y)); // ship and bullets rendered
// render asteroids
for ( auto &a : asteroids) {
a.render(V2(camera.x, camera.y));
}
}
render_status(ship);
if (collision_debug) {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); //white
//ship collider
// SDL_Rect scol = ship.collider;
// scol.x -= camera.x;
// scol.y -= camera.y;
// SDL_RenderDrawRect(renderer, &scol);
Circle scirc = ship.collider;
scirc.x -= camera.x;
scirc.y -= camera.y;
DrawCircle(renderer, scirc);
//bullet colliders
for ( auto &b : ship.shots) {
SDL_Rect bcol = b.collider;
bcol.x -= camera.x;
bcol.y -= camera.y;
SDL_RenderDrawRect(renderer, &bcol);
}
//asteroid colliders
for ( auto &a : asteroids) {
// SDL_Rect acol = a.collider;
// acol.x -= camera.x;
// acol.y -= camera.y;
// SDL_RenderDrawRect(renderer, &acol);
Circle acirc = a.collider;
acirc.x -= camera.x;
acirc.y -= camera.y;
DrawCircle(renderer, acirc);
}
SDL_SetRenderDrawColor(renderer, 0x0, 0x0, 0x0, 0xFF); //black
}
// stop rendering things
if (fps_on) {
// calculate and render FPS
avgFPS = frame_count / (fps_timer.get_ticks() / 1000.0);
if (avgFPS > 2000000) {
avgFPS = 0;
}
fpsText.str("");
fpsText << avgFPS << "fps";
t_fps.load_from_rendered_text(fpsText.str(), RED_);
t_fps.render(SCREEN_WIDTH-t_fps.get_width(), 0);
}
SDL_RenderPresent(renderer);
frame_count++;
// already using delta for calculating position, now using to cap fps at 60
frame_ticks = frame_timer.get_ticks();
if (frame_ticks < SCREEN_TICKS_PER_FRAME) {
SDL_Delay(SCREEN_TICKS_PER_FRAME - frame_ticks);
}
}
}
}
close();
return 0;
}