1
0
Fork 0
spectre/source/Game.cpp

124 lines
2 KiB
C++

#include <iostream>
#include <Spectre/Game/GameTime.h>
#include <Spectre/Game/FPSCounter.h>
#include <Spectre/Input/InputModule.h>
#include <Spectre/System/MessageHandler.h>
#include <Spectre/Game.h>
#include <Platform/PlatformMisc.h>
// TODO: move this to Application::create()
#ifdef _WIN32
#include <Platform/Win32/Win32Application.h>
typedef sp::Win32Application ApplicationType;
#elif defined(__linux__) || defined(unix) || defined(__unix) || defined(__unix__)
#include <Platform/Unix/UnixApplication.h>
typedef sp::UnixApplication ApplicationType;
#else
#error "No Application implementation exists"
#endif
namespace sp {
Game::Game() :
m_running(false)
{
m_platform = new ApplicationType();
m_graphics = new Graphics(m_platform);
m_input = new InputModule(&m_platform->getInput());
m_messageHandler = new MessageHandler();
}
Game::~Game()
{
delete m_input;
delete m_graphics;
delete m_platform;
delete m_messageHandler;
}
void Game::run()
{
if (!m_graphics->init()) {
return;
}
init();
gameLoop();
}
void Game::gameLoop()
{
GameTime time;
time.setTimeStep(120);
m_running = true;
while(m_running) {
processEvents();
// Update input.
getInput()->update();
if (time.beginFrame()) {
while(time.tick()) {
update(time.getTimeStep());
}
render();
m_fpsCounter.addFrame();
}
}
}
void Game::processEvents()
{
MessageQueue& queue = m_platform->getMessageQueue();
Event event;
while(queue.pollEvent(event)) {
if (event.type == Event::Quit) {
exit();
break;
}
// Call message handler.
if (event.type == Event::Size) {
m_messageHandler->onSizeChanged(event.size.display, event.size.width, event.size.height);
}
m_messageHandler->onEvent(event);
}
}
void Game::exit()
{
m_running = false;
}
Graphics* Game::getGraphics() const
{
return m_graphics;
}
InputModule* Game::getInput() const
{
return m_input;
}
FPSCounter& Game::getFpsCounter()
{
return m_fpsCounter;
}
MessageHandler* Game::getMessageHandler() const
{
return m_messageHandler;
}
} // namespace sp