2016-11-27 11:53:18 +00:00
|
|
|
/*
|
|
|
|
* FarmlandsGame.cpp
|
|
|
|
*
|
|
|
|
* Created on: Nov 7, 2016
|
|
|
|
* Author: tibi
|
|
|
|
*/
|
|
|
|
|
2016-12-02 20:20:04 +00:00
|
|
|
#include <model/GameObject.h>
|
2016-11-30 17:50:01 +00:00
|
|
|
#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>
|
|
|
|
|
2016-11-30 17:50:01 +00:00
|
|
|
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),
|
2016-11-30 17:50:01 +00:00
|
|
|
m_time(0)
|
2016-11-27 11:53:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FarmlandsGame::initialize()
|
|
|
|
{
|
2016-11-30 17:50:01 +00:00
|
|
|
bool ok = true;
|
2016-11-27 11:53:18 +00:00
|
|
|
|
|
|
|
// Initialize render system
|
2016-11-30 17:50:01 +00:00
|
|
|
ok &= SdlRenderer::instance().initialize(&GameState::current().renderContext);
|
|
|
|
if (!ok)
|
2016-11-27 11:53:18 +00:00
|
|
|
return false;
|
|
|
|
|
2016-11-30 17:50:01 +00:00
|
|
|
// Initialize & load resources
|
|
|
|
ResourceManager::instance().initialize();
|
2016-12-01 19:08:28 +00:00
|
|
|
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
|
|
|
|
2016-11-30 17:50:01 +00:00
|
|
|
// Finish initialization
|
2016-12-01 19:08:28 +00:00
|
|
|
GameState::current().scene->root.onInitialize();
|
2016-12-02 18:29:40 +00:00
|
|
|
GameState::current().gameInitialized = true;
|
2016-11-27 11:53:18 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FarmlandsGame::onUpdateLogic()
|
|
|
|
{
|
2016-12-01 19:08:28 +00:00
|
|
|
GameState::current().scene->root.onUpdateLogic();
|
2016-11-30 17:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FarmlandsGame::onPreRender()
|
|
|
|
{
|
2016-12-01 19:08:28 +00:00
|
|
|
GameState::current().scene->root.onPreRender();
|
2016-11-30 17:50:01 +00:00
|
|
|
SdlRenderer::instance().renderBegin();
|
2016-11-27 11:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FarmlandsGame::onRender()
|
|
|
|
{
|
2016-12-01 19:08:28 +00:00
|
|
|
GameState::current().scene->root.onRender();
|
2016-11-27 11:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FarmlandsGame::onEvent(SDL_Event& event)
|
|
|
|
{
|
|
|
|
// Let controllers handle event
|
2016-12-01 19:08:28 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 17:50:01 +00:00
|
|
|
void FarmlandsGame::onPostRender()
|
|
|
|
{
|
|
|
|
SdlRenderer::instance().renderEnd();
|
2016-12-01 19:08:28 +00:00
|
|
|
GameState::current().scene->root.onPostRender();
|
2016-11-30 17:50:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 19:49:10 +00:00
|
|
|
void FarmlandsGame::onFrameEnded()
|
|
|
|
{
|
|
|
|
GameState::current().scene->root.onFrameEnded();
|
|
|
|
}
|
|
|
|
|
2016-11-27 11:53:18 +00:00
|
|
|
void FarmlandsGame::stop()
|
|
|
|
{
|
|
|
|
m_running = false;
|
2016-12-01 19:08:28 +00:00
|
|
|
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();
|
2016-12-09 20:33:47 +00:00
|
|
|
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();
|
2016-11-30 17:50:01 +00:00
|
|
|
onPreRender();
|
2016-11-27 11:53:18 +00:00
|
|
|
onRender();
|
2016-11-30 17:50:01 +00:00
|
|
|
onPostRender();
|
2016-12-21 19:49:10 +00:00
|
|
|
onFrameEnded();
|
2016-11-27 11:53:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|