mirror of
https://github.com/MoleskiCoder/EightBit.git
synced 2025-02-04 06:30:41 +00:00
Refactor the "Game" class a little to allow future refactoring.
Signed-off-by: Adrian Conlon <adrian.conlon@gmail.com>
This commit is contained in:
parent
17edcee715
commit
1663c7caf3
@ -38,6 +38,12 @@ namespace Gaming {
|
||||
|
||||
virtual std::string title() const = 0;
|
||||
|
||||
virtual void handleEvents();
|
||||
virtual void update();
|
||||
virtual void draw();
|
||||
virtual bool maybeSynchronise(); // true, if manual synchronisation required
|
||||
virtual void synchronise();
|
||||
|
||||
virtual void runRasterLines() {};
|
||||
virtual void runVerticalBlank() {}
|
||||
|
||||
|
@ -58,6 +58,9 @@ void Game::raisePOWER() {
|
||||
|
||||
configureBackground();
|
||||
createBitmapTexture();
|
||||
|
||||
m_frames = 0UL;
|
||||
m_startTicks = ::SDL_GetTicks();
|
||||
}
|
||||
|
||||
void Game::configureBackground() const {
|
||||
@ -73,11 +76,20 @@ void Game::createBitmapTexture() {
|
||||
}
|
||||
|
||||
void Game::runLoop() {
|
||||
|
||||
m_frames = 0UL;
|
||||
m_startTicks = ::SDL_GetTicks();
|
||||
|
||||
while (powered()) {
|
||||
update();
|
||||
draw();
|
||||
maybeSynchronise();
|
||||
}
|
||||
}
|
||||
|
||||
void Game::update() {
|
||||
handleEvents();
|
||||
runVerticalBlank();
|
||||
runRasterLines();
|
||||
}
|
||||
|
||||
void Game::handleEvents() {
|
||||
::SDL_Event e;
|
||||
while (::SDL_PollEvent(&e)) {
|
||||
switch (e.type) {
|
||||
@ -110,25 +122,28 @@ void Game::runLoop() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
runVerticalBlank();
|
||||
runRasterLines();
|
||||
|
||||
void Game::draw() {
|
||||
updateTexture();
|
||||
copyTexture();
|
||||
displayTexture();
|
||||
}
|
||||
|
||||
bool Game::maybeSynchronise() {
|
||||
++m_frames;
|
||||
const bool synchronising = !m_vsync;
|
||||
if (synchronising)
|
||||
synchronise();
|
||||
return synchronising;
|
||||
}
|
||||
|
||||
if (!m_vsync) {
|
||||
void Game::synchronise() {
|
||||
const auto elapsedTicks = ::SDL_GetTicks() - m_startTicks;
|
||||
const auto neededTicks = (m_frames / fps()) * 1000.0;
|
||||
const auto sleepNeeded = (int)(neededTicks - elapsedTicks);
|
||||
if (sleepNeeded > 0) {
|
||||
if (sleepNeeded > 0)
|
||||
::SDL_Delay(sleepNeeded);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Game::removeJoystick(SDL_Event& e) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user