2011-12-03 22:41:37 +01:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-12-03 22:41:37 +01:00
|
|
|
*
|
|
|
|
* 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 "application.h"
|
|
|
|
#include <framework/core/clock.h>
|
|
|
|
#include <framework/core/resourcemanager.h>
|
|
|
|
#include <framework/core/modulemanager.h>
|
|
|
|
#include <framework/core/eventdispatcher.h>
|
|
|
|
#include <framework/core/configmanager.h>
|
|
|
|
#include <framework/net/connection.h>
|
|
|
|
#include <framework/platform/platformwindow.h>
|
|
|
|
#include <framework/ui/uimanager.h>
|
|
|
|
#include <framework/ui/uiwidget.h>
|
|
|
|
#include <framework/graphics/graphics.h>
|
2011-12-15 19:20:09 +01:00
|
|
|
#include <framework/graphics/particlemanager.h>
|
2011-12-07 01:31:55 +01:00
|
|
|
#include <framework/graphics/painter.h>
|
2012-04-13 21:54:08 +02:00
|
|
|
#include <framework/sound/soundmanager.h>
|
2011-12-03 22:41:37 +01:00
|
|
|
#include <framework/luascript/luainterface.h>
|
2011-12-29 19:18:12 +01:00
|
|
|
#include <framework/platform/crashhandler.h>
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2011-12-05 07:44:03 +01:00
|
|
|
Application *g_app = nullptr;
|
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
void exitSignalHandler(int sig)
|
|
|
|
{
|
|
|
|
static bool signaled = false;
|
|
|
|
switch(sig) {
|
|
|
|
case SIGTERM:
|
|
|
|
case SIGINT:
|
|
|
|
if(!signaled) {
|
|
|
|
signaled = true;
|
2012-03-14 19:45:15 +01:00
|
|
|
g_eventDispatcher.addEvent(std::bind(&Application::close, g_app));
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-12-05 07:44:03 +01:00
|
|
|
Application::Application(const std::string& appName)
|
2012-04-29 01:31:18 +02:00
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
{
|
2011-12-05 07:44:03 +01:00
|
|
|
g_app = this;
|
2011-12-03 22:41:37 +01:00
|
|
|
m_appName = appName;
|
2012-03-25 16:10:15 +02:00
|
|
|
m_frameSleep = 0;
|
2011-12-05 07:44:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Application::~Application()
|
|
|
|
{
|
|
|
|
g_app = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
void Application::init(const std::vector<std::string>& args)
|
2011-12-05 07:44:03 +01:00
|
|
|
{
|
2011-12-03 22:41:37 +01:00
|
|
|
// capture exit signals
|
|
|
|
signal(SIGTERM, exitSignalHandler);
|
|
|
|
signal(SIGINT, exitSignalHandler);
|
|
|
|
|
2012-02-05 23:42:35 +01:00
|
|
|
#ifdef CRASH_HANDLER
|
2011-12-29 19:18:12 +01:00
|
|
|
installCrashHandler();
|
|
|
|
#endif
|
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
// initialize lua
|
|
|
|
g_lua.init();
|
|
|
|
registerLuaFunctions();
|
|
|
|
|
|
|
|
// initialize resources
|
|
|
|
g_resources.init(args[0].c_str());
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
// setup configs write directory
|
|
|
|
if(!g_resources.setupWriteDir(m_appName))
|
|
|
|
logError("Could not setup write directory");
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
// load configs
|
|
|
|
if(!g_configs.load("/config.otml"))
|
|
|
|
logInfo("Using default configurations.");
|
2011-12-03 22:41:37 +01:00
|
|
|
|
|
|
|
// setup platform window
|
2012-04-13 21:54:08 +02:00
|
|
|
g_ui.init();
|
2011-12-05 07:44:03 +01:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
g_window.init();
|
|
|
|
g_window.hide();
|
2012-04-14 02:14:25 +02:00
|
|
|
g_window.setOnResize(std::bind(&Application::resize, this, std::placeholders::_1));
|
|
|
|
g_window.setOnInputEvent(std::bind(&Application::inputEvent, this, std::placeholders::_1));
|
2012-04-13 21:54:08 +02:00
|
|
|
g_window.setOnClose(std::bind(&Application::close, this));
|
2011-12-05 07:44:03 +01:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
// initialize graphics
|
|
|
|
g_graphics.init();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
// initialize sound
|
|
|
|
g_sounds.init();
|
2011-12-05 07:44:03 +01:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
// fire first resize
|
|
|
|
resize(g_window.getSize());
|
|
|
|
|
|
|
|
// display window when the application starts running
|
|
|
|
//g_eventDispatcher.addEvent([]{ g_window.show(); });
|
2011-12-05 19:27:07 +01:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
g_modules.discoverModulesPath();
|
2012-03-22 17:07:23 +01:00
|
|
|
|
|
|
|
m_initialized = true;
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::terminate()
|
|
|
|
{
|
2012-03-22 17:07:23 +01:00
|
|
|
if(!m_initialized)
|
|
|
|
return;
|
|
|
|
|
2012-02-05 23:42:35 +01:00
|
|
|
g_lua.callGlobalField("g_app", "onTerminate");
|
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
// hide the window because there is no render anymore
|
2012-04-13 21:54:08 +02:00
|
|
|
g_window.hide();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
|
|
|
// run modules unload events
|
2012-04-13 21:54:08 +02:00
|
|
|
g_modules.unloadModules();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
|
|
|
// release remaining lua object references
|
|
|
|
g_lua.collectGarbage();
|
|
|
|
|
|
|
|
// poll remaining events
|
|
|
|
poll();
|
|
|
|
|
|
|
|
// terminate network
|
|
|
|
Connection::terminate();
|
|
|
|
|
2011-12-05 07:44:03 +01:00
|
|
|
// terminate graphics
|
2012-04-13 21:54:08 +02:00
|
|
|
g_ui.terminate();
|
|
|
|
g_window.terminate();
|
|
|
|
|
|
|
|
// terminate sound
|
|
|
|
g_sounds.terminate();
|
2011-12-05 07:44:03 +01:00
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
// flush remaining dispatcher events
|
2012-03-14 19:45:15 +01:00
|
|
|
g_eventDispatcher.flush();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
|
|
|
// save configurations
|
2012-04-13 21:54:08 +02:00
|
|
|
g_configs.save();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
|
|
|
// release resources
|
|
|
|
g_resources.terminate();
|
|
|
|
|
|
|
|
// terminate script environment
|
|
|
|
g_lua.terminate();
|
|
|
|
|
|
|
|
logInfo("Application ended successfully.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::run()
|
|
|
|
{
|
2012-03-22 17:07:23 +01:00
|
|
|
if(!m_initialized)
|
|
|
|
return;
|
|
|
|
|
2012-03-27 20:14:35 +02:00
|
|
|
//ticks_t lastPollTicks = g_clock.updateTicks();
|
2011-12-03 22:41:37 +01:00
|
|
|
m_stopping = false;
|
|
|
|
m_running = true;
|
|
|
|
|
|
|
|
// run the first poll
|
|
|
|
poll();
|
|
|
|
|
2012-02-20 03:27:08 +01:00
|
|
|
g_lua.callGlobalField("g_app", "onRun");
|
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
while(!m_stopping) {
|
2012-03-28 13:46:15 +02:00
|
|
|
// only update the current time once per frame to gain performance
|
2011-12-03 22:41:37 +01:00
|
|
|
g_clock.updateTicks();
|
|
|
|
|
2012-03-28 13:46:15 +02:00
|
|
|
// poll all events before rendering
|
|
|
|
poll();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
if(g_window.isVisible()) {
|
2011-12-03 22:41:37 +01:00
|
|
|
g_graphics.beginRender();
|
|
|
|
render();
|
|
|
|
g_graphics.endRender();
|
|
|
|
|
|
|
|
// update screen pixels
|
|
|
|
g_window.swapBuffers();
|
|
|
|
} else {
|
|
|
|
// sleeps until next poll to avoid massive cpu usage
|
|
|
|
g_clock.sleep(POLL_CYCLE_DELAY+1);
|
|
|
|
}
|
2012-03-25 16:10:15 +02:00
|
|
|
|
|
|
|
if(m_frameSleep > 0)
|
|
|
|
g_clock.sleep(m_frameSleep);
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_stopping = false;
|
|
|
|
m_running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::exit()
|
|
|
|
{
|
|
|
|
logInfo("Exiting application..");
|
|
|
|
m_stopping = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::poll()
|
|
|
|
{
|
2012-04-14 11:53:32 +02:00
|
|
|
g_sounds.poll();
|
|
|
|
|
2012-04-13 21:54:08 +02:00
|
|
|
// poll input events
|
|
|
|
g_window.poll();
|
|
|
|
//g_particleManager.update();
|
2011-12-03 22:41:37 +01:00
|
|
|
|
|
|
|
Connection::poll();
|
2012-03-28 13:46:15 +02:00
|
|
|
g_eventDispatcher.poll();
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
|
2012-01-07 06:35:50 +01:00
|
|
|
void Application::close()
|
|
|
|
{
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = true;
|
2012-03-18 14:34:39 +01:00
|
|
|
if(!g_lua.callGlobalField<bool>("g_app", "onClose"))
|
2012-01-07 06:35:50 +01:00
|
|
|
exit();
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = false;
|
2012-01-07 06:35:50 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 22:41:37 +01:00
|
|
|
void Application::render()
|
|
|
|
{
|
2012-03-28 13:46:15 +02:00
|
|
|
// everything is rendered by UI components, even the game
|
2011-12-03 22:41:37 +01:00
|
|
|
g_ui.render();
|
2011-12-07 20:58:09 +01:00
|
|
|
|
2012-03-28 13:46:15 +02:00
|
|
|
//g_particleManager.render();
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::resize(const Size& size)
|
|
|
|
{
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = true;
|
2011-12-07 01:31:55 +01:00
|
|
|
g_graphics.resize(size);
|
2011-12-25 00:14:12 +01:00
|
|
|
g_ui.resize(size);
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = false;
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Application::inputEvent(const InputEvent& event)
|
|
|
|
{
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = true;
|
2011-12-03 22:41:37 +01:00
|
|
|
g_ui.inputEvent(event);
|
2012-05-14 23:36:54 +02:00
|
|
|
m_onInputEvent = false;
|
2011-12-03 22:41:37 +01:00
|
|
|
}
|