mirror of
https://github.com/MoleskiCoder/EightBit.git
synced 2024-06-27 11:29:28 +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 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 runRasterLines() {};
|
||||||
virtual void runVerticalBlank() {}
|
virtual void runVerticalBlank() {}
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,9 @@ void Game::raisePOWER() {
|
||||||
|
|
||||||
configureBackground();
|
configureBackground();
|
||||||
createBitmapTexture();
|
createBitmapTexture();
|
||||||
|
|
||||||
|
m_frames = 0UL;
|
||||||
|
m_startTicks = ::SDL_GetTicks();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::configureBackground() const {
|
void Game::configureBackground() const {
|
||||||
|
@ -73,64 +76,76 @@ void Game::createBitmapTexture() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::runLoop() {
|
void Game::runLoop() {
|
||||||
|
|
||||||
m_frames = 0UL;
|
|
||||||
m_startTicks = ::SDL_GetTicks();
|
|
||||||
|
|
||||||
while (powered()) {
|
while (powered()) {
|
||||||
::SDL_Event e;
|
update();
|
||||||
while (::SDL_PollEvent(&e)) {
|
draw();
|
||||||
switch (e.type) {
|
maybeSynchronise();
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
runVerticalBlank();
|
void Game::update() {
|
||||||
runRasterLines();
|
handleEvents();
|
||||||
|
runVerticalBlank();
|
||||||
|
runRasterLines();
|
||||||
|
}
|
||||||
|
|
||||||
updateTexture();
|
void Game::handleEvents() {
|
||||||
copyTexture();
|
::SDL_Event e;
|
||||||
displayTexture();
|
while (::SDL_PollEvent(&e)) {
|
||||||
|
switch (e.type) {
|
||||||
++m_frames;
|
case SDL_QUIT:
|
||||||
|
lowerPOWER();
|
||||||
if (!m_vsync) {
|
break;
|
||||||
const auto elapsedTicks = ::SDL_GetTicks() - m_startTicks;
|
case SDL_KEYDOWN:
|
||||||
const auto neededTicks = (m_frames / fps()) * 1000.0;
|
handleKeyDown(e.key.keysym.sym);
|
||||||
const auto sleepNeeded = (int)(neededTicks - elapsedTicks);
|
break;
|
||||||
if (sleepNeeded > 0) {
|
case SDL_KEYUP:
|
||||||
::SDL_Delay(sleepNeeded);
|
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) {
|
void Game::removeJoystick(SDL_Event& e) {
|
||||||
const auto which = e.jdevice.which;
|
const auto which = e.jdevice.which;
|
||||||
const auto found = m_gameControllers.find(which);
|
const auto found = m_gameControllers.find(which);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user