-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlxGame.cpp
More file actions
173 lines (143 loc) · 3.9 KB
/
FlxGame.cpp
File metadata and controls
173 lines (143 loc) · 3.9 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
#include "FlxGame.h"
#include "FlxG.h"
#include "FlxState.h"
#include <stdexcept>
namespace flixel {
FlxGame::FlxGame(int gameWidth, int gameHeight, int updateFramerate, int drawFramerate, const std::string& title)
: currentState(nullptr)
, nextState(nullptr)
, updateFramerate(updateFramerate)
, drawFramerate(drawFramerate)
, width(gameWidth)
, height(gameHeight)
, running(false)
, fullscreen(false)
, windowTitle(title)
{
stepMS = 1000.0f / updateFramerate;
stepSeconds = stepMS / 1000.0f;
accumulator = stepMS;
maxAccumulation = 2000.0f / drawFramerate - 1.0f;
if (maxAccumulation < stepMS) {
maxAccumulation = stepMS;
}
FlxG::init(this, width, height);
}
FlxGame::~FlxGame() {
if (currentState) {
delete currentState;
}
FlxG::destroy();
}
void FlxGame::run() {
running = true;
Uint32 lastTime = SDL_GetTicks();
Uint32 currentTime;
float deltaTime;
while (running) {
currentTime = SDL_GetTicks();
deltaTime = (currentTime - lastTime) / 1000.0f;
lastTime = currentTime;
handleEvents();
flixel::FlxG::keys.update();
if (FlxG::fixedTimestep) {
accumulator += deltaTime * 1000.0f;
if (accumulator > maxAccumulation) {
accumulator = maxAccumulation;
}
while (accumulator >= stepMS) {
update(stepSeconds);
accumulator -= stepMS;
}
} else {
update(deltaTime);
}
draw();
}
}
void FlxGame::update(float elapsed) {
if (nextState) {
if (currentState) {
currentState->destroy();
delete currentState;
}
currentState = nextState;
nextState = nullptr;
currentState->create();
}
if (!currentState) {
return;
}
currentState->update(elapsed);
}
void FlxGame::draw() {
if (!currentState) {
return;
}
SDL_SetRenderDrawColor(FlxG::renderer, 0, 0, 0, 255);
SDL_RenderClear(FlxG::renderer);
currentState->draw();
SDL_RenderPresent(FlxG::renderer);
}
void FlxGame::switchState(FlxState* newState) {
nextState = newState;
}
void FlxGame::onResize(int newWidth, int newHeight) {
width = newWidth;
height = newHeight;
FlxG::resizeGame(width, height);
}
void FlxGame::setFullscreen(bool value) {
fullscreen = value;
FlxG::setFullscreen(value);
}
void FlxGame::setUpdateFramerate(int fps) {
updateFramerate = fps;
stepMS = 1000.0f / fps;
stepSeconds = stepMS / 1000.0f;
}
void FlxGame::setDrawFramerate(int fps) {
drawFramerate = fps;
maxAccumulation = 2000.0f / fps - 1.0f;
if (maxAccumulation < stepMS) {
maxAccumulation = stepMS;
}
}
void FlxGame::handleEvents() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
flixel::FlxG::keys.onEvent(event);
switch (event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
onResize(event.window.data1, event.window.data2);
}
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
running = false;
}
break;
}
}
}
void FlxGame::calculateElapsed() {
if (FlxG::fixedTimestep) {
FlxG::elapsed = FlxG::timeScale * stepSeconds;
} else {
FlxG::elapsed = FlxG::timeScale * (accumulator / 1000.0f);
if (FlxG::elapsed > FlxG::maxElapsed * FlxG::timeScale) {
FlxG::elapsed = FlxG::maxElapsed * FlxG::timeScale;
}
}
}
void FlxGame::setTitle(const std::string& title) {
windowTitle = title;
if (FlxG::window) {
SDL_SetWindowTitle(FlxG::window, title.c_str());
}
}
}