tibia-client/src/framework/engine.cpp

130 lines
3.1 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"
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();
2011-03-26 21:53:08 +01:00
// finally show the window
2011-03-20 20:10:40 +01:00
onResize(Platform::getWindowWidth(), Platform::getWindowHeight());
}
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()
{
unsigned long ticks;
static unsigned long lastFrameTicks;
m_running = true;
lastFrameTicks = Platform::getTicks();
while(!m_stopping) {
// fire platform events
Platform::poll();
// update
ticks = Platform::getTicks();
2011-03-21 00:15:17 +01:00
update(ticks, ticks - lastFrameTicks);
lastFrameTicks = ticks;
2011-03-20 23:30:15 +01:00
// render only when visible
2011-04-02 19:32:54 +02:00
//if(Platform::isWindowVisible()) {
2011-03-20 23:30:15 +01:00
render();
2011-03-20 23:30:15 +01:00
// swap buffers
Platform::swapBuffers();
2011-04-02 19:32:54 +02:00
//}
}
lastFrameTicks = 0;
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();
g_graphics.endRender();
}
2011-03-21 00:15:17 +01:00
void Engine::update(int ticks, int elapsedTicks)
{
2010-11-23 16:30:43 +01:00
if(m_currentState)
2011-03-21 00:15:17 +01:00
m_currentState->update(ticks, elapsedTicks);
}
void Engine::onClose()
{
2010-11-23 16:30:43 +01:00
if(m_currentState)
m_currentState->onClose();
}
void Engine::onResize(int width, int height)
{
g_graphics.resize(width, height);
}
void Engine::onInputEvent(InputEvent *event)
{
2010-11-23 16:30:43 +01:00
if(m_currentState)
m_currentState->onInputEvent(event);
}