reorganize some stuff

This commit is contained in:
Eduardo Bart
2012-01-04 23:28:29 -02:00
parent a92af44eb6
commit 30ce5e2ba9
69 changed files with 143 additions and 131 deletions

View File

@@ -90,7 +90,7 @@ void Application::init(const std::vector<std::string>& args, int appFlags)
logError("Could not setup write directory");
// load configs
if(!g_configs.load("config.otml"))
if(!g_configs.load("/config.otml"))
logInfo("Using default configurations.");
}

View File

@@ -50,7 +50,7 @@ void Module::discover(const OTMLNodePtr& moduleNode)
if(OTMLNodePtr node = moduleNode->get("onLoad")) {
g_lua.loadFunction(node->value(), "@" + node->source() + "[" + node->tag() + "]");
g_lua.useValue();
m_loadCallback = g_lua.polymorphicPop<BooleanCallback>();
m_loadCallback = g_lua.polymorphicPop<SimpleCallback>();
}
// set onUnload callback
@@ -79,10 +79,8 @@ bool Module::load()
}
}
if(m_loadCallback && !m_loadCallback()) {
logError("Unable to load module '", m_name, "' because its onLoad event returned false");
return false;
}
if(m_loadCallback)
m_loadCallback();
logInfo("Loaded module '", m_name, "'");
m_loaded = true;

View File

@@ -55,7 +55,7 @@ private:
std::string m_author;
std::string m_website;
std::string m_version;
BooleanCallback m_loadCallback;
SimpleCallback m_loadCallback;
SimpleCallback m_unloadCallback;
std::list<std::string> m_dependencies;
};

View File

@@ -67,7 +67,7 @@ bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFron
void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const std::string& packageExt, bool append)
{
auto files = listDirectoryFiles(resolvePath(packagesDir));
auto files = listDirectoryFiles(checkPath(packagesDir));
for(const std::string& file : files) {
if(boost::ends_with(file, packageExt))
addToSearchPath(packagesDir + "/" + file, !append);
@@ -76,17 +76,17 @@ void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const
bool ResourceManager::fileExists(const std::string& fileName)
{
return (PHYSFS_exists(resolvePath(fileName).c_str()) && !PHYSFS_isDirectory(resolvePath(fileName).c_str()));
return (PHYSFS_exists(checkPath(fileName).c_str()) && !PHYSFS_isDirectory(checkPath(fileName).c_str()));
}
bool ResourceManager::directoryExists(const std::string& directoryName)
{
return (PHYSFS_isDirectory(resolvePath(directoryName).c_str()));
return (PHYSFS_isDirectory(checkPath(directoryName).c_str()));
}
void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
{
std::string fullPath = resolvePath(fileName);
std::string fullPath = checkPath(fileName);
out.clear(std::ios::goodbit);
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
if(!file) {
@@ -114,7 +114,7 @@ std::string ResourceManager::loadFile(const std::string& fileName)
bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, uint size)
{
PHYSFS_file* file = PHYSFS_openWrite(resolvePath(fileName).c_str());
PHYSFS_file* file = PHYSFS_openWrite(checkPath(fileName).c_str());
if(!file)
return false;
@@ -143,13 +143,13 @@ bool ResourceManager::saveFile(const std::string& fileName, const std::string& d
bool ResourceManager::deleteFile(const std::string& fileName)
{
return PHYSFS_delete(resolvePath(fileName).c_str()) != 0;
return PHYSFS_delete(checkPath(fileName).c_str()) != 0;
}
std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& directoryPath)
{
std::list<std::string> files;
auto rc = PHYSFS_enumerateFiles(resolvePath(directoryPath).c_str());
auto rc = PHYSFS_enumerateFiles(checkPath(directoryPath).c_str());
for(int i = 0; rc[i] != NULL; i++)
files.push_back(rc[i]);
@@ -158,8 +158,9 @@ std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& di
return files;
}
std::string ResourceManager::resolvePath(const std::string& path)
std::string ResourceManager::checkPath(const std::string& path)
{
/*
std::string fullPath;
if(boost::starts_with(path, "/"))
fullPath = path;
@@ -169,7 +170,10 @@ std::string ResourceManager::resolvePath(const std::string& path)
fullPath += scriptPath + "/";
fullPath += path;
}
return fullPath;
*/
if(!(boost::starts_with(path, "/")))
logTraceWarning("the following file path is not fully resolved: ", path);
return path;
}
std::string ResourceManager::getBaseDir()

View File

@@ -50,7 +50,7 @@ public:
std::list<std::string> listDirectoryFiles(const std::string& directoryPath = "");
std::string resolvePath(const std::string& path);
std::string checkPath(const std::string& path);
std::string getBaseDir();
};

View File

@@ -32,11 +32,11 @@
// additional utilities
#include "util/types.h"
#include "util/tools.h"
#include "util/point.h"
#include "util/color.h"
#include "util/rect.h"
#include "util/size.h"
#include "util/matrix.h"
#include "math/point.h"
#include "math/color.h"
#include "math/rect.h"
#include "math/size.h"
#include "math/matrix.h"
// logger
#include "core/logger.h"

View File

@@ -28,7 +28,8 @@
void Font::load(const OTMLNodePtr& fontNode)
{
std::string textureName = fontNode->valueAt("texture");
OTMLNodePtr textureNode = fontNode->at("texture");
std::string textureFile = Fw::resolvePath(textureNode->value(), textureNode->source());
Size glyphSize = fontNode->valueAt<Size>("glyph-size");
m_glyphHeight = fontNode->valueAt<int>("height");
m_yOffset = fontNode->valueAt("y-offset", 0);
@@ -36,7 +37,7 @@ void Font::load(const OTMLNodePtr& fontNode)
m_glyphSpacing = fontNode->valueAt("spacing", Size(0,0));
// load font texture
m_texture = g_textures.getTexture(textureName);
m_texture = g_textures.getTexture(textureFile);
if(OTMLNodePtr node = fontNode->get("fixed-glyph-width")) {
for(int glyph = m_firstGlyph; glyph < 256; ++glyph)

View File

@@ -29,7 +29,6 @@
class ParticleAffector {
public:
ParticleAffector();
virtual ~ParticleAffector() { dump << "ParticleAffector deleted"; }
void update(double elapsedTime);
virtual bool load(const OTMLNodePtr& node);

View File

@@ -33,7 +33,6 @@ class ParticleEmitter {
public:
ParticleEmitter(const ParticleSystemPtr& parent);
~ParticleEmitter() { dump << "ParticleEmitter deleted"; }
bool load(const OTMLNodePtr& node);

View File

@@ -30,7 +30,6 @@
class ParticleSystem : public std::enable_shared_from_this<ParticleSystem> {
public:
ParticleSystem();
~ParticleSystem() { dump << "ParticleSystem deleted"; }
bool load(const OTMLNodePtr& node);

View File

@@ -37,9 +37,6 @@ void Application::registerLuaFunctions()
g_lua.registerClass<UIWidget>();
g_lua.bindClassStaticFunction<UIWidget>("create", &UIWidget::create<UIWidget>);
g_lua.bindClassMemberFunction<UIWidget>("destroy", &UIWidget::destroy);
g_lua.bindClassMemberFunction<UIWidget>("render", &UIWidget::render);
g_lua.bindClassMemberFunction<UIWidget>("renderSelf", &UIWidget::renderSelf);
g_lua.bindClassMemberFunction<UIWidget>("renderChildren", &UIWidget::renderChildren);
g_lua.bindClassMemberFunction<UIWidget>("setVisible", &UIWidget::setVisible);
g_lua.bindClassMemberFunction<UIWidget>("setEnabled", &UIWidget::setEnabled);
g_lua.bindClassMemberFunction<UIWidget>("setPressed", &UIWidget::setPressed);
@@ -237,6 +234,7 @@ void Application::registerLuaFunctions()
g_lua.bindClassStaticFunction("g_window", "hide", std::bind(&PlatformWindow::hide, &g_window));
g_lua.bindClassStaticFunction("g_window", "move", std::bind(&PlatformWindow::move, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "resize", std::bind(&PlatformWindow::resize, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setMinimumSize", std::bind(&PlatformWindow::setMinimumSize, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setVerticalSync", std::bind(&PlatformWindow::setVerticalSync, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setFullscreen", std::bind(&PlatformWindow::setFullscreen, &g_window, _1));
g_lua.bindClassStaticFunction("g_window", "setTitle", std::bind(&PlatformWindow::setTitle, &g_window, _1));
@@ -260,22 +258,14 @@ void Application::registerLuaFunctions()
g_lua.bindClassStaticFunction("g_ui", "loadUI", std::bind(&UIManager::loadUI, &g_ui, _1, _2));
g_lua.bindClassStaticFunction("g_ui", "getRootWidget", std::bind(&UIManager::getRootWidget, &g_ui));
/*
// FontManager
g_lua.registerStaticClass("g_fonts");
g_lua.bindClassStaticFunction("g_fonts", "releaseFonts", std::bind(&FontManager::releaseFonts, &g_fonts));
g_lua.bindClassStaticFunction("g_fonts", "importFont", std::bind(&FontManager::importFont, &g_fonts, _1));
g_lua.bindClassStaticFunction("g_fonts", "fontExists", std::bind(&FontManager::fontExists, &g_fonts, _1));
g_lua.bindClassStaticFunction("g_fonts", "getFont", std::bind(&FontManager::getFont, &g_fonts, _1));
g_lua.bindClassStaticFunction("g_fonts", "getDefaultFont", std::bind(&FontManager::getDefaultFont, &g_fonts));
g_lua.bindClassStaticFunction("g_fonts", "setDefaultFont", std::bind(&FontManager::setDefaultFont, &g_fonts, _1));
*/
// EventDispatcher
g_lua.registerStaticClass("g_dispatcher");
g_lua.bindClassStaticFunction("g_dispatcher", "addEvent", std::bind(&EventDispatcher::addEvent, &g_dispatcher, _1, _2));
g_lua.bindClassStaticFunction("g_dispatcher", "scheduleEvent", std::bind(&EventDispatcher::scheduleEvent, &g_dispatcher, _1, _2));
// global functions
g_lua.bindGlobalFunction("importFont", std::bind(&FontManager::importFont, &g_fonts, _1));
g_lua.bindGlobalFunction("setDefaultFont", std::bind(&FontManager::setDefaultFont, &g_fonts, _1));
}

View File

@@ -290,9 +290,14 @@ void LuaInterface::runBuffer(const std::string& buffer, const std::string& sourc
void LuaInterface::loadScript(const std::string& fileName)
{
std::string buffer = g_resources.loadFile(fileName);
std::string source = "@" + g_resources.resolvePath(fileName);
loadBuffer(g_resources.loadFile(fileName), "@" + g_resources.resolvePath(fileName));
// resolve file full path
std::string filePath = fileName;
if(!boost::starts_with(fileName, "/"))
filePath = getCurrentSourcePath() + "/" + filePath;
std::string buffer = g_resources.loadFile(filePath);
std::string source = "@" + filePath;
loadBuffer(buffer, source);
}
void LuaInterface::loadFunction(const std::string& buffer, const std::string& source)

View File

@@ -23,8 +23,8 @@
#ifndef COLOR_H
#define COLOR_H
#include "types.h"
#include "tools.h"
#include "../util/types.h"
#include "../util/tools.h"
#include "../const.h"
class Color

View File

@@ -23,7 +23,7 @@
#ifndef POINT_H
#define POINT_H
#include "types.h"
#include "../util/types.h"
#include <sstream>
#include <cmath>

View File

@@ -23,7 +23,7 @@
#ifndef RECT_H
#define RECT_H
#include "types.h"
#include "../util/types.h"
#include <sstream>
template<class T>

View File

@@ -37,7 +37,7 @@ OTMLDocumentPtr OTMLDocument::parse(const std::string& fileName)
{
std::stringstream fin;
g_resources.loadFile(fileName, fin);
return parse(fin, g_resources.resolvePath(fileName));
return parse(fin, fileName);
}
OTMLDocumentPtr OTMLDocument::parse(std::istream& in, const std::string& source)

View File

@@ -57,6 +57,7 @@ void UIManager::resize(const Size& size)
void UIManager::inputEvent(const InputEvent& event)
{
m_isOnInputEvent = true;
switch(event.type) {
case Fw::KeyPressInputEvent:
m_keyboardReceiver->onKeyPress(event.keyCode, event.keyText, event.keyboardModifiers);
@@ -78,6 +79,7 @@ void UIManager::inputEvent(const InputEvent& event)
m_mouseReceiver->onMouseWheel(event.mousePos, event.wheelDirection);
break;
};
m_isOnInputEvent = false;
}
bool UIManager::importStyle(const std::string& file)

View File

@@ -54,10 +54,13 @@ public:
UIWidgetPtr getRootWidget() { return m_rootWidget; }
bool isOnInputEvent() { return m_isOnInputEvent; }
private:
UIWidgetPtr m_rootWidget;
UIWidgetPtr m_mouseReceiver;
UIWidgetPtr m_keyboardReceiver;
bool m_isOnInputEvent;
std::map<std::string, OTMLNodePtr> m_styles;
};

View File

@@ -40,11 +40,13 @@ public:
void destroy();
protected:
virtual void render();
virtual void renderSelf();
virtual void renderChildren();
protected:
friend class UIManager;
void drawBackground(const Rect& screenCoords);
void drawBorder(const Rect& screenCoords);
void drawImage(const Rect& screenCoords);
@@ -206,7 +208,6 @@ protected:
virtual void onMouseRelease(const Point& mousePos, Fw::MouseButton button);
virtual bool onMouseMove(const Point& mousePos, const Point& mouseMoved);
virtual bool onMouseWheel(const Point& mousePos, Fw::MouseWheelDirection direction);
friend class UIManager;
protected:
std::string m_id;

View File

@@ -76,6 +76,11 @@ std::string mkstr(const T&... args) {
return buf.str();
}
template<typename... T>
void throwException(const T&... args) {
throw Exception(Fw::mkstr(args...));
}
// used by dumper
struct dump_util {
~dump_util() { std::cout << std::endl; }
@@ -270,9 +275,16 @@ std::vector<T> split(const std::string& str, const std::string& separators = " "
return results;
}
template<typename... T>
void throwException(const T&... args) {
throw Exception(Fw::mkstr(args...));
inline std::string resolvePath(const std::string& file, std::string sourcePath) {
if(boost::starts_with(file, "/"))
return file;
if(!boost::ends_with(sourcePath, "/")) {
std::size_t slashPos = sourcePath.find_last_of("/");
if(slashPos == std::string::npos)
throwException("invalid source path '", sourcePath, "' for file '", file, "'");
sourcePath = sourcePath.substr(0, slashPos + 1);
}
return sourcePath + file;
}
template<typename T>