tibia-client/src/framework/core/engine.cpp

129 lines
2.8 KiB
C++
Raw Normal View History

2011-07-17 02:13:53 +02:00
#include "engine.h"
#include "platform.h"
#include "dispatcher.h"
2011-04-17 21:14:24 +02:00
#include <graphics/graphics.h>
#include <graphics/fonts.h>
2011-07-17 02:13:53 +02:00
#include <graphics/textures.h>
2011-04-17 21:14:24 +02:00
#include <ui/uicontainer.h>
2011-05-12 00:16:11 +02:00
#include <ui/uiskins.h>
2011-07-17 02:13:53 +02:00
#include <script/scriptcontext.h>
#include <net/connection.h>
Engine g_engine;
void Engine::init()
{
2011-04-06 21:46:58 +02:00
// initialize stuff
g_graphics.init();
2011-05-12 00:16:11 +02:00
g_fonts.init();
g_lua.init();
}
void Engine::terminate()
{
// destroy root ui
UIContainer::getRoot()->destroy();
2011-05-12 00:16:11 +02:00
// cleanup script stuff
g_lua.terminate();
// poll remaning events
g_engine.poll();
2011-05-12 00:16:11 +02:00
// terminate stuff
g_fonts.terminate();
g_graphics.terminate();
g_dispatcher.cleanup();
}
void Engine::poll()
{
// poll platform events
Platform::poll();
// poll diaptcher tasks
g_dispatcher.poll();
2011-04-23 05:28:23 +02:00
// poll network events
Connection::poll();
}
void Engine::run()
{
2011-05-01 20:47:35 +02:00
// check if root container has elements
const UIContainerPtr& rootContainer = UIContainer::getRoot();
if(rootContainer->getChildCount() == 0)
2011-07-13 23:12:36 +02:00
fatal("FATAL ERROR: no ui loaded at all, no reason to continue running");
2011-05-01 20:47:35 +02:00
2011-04-16 18:08:55 +02:00
std::string fpsText;
Size fpsTextSize;
2011-05-12 00:16:11 +02:00
FontPtr defaultFont = g_uiSkins.getDefaultFont();
2011-04-16 18:08:55 +02:00
2011-04-15 04:13:53 +02:00
m_lastFrameTicks = Platform::getTicks();
int lastFpsTicks = m_lastFrameTicks;
2011-04-07 10:38:29 +02:00
int frameCount = 0;
int fps = 0;
m_running = true;
while(!m_stopping) {
2011-04-16 18:08:55 +02:00
m_lastFrameTicks = Platform::getTicks();
poll();
2011-04-08 07:10:00 +02:00
2011-03-20 23:30:15 +01:00
// render only when visible
2011-04-07 10:38:29 +02:00
if(Platform::isWindowVisible()) {
2011-05-12 00:16:11 +02:00
// calculate fps
2011-04-07 17:41:49 +02:00
if(m_calculateFps) {
2011-04-07 10:38:29 +02:00
frameCount++;
2011-04-15 04:13:53 +02:00
if(m_lastFrameTicks - lastFpsTicks >= 1000) {
lastFpsTicks = m_lastFrameTicks;
2011-04-07 10:38:29 +02:00
fps = frameCount;
frameCount = 0;
2011-04-16 18:08:55 +02:00
// update fps text
2011-07-13 23:12:36 +02:00
fpsText = make_string("FPS: ", fps);
2011-04-16 18:08:55 +02:00
fpsTextSize = defaultFont->calculateTextRectSize(fpsText);
2011-04-07 10:38:29 +02:00
}
}
2011-04-17 22:39:03 +02:00
// render
g_graphics.beginRender();
2011-05-01 20:47:35 +02:00
rootContainer->render();
2011-04-07 10:38:29 +02:00
// render fps
2011-04-16 18:08:55 +02:00
if(m_calculateFps)
defaultFont->renderText(fpsText, Point(g_graphics.getScreenSize().width() - fpsTextSize.width() - 10, 10));
2011-04-07 22:36:40 +02:00
2011-04-17 22:39:03 +02:00
g_graphics.endRender();
2011-03-20 23:30:15 +01:00
// swap buffers
Platform::swapBuffers();
2011-04-07 10:38:29 +02:00
}
}
m_stopping = false;
m_running = false;
}
void Engine::stop()
{
m_stopping = true;
}
void Engine::onClose()
{
2011-07-17 02:13:53 +02:00
g_dispatcher.addTask(boost::bind(&ScriptContext::callModuleField, &g_lua, "App", "onClose"));
}
2011-04-08 07:10:00 +02:00
void Engine::onResize(const Size& size)
{
2011-04-08 07:10:00 +02:00
g_graphics.resize(size);
2011-05-01 20:47:35 +02:00
UIContainer::getRoot()->setSize(size);
}
void Engine::onInputEvent(const InputEvent& event)
{
2011-05-01 20:47:35 +02:00
UIContainer::getRoot()->onInputEvent(event);
}