Rework application class and framework

Make otclient's framework flexible enough to run console apps like
servers, so this mean is possible to build otclient versions without
graphical interface and use it's framework to code servers
This commit is contained in:
Eduardo Bart
2012-07-13 22:10:24 -03:00
parent 099716fe03
commit e3298d561c
131 changed files with 1228 additions and 1016 deletions

View File

@@ -0,0 +1,143 @@
/*
* Copyright (c) 2010-2012 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 "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/luaengine/luainterface.h>
#include <framework/platform/crashhandler.h>
#ifdef FW_NET
#include <framework/net/connection.h>
#endif
void exitSignalHandler(int sig)
{
static bool signaled = false;
switch(sig) {
case SIGTERM:
case SIGINT:
if(!signaled) {
signaled = true;
g_dispatcher.addEvent(std::bind(&Application::close, &g_app));
}
break;
}
}
Application::Application()
{
m_appName = "application";
m_appCompactName = "app";
m_appVersion = "none";
m_stopping = false;
}
void Application::init(const std::vector<std::string>& args)
{
// capture exit signals
signal(SIGTERM, exitSignalHandler);
signal(SIGINT, exitSignalHandler);
#ifdef CRASH_HANDLER
installCrashHandler();
#endif
std::string startupOptions;
for(uint i=1;i<args.size();++i) {
const std::string& arg = args[i];
startupOptions += " ";
startupOptions += arg;
}
if(startupOptions.length() > 0)
g_logger.info(stdext::format("Startup options: %s", startupOptions));
m_startupOptions = startupOptions;
// initialize resources
g_resources.init(args[0].c_str());
// initialize lua
g_lua.init();
registerLuaFunctions();
}
void Application::deinit()
{
g_lua.callGlobalField("g_app", "onTerminate");
// run modules unload events
g_modules.unloadModules();
g_modules.clear();
// release remaining lua object references
g_lua.collectGarbage();
// poll remaining events
poll();
}
void Application::terminate()
{
#ifdef FW_NET
// terminate network
Connection::terminate();
#endif
// save configurations
g_configs.save();
// release resources
g_resources.terminate();
// terminate script environment
g_lua.terminate();
// flush remaining dispatcher events
g_dispatcher.flush();
m_terminated = true;
}
void Application::poll()
{
#ifdef FW_NET
Connection::poll();
#endif
g_dispatcher.poll();
}
void Application::exit()
{
g_logger.info("Exiting application..");
m_stopping = true;
}
void Application::close()
{
if(!g_lua.callGlobalField<bool>("g_app", "onClose"))
exit();
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2010-2012 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.
*/
#ifndef APPLICATION_H
#define APPLICATION_H
#include <framework/global.h>
#include <framework/core/adaptativeframecounter.h>
//@bindsingleton g_app
class Application
{
public:
Application();
virtual ~Application() {}
virtual void init(const std::vector<std::string>& args);
virtual void deinit();
virtual void terminate();
virtual void run() = 0;
virtual void poll();
virtual void exit();
virtual void close();
void setName(const std::string& name) { m_appName = name; }
void setCompactName(const std::string& compactName) { m_appCompactName = compactName; }
void setVersion(const std::string& version) { m_appVersion = version; }
bool isRunning() { return m_running; }
bool isStopping() { return m_stopping; }
bool isTerminated() { return m_terminated; }
const std::string& getName() { return m_appName; }
const std::string& getCompactName() { return m_appCompactName; }
const std::string& getVersion() { return m_appVersion; }
std::string getBuildCompiler() { return BUILD_COMPILER; }
std::string getBuildDate() { return BUILD_DATE; }
std::string getBuildRevision() { return BUILD_REVISION; }
std::string getBuildCommit() { return BUILD_COMMIT; }
std::string getBuildType() { return BUILD_TYPE; }
std::string getStartupOptions() { return m_startupOptions; }
protected:
void registerLuaFunctions();
std::string m_appName;
std::string m_appCompactName;
std::string m_appVersion;
std::string m_startupOptions;
Boolean<false> m_running;
Boolean<false> m_stopping;
Boolean<false> m_terminated;
};
#ifdef FW_GRAPHICS
#include "graphicalapplication.h"
#else
#include "consoleapplication.h"
#endif
#endif

View File

@@ -0,0 +1,60 @@
/*
* Copyright (c) 2010-2012 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 "consoleapplication.h"
#include <framework/core/clock.h>
#include <framework/luaengine/luainterface.h>
#ifdef FW_NET
#include <framework/net/connection.h>
#endif
ConsoleApplication g_app;
void ConsoleApplication::run()
{
m_running = true;
// run the first poll
poll();
// first clock update
g_clock.update();
g_lua.callGlobalField("g_app", "onRun");
while(!m_stopping) {
poll();
#ifdef FW_NET
Connection::poll();
#endif
stdext::millisleep(1);
g_clock.update();
m_frameCounter.update();
}
m_stopping = false;
m_running = false;
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2010-2012 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.
*/
#ifndef CONSOLEAPPLICATION_H
#define CONSOLEAPPLICATION_H
#include "application.h"
class ConsoleApplication : public Application
{
public:
void run();
int getFps() { return m_frameCounter.getLastFps(); }
protected:
AdaptativeFrameCounter m_frameCounter;
};
extern ConsoleApplication g_app;
#endif

View File

@@ -23,7 +23,7 @@
#ifndef EVENT_H
#define EVENT_H
#include <framework/luascript/luaobject.h>
#include <framework/luaengine/luaobject.h>
// @bindclass
class Event : public LuaObject

View File

@@ -22,7 +22,7 @@
#include "filestream.h"
#include "binarytree.h"
#include <framework/application.h>
#include <framework/core/application.h>
#include <physfs.h>
@@ -37,7 +37,7 @@ FileStream::FileStream(const std::string& name, PHYSFS_File *fileHandle, bool wr
FileStream::~FileStream()
{
assert(!g_app.isTermianted());
assert(!g_app.isTerminated());
close();
}

View File

@@ -24,7 +24,7 @@
#define FILESTREAM_H
#include "declarations.h"
#include <framework/luascript/luaobject.h>
#include <framework/luaengine/luaobject.h>
#include <framework/util/databuffer.h>
struct PHYSFS_File;

View File

@@ -0,0 +1,234 @@
/*
* Copyright (c) 2010-2012 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 "graphicalapplication.h"
#include <framework/core/clock.h>
#include <framework/core/eventdispatcher.h>
#include <framework/platform/platformwindow.h>
#include <framework/ui/uimanager.h>
#include <framework/graphics/graphics.h>
#include <framework/graphics/particlemanager.h>
#include <framework/graphics/painter.h>
#ifdef FW_SOUND
#include <framework/sound/soundmanager.h>
#endif
GraphicalApplication g_app;
void GraphicalApplication::init(const std::vector<std::string>& args)
{
Application::init(args);
// setup platform window
g_window.init();
g_window.hide();
g_window.setOnResize(std::bind(&GraphicalApplication::resize, this, std::placeholders::_1));
g_window.setOnInputEvent(std::bind(&GraphicalApplication::inputEvent, this, std::placeholders::_1));
g_window.setOnClose(std::bind(&GraphicalApplication::close, this));
// initialize ui
g_ui.init();
// initialize graphics
g_graphics.init();
// fire first resize event
resize(g_window.getSize());
#ifdef FW_SOUND
// initialize sound
g_sounds.init();
#endif
}
void GraphicalApplication::deinit()
{
// hide the window because there is no render anymore
g_window.hide();
Application::deinit();
}
void GraphicalApplication::terminate()
{
// destroy particles
g_particles.terminate();
// destroy any remaining widget
g_ui.terminate();
#ifdef FW_SOUND
// terminate sound
g_sounds.terminate();
#endif
Application::terminate();
m_terminated = false;
// terminate graphics
m_foreground = nullptr;
g_graphics.terminate();
g_window.terminate();
m_terminated = true;
}
void GraphicalApplication::run()
{
m_running = true;
// first clock update
g_clock.update();
// run the first poll
poll();
g_clock.update();
g_lua.callGlobalField("g_app", "onRun");
// show the application only after we draw some frames
g_dispatcher.scheduleEvent([] { g_window.show(); }, 10);
while(!m_stopping) {
// poll all events before rendering
poll();
if(g_window.isVisible()) {
// the otclient's screen consists of two panes
// background pane - high updated and animated pane (where the game are stuff happens)
// foreground pane - steady pane with few animated stuff (UI)
bool redraw = false;
bool updateForeground = false;
bool cacheForeground = g_graphics.canCacheBackbuffer() && m_foregroundFrameCounter.getMaxFps() != 0;
if(m_backgroundFrameCounter.shouldProcessNextFrame()) {
redraw = true;
if(m_mustRepaint || m_foregroundFrameCounter.shouldProcessNextFrame()) {
m_mustRepaint = false;
updateForeground = true;
}
}
if(redraw) {
if(cacheForeground) {
Rect viewportRect(0, 0, g_painter->getResolution());
// draw the foreground into a texture
if(updateForeground) {
m_foregroundFrameCounter.processNextFrame();
// draw foreground
g_painter->setAlphaWriting(true);
g_painter->clear(Color::alpha);
g_ui.render(Fw::ForegroundPane);
// copy the foreground to a texture
m_foreground->copyFromScreen(viewportRect);
g_painter->clear(Color::black);
g_painter->setAlphaWriting(false);
}
// draw background (animated stuff)
m_backgroundFrameCounter.processNextFrame();
g_ui.render(Fw::BackgroundPane);
// draw the foreground (steady stuff)
g_painter->setColor(Color::white);
g_painter->setOpacity(1.0);
g_painter->drawTexturedRect(viewportRect, m_foreground, viewportRect);
} else {
m_foregroundFrameCounter.processNextFrame();
m_backgroundFrameCounter.processNextFrame();
g_ui.render(Fw::BothPanes);
}
// update screen pixels
g_window.swapBuffers();
}
// only update the current time once per frame to gain performance
g_clock.update();
m_backgroundFrameCounter.update();
m_foregroundFrameCounter.update();
int sleepMicros = m_backgroundFrameCounter.getMaximumSleepMicros();
if(sleepMicros >= AdaptativeFrameCounter::MINIMUM_MICROS_SLEEP)
stdext::microsleep(sleepMicros);
} else {
// sleeps until next poll to avoid massive cpu usage
stdext::millisleep(POLL_CYCLE_DELAY+1);
g_clock.update();
}
}
m_stopping = false;
m_running = false;
}
void GraphicalApplication::poll()
{
#ifdef FW_SOUND
g_sounds.poll();
#endif
// poll window input events
g_window.poll();
g_particles.update();
Application::poll();
}
void GraphicalApplication::close()
{
m_onInputEvent = true;
Application::close();
m_onInputEvent = false;
}
void GraphicalApplication::resize(const Size& size)
{
m_onInputEvent = true;
g_graphics.resize(size);
g_ui.resize(size);
m_onInputEvent = false;
if(g_graphics.canCacheBackbuffer()) {
m_foreground = TexturePtr(new Texture(size));
m_foreground->setUpsideDown(true);
}
m_mustRepaint = true;
}
void GraphicalApplication::inputEvent(const InputEvent& event)
{
m_onInputEvent = true;
g_ui.inputEvent(event);
m_onInputEvent = false;
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2010-2012 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.
*/
#ifndef GRAPHICALAPPLICATION_H
#define GRAPHICALAPPLICATION_H
#include "application.h"
#include <framework/graphics/declarations.h>
#include <framework/core/inputevent.h>
class GraphicalApplication : public Application
{
enum {
POLL_CYCLE_DELAY = 10
};
public:
void init(const std::vector< std::string >& args);
void deinit();
void terminate();
void run();
void poll();
void close();
bool willRepaint() { return m_mustRepaint; }
void repaint() { m_mustRepaint = true; }
void setForegroundPaneMaxFps(int maxFps) { m_foregroundFrameCounter.setMaxFps(maxFps); }
void setBackgroundPaneMaxFps(int maxFps) { m_backgroundFrameCounter.setMaxFps(maxFps); }
int getForegroundPaneFps() { return m_foregroundFrameCounter.getLastFps(); }
int getBackgroundPaneFps() { return m_backgroundFrameCounter.getLastFps(); }
int getForegroundPaneMaxFps() { return m_foregroundFrameCounter.getMaxFps(); }
int getBackgroundPaneMaxFps() { return m_backgroundFrameCounter.getMaxFps(); }
bool isOnInputEvent() { return m_onInputEvent; }
protected:
void resize(const Size& size);
void inputEvent(const InputEvent& event);
private:
Boolean<false> m_onInputEvent;
Boolean<false> m_mustRepaint;
AdaptativeFrameCounter m_backgroundFrameCounter;
AdaptativeFrameCounter m_foregroundFrameCounter;
TexturePtr m_foreground;
};
extern GraphicalApplication g_app;
#endif

View File

@@ -22,7 +22,10 @@
#include "logger.h"
#include "eventdispatcher.h"
#ifdef FW_GRAPHICS
#include <framework/platform/platformwindow.h>
#endif
Logger g_logger;
@@ -61,7 +64,7 @@ void Logger::log(Fw::LogLevel level, const std::string& message)
}
if(level == Fw::LogFatal) {
#ifdef FW_WINDOW
#ifdef FW_GRAPHICS
g_window.displayFatalError(message);
#endif
ignoreLogs = true;

View File

@@ -24,7 +24,7 @@
#include "modulemanager.h"
#include <framework/otml/otml.h>
#include <framework/luascript/luainterface.h>
#include <framework/luaengine/luainterface.h>
Module::Module(const std::string& name)
{

View File

@@ -26,7 +26,7 @@
#include "declarations.h"
#include <framework/otml/declarations.h>
#include <framework/luascript/luaobject.h>
#include <framework/luaengine/luaobject.h>
// @bindclass
class Module : public LuaObject

View File

@@ -24,7 +24,7 @@
#include "resourcemanager.h"
#include <framework/otml/otml.h>
#include <framework/application.h>
#include <framework/core/application.h>
ModuleManager g_modules;

View File

@@ -23,8 +23,8 @@
#include "resourcemanager.h"
#include "filestream.h"
#include <framework/application.h>
#include <framework/luascript/luainterface.h>
#include <framework/core/application.h>
#include <framework/luaengine/luainterface.h>
#include <physfs.h>