tibia-client/src/framework/engine.cpp

152 lines
3.8 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.
*/
#include "engine.h"
2011-03-26 21:53:08 +01:00
#include "fonts.h"
#include "platform.h"
#include "graphics.h"
#include "input.h"
2011-04-06 21:46:58 +02:00
#include "configs.h"
2010-11-23 16:30:43 +01:00
#include "gamestate.h"
2011-04-08 07:10:00 +02:00
#include "dispatcher.h"
2011-04-07 10:42:57 +02:00
#include "net/connections.h"
2011-04-08 07:10:00 +02:00
#include "ui/uicontainer.h"
Engine g_engine;
void Engine::init()
{
2011-04-06 21:46:58 +02:00
// initialize stuff
g_graphics.init();
2011-04-06 21:46:58 +02:00
g_fonts.init();
}
void Engine::terminate()
{
2011-04-06 21:46:58 +02:00
// force last state exit
changeState(NULL);
2011-04-06 21:46:58 +02:00
// terminate stuff
g_fonts.terminate();
g_graphics.terminate();
}
void Engine::run()
{
2011-04-07 10:38:29 +02:00
int ticks = Platform::getTicks();
int lastFpsTicks = ticks;
int frameCount = 0;
int fps = 0;
m_running = true;
while(!m_stopping) {
2011-04-08 07:10:00 +02:00
// poll platform events
Platform::poll();
2011-04-07 22:36:40 +02:00
// poll network events
2011-04-07 20:02:06 +02:00
g_connections.poll();
ticks = Platform::getTicks();
2011-04-08 07:10:00 +02:00
// poll diaptcher tasks
g_dispatcher.poll(ticks);
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++;
if(ticks - lastFpsTicks >= 1000) {
lastFpsTicks = ticks;
fps = frameCount;
frameCount = 0;
}
}
2011-03-20 23:30:15 +01:00
render();
2011-04-07 10:38:29 +02:00
// render fps
2011-04-07 17:41:49 +02:00
if(m_calculateFps) {
2011-04-08 11:50:26 +02:00
std::string fpsText = format("FPS: %d", fps);
Size textSize = g_defaultFont->calculateTextRectSize(fpsText);
g_defaultFont->renderText(fpsText, Point(g_graphics.getScreenSize().width() - textSize.width() - 10, 10));
2011-04-07 10:38:29 +02:00
}
2011-04-07 22:36:40 +02:00
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;
}
2010-11-23 16:30:43 +01:00
void Engine::changeState(GameState* newState)
{
if(m_currentState)
m_currentState->onLeave();
m_currentState = newState;
if(m_currentState)
m_currentState->onEnter();
2010-11-23 16:30:43 +01:00
}
void Engine::render()
{
g_graphics.beginRender();
2010-11-23 16:30:43 +01:00
if(m_currentState)
m_currentState->render();
2011-04-09 01:20:44 +02:00
g_ui->render();
g_graphics.endRender();
}
void Engine::onClose()
{
2010-11-23 16:30:43 +01:00
if(m_currentState)
m_currentState->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-04-09 19:18:50 +02:00
g_ui->setSize(size);
2011-04-08 11:50:26 +02:00
if(m_currentState)
m_currentState->onResize(size);
}
void Engine::onInputEvent(const InputEvent& event)
{
2011-04-08 07:10:00 +02:00
// inputs goest to gui first
2011-04-09 01:20:44 +02:00
if(!g_ui->onInputEvent(event)) {
2011-04-08 07:10:00 +02:00
// if gui didnt capture the input then goes to the state
if(m_currentState)
m_currentState->onInputEvent(event);
}
}