-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
175 lines (138 loc) · 4.84 KB
/
Copy pathgame.cpp
File metadata and controls
175 lines (138 loc) · 4.84 KB
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
#include "game.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include "animation_data.h"
#include "constants.h"
#include "location.h"
#include "perf.h"
#include "z_fighter.h"
using constants::kBackgroundScale;
using constants::kGokuScale;
using constants::kGokuSpeed;
Game::Game() {}
Game::~Game() {}
bool Game::Run() {
if (!InitSdl()) {
return false;
}
// Initialize the game window.
graphics_.CreateWindow();
// Event struct to grab input events from SDL.
SDL_Event event;
SDL_Rect background_source = {.x = 132, .y = 0, .w = 500, .h = 298};
// Create background.
Sprite background("Namek Background", background_source, kBackgroundScale,
graphics_.GetRenderer());
Perf::GetPerf()->StartTimer("create_sprite");
AnimatedSprite goku_animated_sprite("goku_sprite_sheet(in_progress)",
graphics_.GetRenderer(), kGokuScale,
GetGokuAnimations());
Perf::GetPerf()->StopTimer("create_sprite");
Location goku_location(50, 500);
ZFighter goku(std::move(goku_animated_sprite), goku_location);
// If goku is super saiyan.
bool super_saiyan = false;
std::cout << sizeof(AnimatedSprite) << std::endl;
// The game loop.
while (true) {
frame_start_time_ms = SDL_GetTicks();
// Add the background
graphics_.AddSprite(background, Location(0, 0));
Perf::GetPerf()->StartTimer("game_loop");
Perf::GetPerf()->StartTimer("input");
// Clear out old keyup/keydown events.
input_.BeginNewFrame();
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
return true;
break;
case SDL_KEYDOWN:
if (event.key.repeat == 0) {
input_.KeyDownEvent(event);
}
break;
case SDL_KEYUP:
input_.KeyUpEvent(event);
break;
default:
// do nothing
break;
}
}
if (input_.WasKeyPressed(SDL_SCANCODE_ESCAPE)) {
return true;
}
// This is the farthest goku can go to the right before hitting the edge.
int goku_window_boundary =
constants::kWindowWidth - goku.GetMutableSprite().GetWidth();
// TODO: Move to player class.
if (input_.WasKeyPressed(SDL_SCANCODE_D) ||
input_.IsKeyHeld(SDL_SCANCODE_D)) {
// Move goku to the right by his speed, wrapping at the window edge.
goku.GetMutableLocation().x += kGokuSpeed;
// If goku is super saiyan, he moves faster.
goku.GetMutableLocation().x +=
super_saiyan ? constants::kSsGokuSpeedMod : 0;
if (goku.GetMutableLocation().x > goku_window_boundary) {
goku.GetMutableLocation().x = 0;
}
super_saiyan
? goku.GetMutableSprite().PlayAnimation(AnimationType::kSsRunRight)
: goku.GetMutableSprite().PlayAnimation(AnimationType::kRunRight);
}
if (input_.WasKeyPressed(SDL_SCANCODE_A) ||
input_.IsKeyHeld(SDL_SCANCODE_A)) {
goku.GetMutableLocation().x -= kGokuSpeed;
// If goku is super saiyan, he moves faster.
goku.GetMutableLocation().x -=
super_saiyan ? constants::kSsGokuSpeedMod : 0;
// If goku runs left far enough, let's wrap to the right side.
if (goku.GetMutableLocation().x < 0) {
goku.GetMutableLocation().x = goku_window_boundary;
}
super_saiyan
? goku.GetMutableSprite().PlayAnimation(AnimationType::kSsRunLeft)
: goku.GetMutableSprite().PlayAnimation(AnimationType::kRunLeft);
} else {
super_saiyan
? goku.GetMutableSprite().PlayAnimation(AnimationType::kSsIdle)
: goku.GetMutableSprite().PlayAnimation(AnimationType::kDefault);
}
if ((input_.WasKeyPressed(SDL_SCANCODE_T) ||
input_.IsKeyHeld(SDL_SCANCODE_T)) &&
!super_saiyan) {
goku.GetMutableSprite().PlayAnimation(AnimationType::kSsTransformation);
super_saiyan = true;
}
if (input_.WasKeyPressed(SDL_SCANCODE_U) ||
input_.IsKeyHeld(SDL_SCANCODE_U)) {
super_saiyan = false;
}
Perf::GetPerf()->StopTimer("input");
graphics_.AddSprite(goku.GetSprite(), goku.GetLocation());
Perf::GetPerf()->StartTimer("draw");
graphics_.DrawNextFrame();
Perf::GetPerf()->StopTimer("draw");
LimitFrameRate();
Perf::GetPerf()->StopTimer("game_loop");
Perf::GetPerf()->ReportResults();
}
}
void Game::LimitFrameRate() {
int current_time_ms = SDL_GetTicks();
int elapsed_time_ms = current_time_ms - frame_start_time_ms;
if (elapsed_time_ms < constants::kMaxFrameTimeMs) {
SDL_Delay(constants::kMaxFrameTimeMs - elapsed_time_ms);
}
}
bool Game::InitSdl() {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cout << "SDL Failed to initialize. Error: " << SDL_GetError()
<< std::endl;
return false;
}
IMG_Init(IMG_INIT_PNG);
return true;
}