You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.8 KiB

13 years ago
#include "engine.h"
#include "platform.h"
#include "dispatcher.h"
13 years ago
#include <graphics/graphics.h>
#include <graphics/fonts.h>
13 years ago
#include <graphics/textures.h>
13 years ago
#include <ui/uicontainer.h>
13 years ago
#include <ui/uiskins.h>
13 years ago
#include <script/scriptcontext.h>
#include <net/connection.h>
Engine g_engine;
void Engine::init()
{
// initialize stuff
g_graphics.init();
13 years ago
g_fonts.init();
g_lua.init();
}
void Engine::terminate()
{
// destroy root ui
UIContainer::getRoot()->destroy();
13 years ago
// cleanup script stuff
g_lua.terminate();
// poll remaning events
g_engine.poll();
13 years ago
// 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();
// poll network events
Connection::poll();
}
void Engine::run()
{
// check if root container has elements
const UIContainerPtr& rootContainer = UIContainer::getRoot();
if(rootContainer->getChildCount() == 0)
fatal("FATAL ERROR: no ui loaded at all, no reason to continue running");
std::string fpsText;
Size fpsTextSize;
13 years ago
FontPtr defaultFont = g_uiSkins.getDefaultFont();
m_lastFrameTicks = Platform::getTicks();
int lastFpsTicks = m_lastFrameTicks;
13 years ago
int frameCount = 0;
int fps = 0;
m_running = true;
while(!m_stopping) {
m_lastFrameTicks = Platform::getTicks();
poll();
13 years ago
// render only when visible
13 years ago
if(Platform::isWindowVisible()) {
13 years ago
// calculate fps
13 years ago
if(m_calculateFps) {
13 years ago
frameCount++;
if(m_lastFrameTicks - lastFpsTicks >= 1000) {
lastFpsTicks = m_lastFrameTicks;
13 years ago
fps = frameCount;
frameCount = 0;
// update fps text
fpsText = make_string("FPS: ", fps);
fpsTextSize = defaultFont->calculateTextRectSize(fpsText);
13 years ago
}
}
13 years ago
// render
g_graphics.beginRender();
rootContainer->render();
13 years ago
// render fps
if(m_calculateFps)
defaultFont->renderText(fpsText, Point(g_graphics.getScreenSize().width() - fpsTextSize.width() - 10, 10));
13 years ago
g_graphics.endRender();
// swap buffers
Platform::swapBuffers();
13 years ago
}
}
m_stopping = false;
m_running = false;
}
void Engine::stop()
{
m_stopping = true;
}
void Engine::onClose()
{
13 years ago
g_dispatcher.addTask(boost::bind(&ScriptContext::callModuleField, &g_lua, "App", "onClose"));
}
13 years ago
void Engine::onResize(const Size& size)
{
13 years ago
g_graphics.resize(size);
UIContainer::getRoot()->setSize(size);
}
void Engine::onInputEvent(const InputEvent& event)
{
UIContainer::getRoot()->onInputEvent(event);
}