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.
|
|
|
|
*/
|
|
|
|
|
2010-11-21 22:48:58 +01:00
|
|
|
|
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>
|
2010-11-18 23:13:35 +01:00
|
|
|
|
|
|
|
Engine g_engine;
|
|
|
|
|
|
|
|
void Engine::init()
|
|
|
|
{
|
2011-04-06 21:46:58 +02:00
|
|
|
// initialize stuff
|
2010-11-18 23:13:35 +01:00
|
|
|
g_graphics.init();
|
2011-04-10 22:40:44 +02:00
|
|
|
g_fonts.init("tibia-12px-rounded");
|
2010-11-18 23:13:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::terminate()
|
|
|
|
{
|
2011-04-06 21:46:58 +02:00
|
|
|
// terminate stuff
|
|
|
|
g_fonts.terminate();
|
2010-11-18 23:13:35 +01:00
|
|
|
g_graphics.terminate();
|
|
|
|
}
|
|
|
|
|
2011-04-22 15:49:46 +02:00
|
|
|
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();
|
2011-04-22 15:49:46 +02:00
|
|
|
}
|
|
|
|
|
2010-11-18 23:13:35 +01:00
|
|
|
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-04-10 22:40:44 +02:00
|
|
|
Font *defaultFont = g_fonts.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;
|
2010-11-18 23:13:35 +01:00
|
|
|
m_running = true;
|
|
|
|
|
|
|
|
while(!m_stopping) {
|
2011-04-16 18:08:55 +02:00
|
|
|
m_lastFrameTicks = Platform::getTicks();
|
|
|
|
|
2011-04-22 15:49:46 +02:00
|
|
|
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()) {
|
|
|
|
// calculate and 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-04-22 20:48:02 +02:00
|
|
|
fpsText = f("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();
|
2010-11-18 23:13:35 +01:00
|
|
|
|
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
|
|
|
}
|
2010-11-18 23:13:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_stopping = false;
|
|
|
|
m_running = false;
|
2011-05-01 20:47:35 +02:00
|
|
|
|
|
|
|
// destroy root ui
|
|
|
|
rootContainer->destroy();
|
|
|
|
|
|
|
|
// poll remaning ui events
|
|
|
|
g_engine.poll();
|
2010-11-18 23:13:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::stop()
|
|
|
|
{
|
|
|
|
m_stopping = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::onClose()
|
|
|
|
{
|
2011-04-22 15:49:46 +02:00
|
|
|
if(m_onCloseCallback)
|
|
|
|
g_dispatcher.addTask(m_onCloseCallback);
|
2010-11-18 23:13:35 +01:00
|
|
|
}
|
|
|
|
|
2011-04-08 07:10:00 +02:00
|
|
|
void Engine::onResize(const Size& size)
|
2010-11-18 23:13:35 +01:00
|
|
|
{
|
2011-04-08 07:10:00 +02:00
|
|
|
g_graphics.resize(size);
|
2011-05-01 20:47:35 +02:00
|
|
|
UIContainer::getRoot()->setSize(size);
|
2010-11-18 23:13:35 +01:00
|
|
|
}
|
|
|
|
|
2011-04-09 21:07:35 +02:00
|
|
|
void Engine::onInputEvent(const InputEvent& event)
|
2010-11-18 23:13:35 +01:00
|
|
|
{
|
2011-05-01 20:47:35 +02:00
|
|
|
UIContainer::getRoot()->onInputEvent(event);
|
2010-11-18 23:13:35 +01:00
|
|
|
}
|