refactoring some stuff

This commit is contained in:
Eduardo Bart 2011-04-06 16:46:58 -03:00
parent a5d475aec1
commit 12cdefff38
24 changed files with 280 additions and 162 deletions

View File

@ -55,12 +55,13 @@ SET(SOURCES
# framework sources
src/framework/framebuffer.cpp
src/framework/font.cpp
src/framework/fonts.cpp
src/framework/textureloader.cpp
src/framework/texture.cpp
src/framework/texturemanager.cpp
src/framework/configmanager.cpp
src/framework/resourcemanager.cpp
src/framework/textures.cpp
src/framework/configs.cpp
src/framework/resources.cpp
src/framework/engine.cpp
src/framework/graphics.cpp
src/framework/logger.cpp

View File

@ -22,24 +22,14 @@
*/
#include "configmanager.h"
#include "resourcemanager.h"
#include "configs.h"
#include "resources.h"
#include <yaml-cpp/yaml.h>
ConfigManager g_config;
Configs g_config;
ConfigManager::ConfigManager()
{
}
ConfigManager::~ConfigManager()
{
}
bool ConfigManager::load(const std::string& fileName)
bool Configs::load(const std::string& fileName)
{
m_fileName = fileName;
@ -72,7 +62,7 @@ bool ConfigManager::load(const std::string& fileName)
return true;
}
void ConfigManager::save()
void Configs::save()
{
if(!m_fileName.empty()) {
YAML::Emitter out;
@ -81,27 +71,27 @@ void ConfigManager::save()
}
}
void ConfigManager::setValue(const std::string &key, const std::string &value)
void Configs::setValue(const std::string &key, const std::string &value)
{
m_confsMap[key] = value;
}
void ConfigManager::setValue(const std::string &key, const char *value)
void Configs::setValue(const std::string &key, const char *value)
{
m_confsMap[key] = value;
}
void ConfigManager::setValue(const std::string &key, int value)
void Configs::setValue(const std::string &key, int value)
{
setValue(key, convertType<std::string, int>(value));
}
void ConfigManager::setValue(const std::string &key, float value)
void Configs::setValue(const std::string &key, float value)
{
setValue(key, convertType<std::string, float>(value));
}
void ConfigManager::setValue(const std::string &key, bool value)
void Configs::setValue(const std::string &key, bool value)
{
if(value)
setValue(key,"true");
@ -109,7 +99,7 @@ void ConfigManager::setValue(const std::string &key, bool value)
setValue(key,"false");
}
const std::string &ConfigManager::getString(const std::string &key)
const std::string &Configs::getString(const std::string &key)
{
auto iter = m_confsMap.find(key);
if(iter == m_confsMap.end()) {
@ -120,7 +110,7 @@ const std::string &ConfigManager::getString(const std::string &key)
return iter->second;
}
float ConfigManager::getFloat(const std::string &key)
float Configs::getFloat(const std::string &key)
{
auto iter = m_confsMap.find(key);
if(iter == m_confsMap.end()) {
@ -130,7 +120,7 @@ float ConfigManager::getFloat(const std::string &key)
return convertType<float, std::string>(iter->second);
}
bool ConfigManager::getBoolean(const std::string &key)
bool Configs::getBoolean(const std::string &key)
{
auto iter = m_confsMap.find(key);
if(iter == m_confsMap.end()) {
@ -140,7 +130,7 @@ bool ConfigManager::getBoolean(const std::string &key)
return (iter->second == "true");
}
int ConfigManager::getInteger(const std::string &key)
int Configs::getInteger(const std::string &key)
{
auto iter = m_confsMap.find(key);
if(iter == m_confsMap.end()) {

View File

@ -22,16 +22,15 @@
*/
#ifndef CONFIGMANAGER_H
#define CONFIGMANAGER_H
#ifndef CONFIGS_H
#define CONFIGS_H
#include "prerequisites.h"
class ConfigManager
class Configs
{
public:
ConfigManager();
~ConfigManager();
Configs() { }
/// Read configuration file and parse all settings to memory
bool load(const std::string& fileName);
@ -55,6 +54,6 @@ private:
std::map<std::string, std::string> m_confsMap;
};
extern ConfigManager g_config;
extern Configs g_config;
#endif // CONFIGMANAGER_H
#endif // CONFIGS_H

View File

@ -27,29 +27,16 @@
#include "platform.h"
#include "graphics.h"
#include "input.h"
#include "configmanager.h"
#include "configs.h"
#include "gamestate.h"
Engine g_engine;
Engine::Engine() :
m_stopping(false),
m_running(false),
m_currentState(NULL)
{
}
Engine::~Engine()
{
}
void Engine::init()
{
// initialize graphics stuff
// initialize stuff
g_graphics.init();
// load fonts
g_fonts.load();
g_fonts.init();
// finally show the window
onResize(Platform::getWindowWidth(), Platform::getWindowHeight());
@ -57,8 +44,11 @@ void Engine::init()
void Engine::terminate()
{
// force last state exit
changeState(NULL);
// terminate stuff
g_fonts.terminate();
g_graphics.terminate();
}

View File

@ -34,8 +34,9 @@ class GameState;
class Engine
{
public:
Engine();
~Engine();
Engine() : m_stopping(false),
m_running(false),
m_currentState(NULL) { }
void init();
void terminate();

30
src/framework/font.cpp Normal file
View File

@ -0,0 +1,30 @@
/* 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 "font.h"
bool Font::load(const std::string& file)
{
return false;
}

47
src/framework/font.h Normal file
View File

@ -0,0 +1,47 @@
/* 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.
*/
#ifndef FONT_H
#define FONT_H
#include "prerequisites.h"
class Font
{
public:
Font() { }
virtual ~Font() { }
/// Load font from file
bool load(const std::string &file);
std::string& getName() { return m_name; }
private:
std::string m_name;
};
typedef std::shared_ptr<Font> FontPtr;
#endif // FONT_H

View File

@ -1,22 +1,49 @@
/* 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 "fonts.h"
#include "resourcemanager.h"
#include "font.h"
#include "resources.h"
Fonts g_fonts;
Fonts::Fonts()
{
}
Fonts::~Fonts()
{
}
bool Fonts::load()
void Fonts::init()
{
std::list<std::string> files = g_resources.getDirectoryFiles("fonts");
for(auto it = files.begin(); it != files.end(); ++it) {
notice("File: %s", (*it).c_str());
foreach(const std::string& file, files) {
if(boost::ends_with(file, ".yml")) {
std::shared_ptr<Font> font(new Font);
font->load(file);
m_fonts[font->getName()] = font;
}
}
}
return true;
Font* Fonts::get(const std::string& fontName)
{
auto it = m_fonts.find(fontName);
if(it != m_fonts.end())
return it->second.get();
return NULL;
}

View File

@ -1,25 +1,49 @@
/* 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.
*/
#ifndef FONTS_H
#define FONTS_H
#include "prerequisites.h"
#include "rect.h"
struct Font
{
Rect textureArea[256];
};
#include "font.h"
class Fonts
{
public:
Fonts();
~Fonts();
Fonts() { }
bool load();
/// Initialize all fonts
void init();
/// Get a font by name
Font *get(const std::string& fontName);
/// Terminate all fonts
void terminate() { }
private:
std::map<int, Font*> mFonts;
std::map<std::string, std::shared_ptr<Font> > m_fonts;
};
extern Fonts g_fonts;

View File

@ -32,16 +32,6 @@
Graphics g_graphics;
Graphics::Graphics()
{
}
Graphics::~Graphics()
{
}
void Graphics::init()
{
// setup opengl

View File

@ -35,8 +35,7 @@ class Texture;
class Graphics
{
public:
Graphics();
~Graphics();
Graphics() { }
void init();
void terminate();

View File

@ -23,6 +23,10 @@
#include "logger.h"
#include "util.h"
#include <iostream>
#include <stdarg.h>
void Logger::log(int level, const char *trace, const char *format, ...)
{

View File

@ -25,7 +25,7 @@
#ifndef LOGGER_H
#define LOGGER_H
#include "prerequisites.h"
#include <sstream>
namespace Logger {

View File

@ -42,7 +42,6 @@ typedef int8_t int8;
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdarg>
#include <cassert>
#include <ctime>
#include <cmath>
@ -63,6 +62,8 @@ typedef int8_t int8;
// boost utilities
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
// internal logger
#include "logger.h"

View File

@ -22,33 +22,23 @@
*/
#include "resourcemanager.h"
#include "resources.h"
#include <physfs.h>
ResourceManager g_resources;
Resources g_resources;
ResourceManager::ResourceManager()
{
}
ResourceManager::~ResourceManager()
{
}
void ResourceManager::init(const char *argv0)
void Resources::init(const char *argv0)
{
PHYSFS_init(argv0);
}
void ResourceManager::terminate()
void Resources::terminate()
{
PHYSFS_deinit();
}
bool ResourceManager::setWriteDir(const std::string& path)
bool Resources::setWriteDir(const std::string& path)
{
bool ret = (bool)PHYSFS_setWriteDir(path.c_str());
@ -57,7 +47,7 @@ bool ResourceManager::setWriteDir(const std::string& path)
return ret;
}
bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFront /*= true*/)
bool Resources::addToSearchPath(const std::string& path, bool insertInFront /*= true*/)
{
if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1)) {
error("Error while adding \"%s\" to resources search path: %s", path.c_str(), PHYSFS_getLastError());
@ -66,12 +56,12 @@ bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFron
return true;
}
bool ResourceManager::fileExists(const std::string& filePath)
bool Resources::fileExists(const std::string& filePath)
{
return PHYSFS_exists(filePath.c_str());
}
unsigned char *ResourceManager::loadFile(const std::string& fileName, unsigned int *fileSize)
unsigned char *Resources::loadFile(const std::string& fileName, unsigned int *fileSize)
{
PHYSFS_file *file = PHYSFS_openRead(fileName.c_str());
if(!file) {
@ -88,7 +78,7 @@ unsigned char *ResourceManager::loadFile(const std::string& fileName, unsigned i
return buffer;
}
std::string ResourceManager::loadTextFile(const std::string& fileName)
std::string Resources::loadTextFile(const std::string& fileName)
{
std::string text;
unsigned int fileSize;
@ -100,7 +90,7 @@ std::string ResourceManager::loadTextFile(const std::string& fileName)
return text;
}
bool ResourceManager::saveFile(const std::string &fileName, const unsigned char *data, unsigned int size)
bool Resources::saveFile(const std::string &fileName, const unsigned char *data, unsigned int size)
{
PHYSFS_file *file = PHYSFS_openWrite(fileName.c_str());
if(!file) {
@ -113,12 +103,12 @@ bool ResourceManager::saveFile(const std::string &fileName, const unsigned char
return true;
}
bool ResourceManager::saveTextFile(const std::string &fileName, std::string text)
bool Resources::saveTextFile(const std::string &fileName, std::string text)
{
return saveFile(fileName, (const unsigned char*)text.c_str(), text.size());
}
std::list<std::string> ResourceManager::getDirectoryFiles(const std::string& directory)
std::list<std::string> Resources::getDirectoryFiles(const std::string& directory)
{
std::list<std::string> files;
char **rc = PHYSFS_enumerateFiles(directory.c_str());

View File

@ -22,16 +22,15 @@
*/
#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H
#ifndef RESOURCES_H
#define RESOURCES_H
#include "prerequisites.h"
class ResourceManager
class Resources
{
public:
ResourceManager();
~ResourceManager();
Resources() { }
void init(const char *argv0);
void terminate();
@ -68,6 +67,6 @@ public:
std::list<std::string> getDirectoryFiles(const std::string& directory);
};
extern ResourceManager g_resources;
extern Resources g_resources;
#endif // RESOURCEMANAGER_H
#endif // RESOURCES_H

View File

@ -28,7 +28,7 @@
#include "prerequisites.h"
#include "size.h"
class TextureManager;
class Textures;
class Texture
{

View File

@ -22,23 +22,13 @@
*/
#include "texturemanager.h"
#include "resourcemanager.h"
#include "textures.h"
#include "resources.h"
#include "textureloader.h"
TextureManager g_textures;
Textures g_textures;
TextureManager::TextureManager()
{
}
TextureManager::~TextureManager()
{
m_texturesMap.clear();
}
TexturePtr TextureManager::get(const std::string& textureFile)
TexturePtr Textures::get(const std::string& textureFile)
{
TexturePtr texture;

48
src/framework/textures.h Normal file
View File

@ -0,0 +1,48 @@
/* 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.
*/
#ifndef TEXTURES_H
#define TEXTURES_H
#include "prerequisites.h"
#include "texture.h"
typedef std::weak_ptr<Texture> TextureWeakPtr;
class Textures
{
public:
Textures() { }
/// Load a texture from file, if it was already loaded it will be retrieved from cache
TexturePtr get(const std::string& textureFile);
private:
typedef std::map<std::string, TextureWeakPtr > TexturesMap;
TexturesMap m_texturesMap;
};
extern Textures g_textures;
#endif // TEXTURES_H

View File

@ -24,6 +24,8 @@
#include "util.h"
#include <stdarg.h>
std::string vformat(const char *format, va_list args)
{
if(!format)

View File

@ -25,7 +25,8 @@
#ifndef UTIL_H
#define UTIL_H
#include "prerequisites.h"
#include "logger.h"
#include <boost/lexical_cast.hpp>
/// Formatting like printf for std::string, va_list input version
std::string vformat(const char *format, va_list args);
@ -34,20 +35,16 @@ std::string vformat(const char *format, va_list args);
std::string format(const char *format, ...);
/// Convert any data type through boost::lexical_cast
//TODO: move declatation to util.cpp
template<class R, class T>
R convertType(T t)
{
R r = R();
R ret = R();
try {
r = boost::lexical_cast<R>(t);
ret = boost::lexical_cast<R>(t);
} catch(boost::bad_lexical_cast bad) {
error("Error converting type: %s", bad.what());
}
catch(boost::bad_lexical_cast bad){
//TODO: add an error message
}
return r;
return ret;
}
#endif

View File

@ -23,8 +23,8 @@
#include "framework/engine.h"
#include "framework/configmanager.h"
#include "framework/resourcemanager.h"
#include "framework/configs.h"
#include "framework/resources.h"
#include "framework/platform.h"
#include "menustate.h"
@ -108,7 +108,7 @@ int main(int argc, const char *argv[])
// state scope
{
std::unique_ptr<MenuState> menuState(new MenuState());
std::shared_ptr<MenuState> menuState(new MenuState());
g_engine.changeState(menuState.get());
Platform::showWindow();

View File

@ -25,23 +25,13 @@
#include "menustate.h"
#include "framework/framebuffer.h"
#include "framework/graphics.h"
#include "framework/texturemanager.h"
#include "framework/textures.h"
#include "framework/logger.h"
#include "framework/engine.h"
#include "framework/rect.h"
TexturePtr background;
MenuState::MenuState()
{
}
MenuState::~MenuState()
{
}
void MenuState::onEnter()
{
m_background = g_textures.get("background.png");

View File

@ -32,8 +32,7 @@ class MenuState : public GameState
{
public:
MenuState();
virtual ~MenuState();
MenuState() { }
virtual void onEnter();
virtual void onLeave();