83 lines
2 KiB
C++
83 lines
2 KiB
C++
#include "gamescenebase.hpp"
|
|
#include "inputbag.hpp"
|
|
#include "sdlmain.hpp"
|
|
#include <cassert>
|
|
#include <SDL2/SDL.h>
|
|
|
|
namespace curry {
|
|
namespace {
|
|
///---------------------------------------------------------------------
|
|
///---------------------------------------------------------------------
|
|
bool DoEvents (cloonel::InputBag& parInput, cloonel::SDLMain* parSdlMain) {
|
|
using cloonel::InputDevice_Keyboard;
|
|
|
|
SDL_Event eve;
|
|
while (SDL_PollEvent(&eve)) {
|
|
switch (eve.type) {
|
|
case SDL_KEYDOWN:
|
|
//eve.key.keysym.sym
|
|
parInput.NotifyKeyAction(InputDevice_Keyboard, eve.key.keysym.scancode, true);
|
|
break;
|
|
|
|
case SDL_KEYUP:
|
|
parInput.NotifyKeyAction(InputDevice_Keyboard, eve.key.keysym.scancode, false);
|
|
break;
|
|
|
|
case SDL_QUIT:
|
|
return true;
|
|
|
|
case SDL_WINDOWEVENT:
|
|
if (SDL_WINDOWEVENT_RESIZED == eve.window.event) {
|
|
parSdlMain->SetResolution(vec2us(static_cast<uint16_t>(eve.window.data1), static_cast<uint16_t>(eve.window.data2)));
|
|
}
|
|
else if (SDL_WINDOWEVENT_CLOSE == eve.window.event) {
|
|
return true;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
} //unnamed namespace
|
|
|
|
GameSceneBase::GameSceneBase (cloonel::SDLMain* parSdlMain) :
|
|
m_input(std::make_unique<cloonel::InputBag>()),
|
|
m_sdlmain(parSdlMain),
|
|
m_wants_to_quit(false)
|
|
{
|
|
assert(m_sdlmain);
|
|
}
|
|
|
|
GameSceneBase::~GameSceneBase() noexcept = default;
|
|
|
|
void GameSceneBase::prepare() {
|
|
m_wants_to_quit = false;
|
|
this->on_prepare();
|
|
}
|
|
|
|
void GameSceneBase::destroy() noexcept {
|
|
set_wants_to_quit();
|
|
this->on_destroy();
|
|
}
|
|
|
|
bool GameSceneBase::wants_to_quit() const {
|
|
return m_wants_to_quit;
|
|
}
|
|
|
|
void GameSceneBase::set_wants_to_quit() {
|
|
m_wants_to_quit = true;
|
|
}
|
|
|
|
void GameSceneBase::exec() {
|
|
const bool quit = DoEvents(*m_input, m_sdlmain);
|
|
m_input->KeyStateUpdateFinished();
|
|
if (quit)
|
|
set_wants_to_quit();
|
|
|
|
this->on_update();
|
|
}
|
|
|
|
cloonel::SDLMain* GameSceneBase::sdl_main() {
|
|
return m_sdlmain;
|
|
}
|
|
} //namespace curry
|