Refactor the "Game" class a little to allow future refactoring.

Signed-off-by: Adrian Conlon <adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2020-05-03 20:55:58 +01:00
parent 17edcee715
commit 1663c7caf3
2 changed files with 72 additions and 51 deletions

View File

@ -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() {}

View File

@ -58,6 +58,9 @@ void Game::raisePOWER() {
configureBackground();
createBitmapTexture();
m_frames = 0UL;
m_startTicks = ::SDL_GetTicks();
}
void Game::configureBackground() const {
@ -73,64 +76,76 @@ void Game::createBitmapTexture() {
}
void Game::runLoop() {
m_frames = 0UL;
m_startTicks = ::SDL_GetTicks();
while (powered()) {
::SDL_Event e;
while (::SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
lowerPOWER();
break;
case SDL_KEYDOWN:
handleKeyDown(e.key.keysym.sym);
break;
case SDL_KEYUP:
handleKeyUp(e.key.keysym.sym);
break;
case SDL_JOYBUTTONDOWN:
handleJoyButtonDown(e.jbutton);
break;
case SDL_JOYBUTTONUP:
handleJoyButtonUp(e.jbutton);
break;
case SDL_CONTROLLERBUTTONDOWN:
handleControllerButtonDown(e.cbutton);
break;
case SDL_CONTROLLERBUTTONUP:
handleControllerButtonUp(e.cbutton);
break;
case SDL_JOYDEVICEADDED:
addJoystick(e);
break;
case SDL_JOYDEVICEREMOVED:
removeJoystick(e);
break;
}
}
update();
draw();
maybeSynchronise();
}
}
runVerticalBlank();
runRasterLines();
void Game::update() {
handleEvents();
runVerticalBlank();
runRasterLines();
}
updateTexture();
copyTexture();
displayTexture();
++m_frames;
if (!m_vsync) {
const auto elapsedTicks = ::SDL_GetTicks() - m_startTicks;
const auto neededTicks = (m_frames / fps()) * 1000.0;
const auto sleepNeeded = (int)(neededTicks - elapsedTicks);
if (sleepNeeded > 0) {
::SDL_Delay(sleepNeeded);
}
void Game::handleEvents() {
::SDL_Event e;
while (::SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
lowerPOWER();
break;
case SDL_KEYDOWN:
handleKeyDown(e.key.keysym.sym);
break;
case SDL_KEYUP:
handleKeyUp(e.key.keysym.sym);
break;
case SDL_JOYBUTTONDOWN:
handleJoyButtonDown(e.jbutton);
break;
case SDL_JOYBUTTONUP:
handleJoyButtonUp(e.jbutton);
break;
case SDL_CONTROLLERBUTTONDOWN:
handleControllerButtonDown(e.cbutton);
break;
case SDL_CONTROLLERBUTTONUP:
handleControllerButtonUp(e.cbutton);
break;
case SDL_JOYDEVICEADDED:
addJoystick(e);
break;
case SDL_JOYDEVICEREMOVED:
removeJoystick(e);
break;
}
}
}
void Game::draw() {
updateTexture();
copyTexture();
displayTexture();
}
bool Game::maybeSynchronise() {
++m_frames;
const bool synchronising = !m_vsync;
if (synchronising)
synchronise();
return synchronising;
}
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)
::SDL_Delay(sleepNeeded);
}
void Game::removeJoystick(SDL_Event& e) {
const auto which = e.jdevice.which;
const auto found = m_gameControllers.find(which);