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

153 lines
4.0 KiB
C++
Raw Normal View History

2010-11-19 16:55:55 +01:00
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2011-04-17 21:14:24 +02:00
#include <prerequisites.h>
#include <core/engine.h>
#include <core/platform.h>
#include <core/dispatcher.h>
#include <graphics/graphics.h>
#include <graphics/fonts.h>
#include <ui/uicontainer.h>
2011-04-20 08:40:31 +02:00
#include <net/connection.h>
2011-05-03 00:48:41 +02:00
#include <script/luascript.h>
2011-05-12 00:16:11 +02:00
#include <ui/uiskins.h>
2011-05-13 01:24:57 +02:00
#include <graphics/textures.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();
}
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)
logFatal("FATAL ERROR: no ui loaded at all, no reason to continue running");
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-05-12 00:16:11 +02:00
fpsText = fmt("FPS: %d", 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-05-03 00:48:41 +02:00
g_dispatcher.addTask(boost::bind(&LuaScript::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);
}