farmlands/src/controller/FarmlandsGame.cpp

141 lines
2.6 KiB
C++
Raw Normal View History

2016-11-27 11:53:18 +00:00
/*
* FarmlandsGame.cpp
*
* Created on: Nov 7, 2016
* Author: tibi
*/
#include <model/GameObject.h>
#include <controller/FarmlandsGame.h>
#include <graphics/backend/SdlRenderer.h>
2016-11-27 11:53:18 +00:00
#include <resources/Resources.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <unistd.h>
#include <iostream>
using namespace farmlands::graphics;
using namespace farmlands::graphics::backend;
using namespace farmlands::resources;
2016-11-27 11:53:18 +00:00
namespace farmlands {
namespace controller {
FarmlandsGame::FarmlandsGame() :
m_running(true),
m_time(0)
2016-11-27 11:53:18 +00:00
{
}
bool FarmlandsGame::initialize()
{
bool ok = true;
2016-11-27 11:53:18 +00:00
// Initialize render system
ok &= SdlRenderer::instance().initialize(&GameState::current().renderContext);
if (!ok)
2016-11-27 11:53:18 +00:00
return false;
// Initialize & load resources
ResourceManager::instance().initialize();
ResourceManager::instance().loadGame();
GameState::current().renderContext.viewport.pixelsPerUnitX = GameState::current().scene->cellWidth;
GameState::current().renderContext.viewport.pixelsPerUnitY = GameState::current().scene->cellHeight;
2016-11-27 11:53:18 +00:00
// Finish initialization
GameState::current().scene->root.onInitialize();
GameState::current().gameInitialized = true;
2016-11-27 11:53:18 +00:00
return true;
}
void FarmlandsGame::onUpdateLogic()
{
GameState::current().scene->root.onUpdateLogic();
}
void FarmlandsGame::onPreRender()
{
GameState::current().scene->root.onPreRender();
SdlRenderer::instance().renderBegin();
2016-11-27 11:53:18 +00:00
}
void FarmlandsGame::onRender()
{
GameState::current().scene->root.onRender();
2016-11-27 11:53:18 +00:00
}
void FarmlandsGame::onEvent(SDL_Event& event)
{
// Let controllers handle event
if (GameState::current().scene->root.onEvent(event))
2016-11-27 11:53:18 +00:00
return;
// Nobody? Handle global events
switch(event.type)
{
case SDL_QUIT:
stop();
break;
case SDL_KEYDOWN:
if (event.key.keysym.scancode == SDL_Scancode::SDL_SCANCODE_ESCAPE)
stop();
break;
case SDL_WINDOWEVENT_RESIZED:
break;
}
}
void FarmlandsGame::onPostRender()
{
SdlRenderer::instance().renderEnd();
GameState::current().scene->root.onPostRender();
}
void FarmlandsGame::onFrameEnded()
{
GameState::current().scene->root.onFrameEnded();
}
2016-11-27 11:53:18 +00:00
void FarmlandsGame::stop()
{
m_running = false;
GameState::current().scene->root.onDestroy();
2016-11-27 11:53:18 +00:00
}
int FarmlandsGame::run()
{
// Initialize game
if (!initialize())
return 1;
// Main loop
while(m_running)
{
// Update elapsed time
Uint32 now = SDL_GetTicks();
GameState::current().deltaTime = (now - m_time) * 0.001f;
2016-11-27 11:53:18 +00:00
m_time = now;
SDL_Event event;
while (SDL_PollEvent(&event))
onEvent(event);
onUpdateLogic();
onPreRender();
2016-11-27 11:53:18 +00:00
onRender();
onPostRender();
onFrameEnded();
2016-11-27 11:53:18 +00:00
}
// Cleanup
return 0;
}
}
}