-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaper.cpp
663 lines (597 loc) · 20.9 KB
/
leaper.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
#include "leaper.h"
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
Texture level_map;
Texture actor_map;
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;
Tilemap tiled_map;
std::vector<SDL_Rect> level_sprites;
int level_sprites_gid_offset;
SDL_Rect actor_sprites[7];
int actor_sprites_gid_offset;
SDL_Rect camera = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
// struct Block {
// int sprite;
// SDL_Rect collider;
// };
struct GameState {
int state;
int score;
int lives;
int level;
// Block* blocks[LEVEL_TILE_WIDTH*LEVEL_TILE_HEIGHT];
};
GameState gs;
struct EditorState {
int active_sprite;
bool level_edit;
};
EditorState es;
class Player {
public:
static const int width = TILE_SIZE; // pixels
static const int height = TILE_SIZE;
static const int colw = TILE_SIZE/2;
static const int colh = (TILE_SIZE/4)*3;
static constexpr float max_speed = 200.0; //pixels / second
static constexpr float jump_speed = 200.0; //pixels / second
// static constexpr float acceleration = 320.0; //pixels / second^2
static constexpr float gravity = 800.0; //pixels / second^2 (134)
static constexpr float max_fall_speed = gravity; //pixels / second
Player();
void handle_event(SDL_Event &event);
void move(float delta);
void render();//v2 camera);
void set_camera();
v2 get_position();
v2 get_velocity();
int get_lives();
int get_hp();
// std::vector<Bullet> shots;
SDL_Rect collider;
// void fire();
private:
v2 position;
v2 velocity;
int hp;
int lives;
float walk_speed;
bool up, down, left, right;
bool jump, in_jump;
SDL_RendererFlip flip;
int frame;
int walk_frame;
};
Player::Player() {
position = {LEVEL_WIDTH/2, LEVEL_HEIGHT-(SCREEN_HEIGHT/2)};
frame = STAND0;
walk_frame = WALK0;
hp = 10;
collider = {static_cast<int>(position.x)-width/2, static_cast<int>(position.y)-height/2, colw, colh};
lives = 3;
walk_speed = 0.0;
velocity = {0,0};
up = false;
down = false;
left = false;
right = false;
jump = false;
in_jump = false;
flip = SDL_FLIP_NONE;
}
void Player::handle_event(SDL_Event &event) {
// FIXME: theses feel sloppy with respect to simultaneous presses
if (event.type == SDL_KEYDOWN) { // && event.key.repeat == 0) {
switch (event.key.keysym.sym) {
case SDLK_UP:
up = true;
break;
case SDLK_DOWN:
down = true;
break;
case SDLK_LEFT:
left = true;
break;
case SDLK_RIGHT:
right = true;
break;
case SDLK_SPACE:
// fire();
if (!in_jump) {
jump = true;
in_jump = true;
}
break;
default:
break;
}
} else if (event.type == SDL_KEYUP) { // && event.key.repeat == 0) {
switch (event.key.keysym.sym) {
case SDLK_UP:
up = false;
break;
case SDLK_DOWN:
down = false;
break;
case SDLK_LEFT:
left = false;
break;
case SDLK_RIGHT:
right = false;
break;
case SDLK_SPACE:
jump = false;
break;
default:
break;
}
}
}
void Player::move(float delta) {
v2 position_o = position;
if (left) {
// velocity.x -= acceleration * delta;
velocity.x = -max_speed;
}
if (right) {
// velocity.x += acceleration * delta;
velocity.x = max_speed;
}
if (!left && !right) {
velocity.x = 0.0;
}
if (velocity.x > max_speed) {
velocity.x = max_speed;
} else if (velocity.x < -max_speed) {
velocity.x = -max_speed;
}
if (jump && !in_jump) {
velocity.y = -jump_speed;
}
// //friction
// velocity.x *= .99;
// velocity.y *= .99;
position.x += velocity.x * delta;
position.y += velocity.y * delta;
collider.x = position.x;
collider.y = position.y;
// TODO: check for collisions
// look at all the tiles in the same rows and columns as the player, do x, then y
// if there is nothing colliding below, apply gravity to y
// if there is something colliding to the side, reduce speed to 0 on that direction
int x_min, x_max, y_min, y_max;
v2 tile_position = screen_to_tile(V2(collider.x,collider.y));
x_min = tile_position.x - 1;
if (x_min < 0) x_min = 0;
x_max = tile_position.x + 1;
if (x_max >= LEVEL_TILE_WIDTH) x_max = LEVEL_TILE_WIDTH -1;
y_min = tile_position.y - 1;
if (y_min < 0) y_min = 0;
y_max = tile_position.y + 1;
if (y_max >= LEVEL_TILE_HEIGHT) y_max = LEVEL_TILE_HEIGHT -1;
unsigned collisions = 0b000;
v2 top_of_ground = {0,0};
for (int y = y_min; y <= y_max; y++) {
for (int x = x_min; x <= x_max; x++) {
int gid = tiled_map.tile_gid(LEVEL_TILE_WIDTH*y+x);
if (gid > 0) {
collisions |= check_collision(collider, tiled_map.get_collider(gid, x*TILE_SIZE, y*TILE_SIZE));
if (collisions & BOTTOM) {
top_of_ground.x = position.x;
top_of_ground.y = tiled_map.get_collider(gid, x*TILE_SIZE, y*TILE_SIZE).y - collider.h;
break;
}
}
}
}
// std::cout << screen_to_tile(position) << " " << collisions;
// if (collisions & TOP) std::cout << " top";
// if (collisions & BOTTOM) std::cout << " bottom";
// if (collisions & LEFT) std::cout << " left";
// if (collisions & RIGHT) std::cout << " right";
// std::cout << std::endl;
if (collisions & BOTTOM) {
// set position to be just touching, not fall a delta distance again
position = top_of_ground;
velocity.y = 0.0;
in_jump = false;
} else {
velocity.y += gravity * delta;
if (velocity.y > max_fall_speed) {
velocity.y = max_fall_speed;
}
}
if (collider.x < 0) {
position.x = 0;
collider.x = position.x;
velocity.x = 0.0;
} else if (collider.x > SCREEN_WIDTH - colw) {
position.x = SCREEN_WIDTH - colw;
collider.x = position.x;
velocity.x = 0.0;
}
set_camera();
}
void Player::render() {
// change what frame is rendered depending on movement
frame = STAND0;
if (velocity.x < 0) {
flip = SDL_FLIP_NONE;
frame = (walk_frame/9)+2;
}
if (velocity.x > 0 ) {
flip = SDL_FLIP_HORIZONTAL;
frame = (walk_frame/9)+2;
}
if (velocity.y > 0 || velocity.y < 0) {
frame = JUMP;
}
actor_map.render((position.x-(colw/2)) - camera.x, (position.y-(height/4)) - camera.y, &actor_sprites[frame], 0.0, NULL, flip);
walk_frame++;
if (walk_frame/9 >= WALK_FRAMES) {
walk_frame = WALK0;
}
}
void Player::set_camera() {
camera.x = 0;
camera.y = (position.y + height/2) - (SCREEN_HEIGHT/2);
if (camera.y < 0) {
camera.y = 0;
}
if (camera.y > LEVEL_HEIGHT - camera.h) {
camera.y = LEVEL_HEIGHT - camera.h;
}
}
v2 Player::get_position() {
return position;
}
v2 Player::get_velocity() {
return velocity;
}
int Player::get_lives() {
return lives;
}
int Player::get_hp() {
return hp;
}
// void Player::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("Leaper", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
// window = SDL_CreateWindow("Leaper", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, LEVEL_WIDTH, LEVEL_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 (!level_map.load_from_file("res/leaper_tiles.png")) {
success = false;
} else {
// set standard alpha blending
// level_map.set_blend_mode(SDL_BLENDMODE_BLEND);
// level_map.set_alpha(255);
}
if (!actor_map.load_from_file("res/leaper_character.png")) {
success = false;
} else {
// set standard alpha blending
// actor_map.set_blend_mode(SDL_BLENDMODE_BLEND);
// actor_map.set_alpha(255);
}
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;
}
if (!tiled_map.load_from_file("res/leaperB.json")) {
success = false;
}
// TODO: replace this with tilemap files and level map files (no more hardcodes)
actor_sprites[STAND0].x = 0;
actor_sprites[STAND0].y = 0;
actor_sprites[STAND0].w = 32;
actor_sprites[STAND0].h = 32;
actor_sprites[STAND1].x = 32;
actor_sprites[STAND1].y = 0;
actor_sprites[STAND1].w = 32;
actor_sprites[STAND1].h = 32;
actor_sprites[WALK0].x = 64;
actor_sprites[WALK0].y = 0;
actor_sprites[WALK0].w = 32;
actor_sprites[WALK0].h = 32;
actor_sprites[WALK1].x = 96;
actor_sprites[WALK1].y = 0;
actor_sprites[WALK1].w = 32;
actor_sprites[WALK1].h = 32;
actor_sprites[WALK2].x = 128;
actor_sprites[WALK2].y = 0;
actor_sprites[WALK2].w = 32;
actor_sprites[WALK2].h = 32;
actor_sprites[JUMP].x = 160;
actor_sprites[JUMP].y = 0;
actor_sprites[JUMP].w = 32;
actor_sprites[JUMP].h = 32;
actor_sprites[HIT].x = 192;
actor_sprites[HIT].y = 0;
actor_sprites[HIT].w = 32;
actor_sprites[HIT].h = 32;
return success;
}
// void delete_level() {
// for (int x = 0; x < LEVEL_TILE_WIDTH; x++) {
// for (int y = 0; y < LEVEL_TILE_HEIGHT; y++) {
// if (gs.blocks[LEVEL_TILE_WIDTH*y+x] != NULL) {
// delete gs.blocks[LEVEL_TILE_WIDTH*y+x];
// gs.blocks[LEVEL_TILE_WIDTH*y+x] = NULL;
// }
// }
// }
// }
bool close() {
// tmx_map_free(map);
//TODO: free tilesets, cute_tiled* things
// delete_level();
level_map.free();
actor_map.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() {
// score
scoreText.str("");
scoreText << " ";
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 << "LEAPER";
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() {
// TODO: render blocks from bottom to top, clipping through the camera
tiled_map.render_layers(camera);
if (es.level_edit) {
if (es.active_sprite != EMPTY) {
level_map.render(0,0, &level_sprites[es.active_sprite]);
}
}
}
void cycle_edit_sprite() {
es.active_sprite += 1;
if (es.active_sprite >= level_sprites.size()) {
es.active_sprite = 0;
}
}
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;
Player player;
v2 player_position;
//starting initialization
gs.state = START;
es.level_edit = false;
es.active_sprite = GRASS0;
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_3:
es.level_edit = !es.level_edit;
break;
case SDLK_4:
cycle_edit_sprite();
break;
case SDLK_9:
break;
case SDLK_0:
break;
case SDLK_KP_ENTER:
case SDLK_RETURN:
switch (gs.state) {
case START:
gs.state = PLAY;
gs.level = 1;
break;
case PAUSE:
gs.state = PLAY;
break;
case PLAY:
gs.state = PAUSE;
break;
case OVER:
player = Player();
player_position = player.get_position();
gs.state = START;
break;
default:
break;
}
break;
}
}
// else if (event.type == SDL_MOUSEBUTTONDOWN && es.level_edit) { // || event.type == SDL_MOUSEBUTTONUP) {
// //Get mouse position
// int x, y;
// SDL_GetMouseState( &x, &y );
// v2 target_tile = screen_to_tile(V2(x,y));
// int index = LEVEL_TILE_WIDTH*static_cast<int>(target_tile.y)+static_cast<int>(target_tile.x);
// if (gs.blocks[index] == NULL) {
// gs.blocks[index] = new Block();
// }
// gs.blocks[index]->sprite = es.active_sprite;
// switch (es.active_sprite) {
// case EMPTY:
// case GRASS0:
// case GRASS1:
// case GRASS2:
// gs.blocks[index]->collider = {0,0,0,0};
// break;
// case GROUND0:
// case GROUND1:
// case GROUND2:
// gs.blocks[index]->collider = {static_cast<int>(target_tile.x)*TILE_SIZE, static_cast<int>(target_tile.y)*TILE_SIZE, TILE_SIZE, TILE_SIZE};
// break;
// case GRASS_LEDGE:
// case STONE_LEDGE:
// gs.blocks[index]->collider = {static_cast<int>(target_tile.x)*TILE_SIZE, static_cast<int>(target_tile.y)*TILE_SIZE, TILE_SIZE, TILE_SIZE/3};
// break;
// }
// }
player.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
player.move(delta); //also updates the camera
// 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
if (gs.state == PLAY) {
render_scene();// background/level
player.render();// player and bullets rendered
}
render_status();
if (collision_debug) {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); //white
//player collider
SDL_Rect col = player.collider;
SDL_RenderDrawRect(renderer, &col);
for (int y = 0; y < LEVEL_TILE_HEIGHT; y++) {
for (int x = 0; x < LEVEL_TILE_WIDTH; x++) {
int gid = tiled_map.tile_gid(LEVEL_TILE_WIDTH*y+x);
if (gid > 0) {
SDL_Rect c = tiled_map.get_collider(gid, x*TILE_SIZE, y*TILE_SIZE);
SDL_RenderDrawRect(renderer, &c);
}
}
}
SDL_SetRenderDrawColor(renderer, 0x0, 0x0, 0x0, 0xFF); //black
}
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); //white
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;
}