config manager using yaml
logger improvements more documentation
This commit is contained in:
parent
f43e2bde48
commit
d121154932
|
@ -1,11 +1,16 @@
|
||||||
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
|
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
|
||||||
PROJECT(otclient)
|
PROJECT(otclient)
|
||||||
|
|
||||||
|
SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
|
||||||
|
|
||||||
# find needed packages
|
# find needed packages
|
||||||
SET(Boost_USE_STATIC_LIBS ON)
|
SET(Boost_USE_STATIC_LIBS ON)
|
||||||
FIND_PACKAGE(Boost COMPONENTS thread filesystem REQUIRED)
|
FIND_PACKAGE(Boost REQUIRED)
|
||||||
FIND_PACKAGE(OpenGL REQUIRED)
|
FIND_PACKAGE(OpenGL REQUIRED)
|
||||||
FIND_PACKAGE(Lua51 REQUIRED)
|
FIND_PACKAGE(Lua51 REQUIRED)
|
||||||
|
FIND_PACKAGE(YamlCpp REQUIRED)
|
||||||
|
FIND_PACKAGE(PhysFS REQUIRED)
|
||||||
|
FIND_PACKAGE(PNG REQUIRED)
|
||||||
|
|
||||||
# choose a default build type if not specified
|
# choose a default build type if not specified
|
||||||
IF(NOT CMAKE_BUILD_TYPE)
|
IF(NOT CMAKE_BUILD_TYPE)
|
||||||
|
@ -15,19 +20,26 @@ MESSAGE(STATUS "BUILD TYPE: " ${CMAKE_BUILD_TYPE})
|
||||||
|
|
||||||
# setup compiler options
|
# setup compiler options
|
||||||
IF(CMAKE_COMPILER_IS_GNUCXX)
|
IF(CMAKE_COMPILER_IS_GNUCXX)
|
||||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wl,--as-needed")
|
SET(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -Wno-unused-parameter")
|
||||||
SET(CMAKE_CXX_FLAGS_DEBUG "-O1 -g -ggdb -fno-inline")
|
SET(CMAKE_CXX_FLAGS_DEBUG "-O1 -g -ggdb -fno-inline")
|
||||||
SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wl,-s")
|
SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wl,-s")
|
||||||
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
|
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
|
||||||
|
SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -Wl,--as-needed")
|
||||||
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
|
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
|
||||||
|
|
||||||
INCLUDE_DIRECTORIES(
|
INCLUDE_DIRECTORIES(
|
||||||
|
${Boost_INCLUDE_DIRS}
|
||||||
${LUA_INCLUDE_DIRS}
|
${LUA_INCLUDE_DIRS}
|
||||||
${Boost_INCLUDE_DIRS})
|
${YAMLCPP_INCLUDE_DIRS}
|
||||||
|
${PHYSFS_INCLUDE_DIRS}
|
||||||
|
${PNG_INCLUDE_DIRS})
|
||||||
|
|
||||||
LINK_DIRECTORIES(
|
LINK_DIRECTORIES(
|
||||||
${Boost_LIBRARY_DIRS}
|
${Boost_LIBRARY_DIRS}
|
||||||
${LUA_LIBRARY_DIRS})
|
${LUA_LIBRARY_DIRS}
|
||||||
|
${PHYSFS_LIBRARY_DIRS}
|
||||||
|
${YAMLCPP_LIBRARY_DIRS}
|
||||||
|
${PNG_LIBRARY_DIRS})
|
||||||
|
|
||||||
# setup definitions
|
# setup definitions
|
||||||
ADD_DEFINITIONS(-D_REENTRANT)
|
ADD_DEFINITIONS(-D_REENTRANT)
|
||||||
|
@ -38,6 +50,8 @@ ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
|
||||||
# find sources
|
# find sources
|
||||||
SET(SOURCES
|
SET(SOURCES
|
||||||
|
src/configmanager.cpp
|
||||||
|
src/resourcemanager.cpp
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/engine.cpp
|
src/engine.cpp
|
||||||
src/graphics.cpp
|
src/graphics.cpp
|
||||||
|
@ -57,5 +71,7 @@ ADD_EXECUTABLE(otclient ${SOURCES})
|
||||||
TARGET_LINK_LIBRARIES(otclient
|
TARGET_LINK_LIBRARIES(otclient
|
||||||
${Boost_LIBRARIES}
|
${Boost_LIBRARIES}
|
||||||
${OPENGL_LIBRARY}
|
${OPENGL_LIBRARY}
|
||||||
${LUA51_LIBRARY})
|
${LUA51_LIBRARY}
|
||||||
|
${YAMLCPP_LIBRARY}
|
||||||
|
${PHYSFS_LIBRARY}
|
||||||
|
${PNG_LIBRARY})
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
# - Try to find yaml-cpp
|
||||||
|
# Once done, this will define
|
||||||
|
#
|
||||||
|
# YAMLCPP_LIBRARY, link these to use yaml-cpp
|
||||||
|
# YAMLCPP_FOUND - system has yaml-cpp
|
||||||
|
# YAMLCPP_INCLUDE_DIR, the yaml-cpp include directories
|
||||||
|
|
||||||
|
FIND_PATH(YAMLCPP_INCLUDE_DIR yaml-cpp/yaml.h
|
||||||
|
HINTS
|
||||||
|
$ENV{PHYSFSDIR}
|
||||||
|
PATH_SUFFIXES include/yaml-cpp include
|
||||||
|
PATHS
|
||||||
|
~/Library/Frameworks
|
||||||
|
/Library/Frameworks
|
||||||
|
/usr/local
|
||||||
|
/usr
|
||||||
|
/sw # Fink
|
||||||
|
/opt/local # DarwinPorts
|
||||||
|
/opt/csw # Blastwave
|
||||||
|
/opt
|
||||||
|
)
|
||||||
|
|
||||||
|
FIND_LIBRARY(YAMLCPP_LIBRARY
|
||||||
|
NAMES yaml-cpp
|
||||||
|
HINTS
|
||||||
|
$ENV{PHYSFSDIR}
|
||||||
|
PATH_SUFFIXES lib64 lib
|
||||||
|
PATHS
|
||||||
|
~/Library/Frameworks
|
||||||
|
/Library/Frameworks
|
||||||
|
/usr/local
|
||||||
|
/usr
|
||||||
|
/sw
|
||||||
|
/opt/local
|
||||||
|
/opt/csw
|
||||||
|
/opt
|
||||||
|
)
|
||||||
|
|
||||||
|
INCLUDE("${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake")
|
||||||
|
FIND_PACKAGE_HANDLE_STANDARD_ARGS(YamlCpp DEFAULT_MSG YAMLCPP_LIBRARY YAMLCPP_INCLUDE_DIR)
|
|
@ -0,0 +1,153 @@
|
||||||
|
/* 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 "configmanager.h"
|
||||||
|
#include "logger.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
|
ConfigManager g_config;
|
||||||
|
|
||||||
|
ConfigManager::ConfigManager()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigManager::~ConfigManager()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConfigManager::load(const std::string& fileName)
|
||||||
|
{
|
||||||
|
std::ifstream fin(fileName.c_str());
|
||||||
|
if(!fin.good())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
m_fileName = fileName;
|
||||||
|
|
||||||
|
try {
|
||||||
|
YAML::Parser parser(fin);
|
||||||
|
|
||||||
|
YAML::Node doc;
|
||||||
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
|
for(YAML::Iterator it=doc.begin(); it != doc.end(); ++it) {
|
||||||
|
std::string key, value;
|
||||||
|
it.first() >> key;
|
||||||
|
it.second() >> value;
|
||||||
|
m_confsMap[key] = value;
|
||||||
|
}
|
||||||
|
} catch (YAML::ParserException& e) {
|
||||||
|
error("Malformed configuration file!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigManager::save()
|
||||||
|
{
|
||||||
|
std::ofstream fout(m_fileName.c_str());
|
||||||
|
if(!fout.good()) {
|
||||||
|
error("Failed to save configuration file %s", m_fileName.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
YAML::Emitter out;
|
||||||
|
out << m_confsMap;
|
||||||
|
|
||||||
|
fout << out.c_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigManager::setValue(const std::string &key, const std::string &value)
|
||||||
|
{
|
||||||
|
m_confsMap[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigManager::setValue(const std::string &key, const char *value)
|
||||||
|
{
|
||||||
|
m_confsMap[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigManager::setValue(const std::string &key, int value)
|
||||||
|
{
|
||||||
|
setValue(key, castToString<int>(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigManager::setValue(const std::string &key, float value)
|
||||||
|
{
|
||||||
|
setValue(key, castToString<float>(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigManager::setValue(const std::string &key, bool value)
|
||||||
|
{
|
||||||
|
if(value)
|
||||||
|
setValue(key,"true");
|
||||||
|
else
|
||||||
|
setValue(key,"false");
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string &ConfigManager::getString(const std::string &key)
|
||||||
|
{
|
||||||
|
std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
|
||||||
|
if(iter == m_confsMap.end()) {
|
||||||
|
warning("Config value %s not found", key.c_str());
|
||||||
|
static std::string emptystr;
|
||||||
|
return emptystr;
|
||||||
|
}
|
||||||
|
return iter->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
float ConfigManager::getFloat(const std::string &key)
|
||||||
|
{
|
||||||
|
std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
|
||||||
|
if(iter == m_confsMap.end()) {
|
||||||
|
warning("Config value %s not found", key.c_str());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return castFromString<float>(iter->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConfigManager::getBoolean(const std::string &key)
|
||||||
|
{
|
||||||
|
std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
|
||||||
|
if(iter == m_confsMap.end()) {
|
||||||
|
warning("Config value %s not found", key.c_str());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return (iter->second == std::string("true"));
|
||||||
|
}
|
||||||
|
|
||||||
|
int ConfigManager::getInteger(const std::string &key)
|
||||||
|
{
|
||||||
|
std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
|
||||||
|
if(iter == m_confsMap.end()) {
|
||||||
|
warning("Config value %s not found", key.c_str());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return castFromString<int>(iter->second);
|
||||||
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
/* 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 CONFIGMANAGER_H
|
||||||
|
#define CONFIGMANAGER_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
class ConfigManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ConfigManager();
|
||||||
|
~ConfigManager();
|
||||||
|
|
||||||
|
/// Read configuration file and parse all settings to memory
|
||||||
|
bool load(const std::string& fileName);
|
||||||
|
|
||||||
|
/// Dump all settings to configuration file
|
||||||
|
void save();
|
||||||
|
|
||||||
|
void setValue(const std::string &key, const std::string &value);
|
||||||
|
void setValue(const std::string &key, const char *value);
|
||||||
|
void setValue(const std::string &key, float value);
|
||||||
|
void setValue(const std::string &key, bool value);
|
||||||
|
void setValue(const std::string &key, int value);
|
||||||
|
|
||||||
|
const std::string &getString(const std::string &key);
|
||||||
|
float getFloat(const std::string &key);
|
||||||
|
bool getBoolean(const std::string &key);
|
||||||
|
int getInteger(const std::string &key);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string m_fileName;
|
||||||
|
std::map<std::string, std::string> m_confsMap;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern ConfigManager g_config;
|
||||||
|
|
||||||
|
#endif // CONFIGMANAGER_H
|
|
@ -21,6 +21,7 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef VERSION_H
|
#ifndef VERSION_H
|
||||||
#define VERSION_H
|
#define VERSION_H
|
||||||
|
|
||||||
|
|
|
@ -21,11 +21,14 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "engine.h"
|
#include "engine.h"
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include "graphics.h"
|
#include "graphics.h"
|
||||||
#include "const.h"
|
#include "const.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
#include "configmanager.h"
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
Engine g_engine;
|
Engine g_engine;
|
||||||
|
|
||||||
|
@ -44,8 +47,8 @@ void Engine::init()
|
||||||
{
|
{
|
||||||
Platform::init();
|
Platform::init();
|
||||||
|
|
||||||
int width = 640;
|
int width = g_config.getInteger("width");
|
||||||
int height = 480;
|
int height = g_config.getInteger("height");
|
||||||
|
|
||||||
// create the window
|
// create the window
|
||||||
Platform::createWindow(width, height, 550, 450);
|
Platform::createWindow(width, height, 550, 450);
|
||||||
|
@ -63,6 +66,10 @@ void Engine::init()
|
||||||
|
|
||||||
void Engine::terminate()
|
void Engine::terminate()
|
||||||
{
|
{
|
||||||
|
// save configs
|
||||||
|
g_config.setValue("width", Platform::getWindowWidth());
|
||||||
|
g_config.setValue("height", Platform::getWindowHeight());
|
||||||
|
|
||||||
Platform::showMouseCursor();
|
Platform::showMouseCursor();
|
||||||
Platform::terminate();
|
Platform::terminate();
|
||||||
g_graphics.terminate();
|
g_graphics.terminate();
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef ENGINE_H
|
#ifndef ENGINE_H
|
||||||
#define ENGINE_H
|
#define ENGINE_H
|
||||||
|
|
||||||
|
@ -35,20 +36,24 @@ public:
|
||||||
void init();
|
void init();
|
||||||
void terminate();
|
void terminate();
|
||||||
|
|
||||||
|
/// Main loop
|
||||||
void run();
|
void run();
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
bool isRunning() const { return m_running; }
|
bool isRunning() const { return m_running; }
|
||||||
bool isStopping() const { return m_stopping; }
|
bool isStopping() const { return m_stopping; }
|
||||||
unsigned long getLastFrameTicks() const { return m_lastFrameTicks; }
|
|
||||||
|
|
||||||
// events fired by platform
|
/// Fired by platform on window close
|
||||||
void onClose();
|
void onClose();
|
||||||
|
/// Fired by platform on window resize
|
||||||
void onResize(int width, int height);
|
void onResize(int width, int height);
|
||||||
|
/// Fired by platform on mouse/keyboard input
|
||||||
void onInputEvent(InputEvent *event);
|
void onInputEvent(InputEvent *event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/// Called to render every frame
|
||||||
void render();
|
void render();
|
||||||
|
/// Called between renders
|
||||||
void update(int elapsedTicks);
|
void update(int elapsedTicks);
|
||||||
|
|
||||||
bool m_stopping;
|
bool m_stopping;
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "graphics.h"
|
#include "graphics.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef GRAPHICS_H
|
#ifndef GRAPHICS_H
|
||||||
#define GRAPHICS_H
|
#define GRAPHICS_H
|
||||||
|
|
||||||
|
@ -33,9 +34,13 @@ public:
|
||||||
void init();
|
void init();
|
||||||
void terminate();
|
void terminate();
|
||||||
|
|
||||||
|
///Called after every window resize
|
||||||
void resize(int width, int height);
|
void resize(int width, int height);
|
||||||
|
|
||||||
|
///Called before every render
|
||||||
void beginRender();
|
void beginRender();
|
||||||
|
|
||||||
|
///Called after every render
|
||||||
void endRender();
|
void endRender();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef INPUT_H
|
#ifndef INPUT_H
|
||||||
#define INPUT_H
|
#define INPUT_H
|
||||||
|
|
||||||
|
|
|
@ -21,19 +21,17 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
|
|
||||||
void _log(int level, const char *trace, const char *format, ...)
|
void Logger::log(int level, const char *trace, const char *format, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
std::stringstream out;
|
|
||||||
std::string strace;
|
std::string strace;
|
||||||
|
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
@ -51,25 +49,22 @@ void _log(int level, const char *trace, const char *format, ...)
|
||||||
static char const *colors[] = { "\033[01;31m ", "\033[01;31m", "\033[01;33m", "\033[0;32m", "\033[01;34m" };
|
static char const *colors[] = { "\033[01;31m ", "\033[01;31m", "\033[01;33m", "\033[0;32m", "\033[01;34m" };
|
||||||
static bool colored = getenv("COLORED_OUTPUT");
|
static bool colored = getenv("COLORED_OUTPUT");
|
||||||
if(colored)
|
if(colored)
|
||||||
out << colors[level];
|
std::cout << colors[level];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(!strace.empty())
|
if(!strace.empty())
|
||||||
out << "[" << strace << "] ";
|
std::cout << "[" << strace << "] ";
|
||||||
|
|
||||||
static char const *prefixes[] = { "FATAL ERROR: ", "ERROR: ", "WARNING: ", "", "", "" };
|
static char const *prefixes[] = { "FATAL ERROR: ", "ERROR: ", "WARNING: ", "", "", "" };
|
||||||
out << prefixes[level];
|
std::cout << prefixes[level];
|
||||||
out << text;
|
std::cout << text;
|
||||||
|
|
||||||
#ifdef linux
|
#ifdef linux
|
||||||
if(colored)
|
if(colored)
|
||||||
out << "\033[0m";
|
std::cout << "\033[0m";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(level <= LWARNING)
|
std::cout << std::endl;
|
||||||
std::cerr << out.str() << std::endl;
|
|
||||||
else
|
|
||||||
std::cout << out.str() << std::endl;
|
|
||||||
|
|
||||||
if(level == LFATAL)
|
if(level == LFATAL)
|
||||||
exit(-1);
|
exit(-1);
|
||||||
|
|
34
src/logger.h
34
src/logger.h
|
@ -21,11 +21,16 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef LOGGER_H
|
#ifndef LOGGER_H
|
||||||
#define LOGGER_H
|
#define LOGGER_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
namespace Logger {
|
||||||
|
|
||||||
enum ELogLevel {
|
enum ELogLevel {
|
||||||
LFATAL = 0,
|
LFATAL = 0,
|
||||||
LERROR,
|
LERROR,
|
||||||
|
@ -34,15 +39,28 @@ enum ELogLevel {
|
||||||
LDEBUG
|
LDEBUG
|
||||||
};
|
};
|
||||||
|
|
||||||
void _log(int level, const char *trace, const char *format, ...);
|
void log(int level, const char *trace, const char *format, ...);
|
||||||
|
|
||||||
#define fatal(...) _log(LFATAL, NULL, __VA_ARGS__)
|
}
|
||||||
#define error(...) _log(LERROR, NULL, __VA_ARGS__)
|
|
||||||
#define warning(...) _log(LWARNING, NULL, __VA_ARGS__)
|
|
||||||
#define debug(...) _log(LDEBUG, NULL, __VA_ARGS__)
|
|
||||||
#define notice(...) _log(LNOTICE, NULL, __VA_ARGS__)
|
|
||||||
|
|
||||||
#define trace() _log(LDEBUG, __PRETTY_FUNCTION__, "")
|
#define fatal(...) Logger::log(Logger::LFATAL, NULL, __VA_ARGS__)
|
||||||
#define tdebug(...) _log(LDEBUG, __PRETTY_FUNCTION__, __VA_ARGS__)
|
#define error(...) Logger::log(Logger::LERROR, NULL, __VA_ARGS__)
|
||||||
|
#define warning(...) Logger::log(Logger::LWARNING, NULL, __VA_ARGS__)
|
||||||
|
#define debug(...) Logger::log(Logger::LDEBUG, NULL, __VA_ARGS__)
|
||||||
|
#define notice(...) Logger::log(Logger::LNOTICE, NULL, __VA_ARGS__)
|
||||||
|
|
||||||
|
#define trace() Logger::log(Logger::LDEBUG, __PRETTY_FUNCTION__, "")
|
||||||
|
#define tdebug(...) Logger::log(Logger::LDEBUG, __PRETTY_FUNCTION__, __VA_ARGS__)
|
||||||
|
|
||||||
|
struct Dump {
|
||||||
|
public:
|
||||||
|
Dump() { }
|
||||||
|
~Dump() { debug(m_buf.str().c_str()); }
|
||||||
|
template<class T> Dump &operator<<(const T &x) { m_buf << x << " "; return *this; }
|
||||||
|
private:
|
||||||
|
std::ostringstream m_buf;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define dump() Dump()
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
16
src/main.cpp
16
src/main.cpp
|
@ -21,11 +21,14 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "engine.h"
|
#include "engine.h"
|
||||||
#include "const.h"
|
#include "const.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
|
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
|
#include "configmanager.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
// catches terminate signals to exit nicely
|
// catches terminate signals to exit nicely
|
||||||
void signal_handler(int sig)
|
void signal_handler(int sig)
|
||||||
|
@ -45,6 +48,13 @@ void signal_handler(int sig)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Default otclient configurations
|
||||||
|
void setDefaultConfigs()
|
||||||
|
{
|
||||||
|
g_config.setValue("width", 640);
|
||||||
|
g_config.setValue("height", 480);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, const char *argv[])
|
int main(int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
// install our signal handler
|
// install our signal handler
|
||||||
|
@ -52,11 +62,17 @@ int main(int argc, const char *argv[])
|
||||||
signal(SIGINT, signal_handler);
|
signal(SIGINT, signal_handler);
|
||||||
signal(SIGQUIT, signal_handler);
|
signal(SIGQUIT, signal_handler);
|
||||||
|
|
||||||
|
setDefaultConfigs();
|
||||||
|
if(!g_config.load("config.yml"))
|
||||||
|
notice("Could not read configuration file, default configurations will be used.");
|
||||||
|
|
||||||
notice(APP_LONGNAME);
|
notice(APP_LONGNAME);
|
||||||
|
|
||||||
// setup the engine and run
|
// setup the engine and run
|
||||||
g_engine.init();
|
g_engine.init();
|
||||||
g_engine.run();
|
g_engine.run();
|
||||||
g_engine.terminate();
|
g_engine.terminate();
|
||||||
|
|
||||||
|
g_config.save();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,17 +21,22 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef PLATFORM_H
|
#ifndef PLATFORM_H
|
||||||
#define PLATFORM_H
|
#define PLATFORM_H
|
||||||
|
|
||||||
|
// namespace with platform specific stuff
|
||||||
namespace Platform
|
namespace Platform
|
||||||
{
|
{
|
||||||
void init();
|
void init();
|
||||||
void terminate();
|
void terminate();
|
||||||
|
|
||||||
|
/// Poll platform input/window events
|
||||||
void poll();
|
void poll();
|
||||||
|
|
||||||
|
/// Get current time in milliseconds since first frame render
|
||||||
unsigned long getTicks();
|
unsigned long getTicks();
|
||||||
|
/// Sleep in current thread
|
||||||
void sleep(unsigned long miliseconds);
|
void sleep(unsigned long miliseconds);
|
||||||
|
|
||||||
bool createWindow(int width, int height, int minWidth, int minHeight);
|
bool createWindow(int width, int height, int minWidth, int minHeight);
|
||||||
|
@ -40,8 +45,12 @@ namespace Platform
|
||||||
void setWindowTitle(const char *title);
|
void setWindowTitle(const char *title);
|
||||||
bool isWindowFocused();
|
bool isWindowFocused();
|
||||||
bool isWindowVisible();
|
bool isWindowVisible();
|
||||||
|
int getWindowWidth();
|
||||||
|
int getWindowHeight();
|
||||||
|
|
||||||
|
/// Get GL extension function address
|
||||||
void *getExtensionProcAddress(const char *ext);
|
void *getExtensionProcAddress(const char *ext);
|
||||||
|
/// Check if GL extension is supported
|
||||||
bool isExtensionSupported(const char *ext);
|
bool isExtensionSupported(const char *ext);
|
||||||
|
|
||||||
const char *getTextFromClipboard();
|
const char *getTextFromClipboard();
|
||||||
|
@ -50,8 +59,10 @@ namespace Platform
|
||||||
void hideMouseCursor();
|
void hideMouseCursor();
|
||||||
void showMouseCursor();
|
void showMouseCursor();
|
||||||
|
|
||||||
|
/// Enable/disable vertical synchronization
|
||||||
void setVsync(bool enable = true);
|
void setVsync(bool enable = true);
|
||||||
|
/// Swap GL buffers
|
||||||
void swapBuffers();
|
void swapBuffers();
|
||||||
};
|
}
|
||||||
|
|
||||||
#endif // PLATFORM_H
|
#endif // PLATFORM_H
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
/* 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 "resourcemanager.h"
|
||||||
|
|
||||||
|
ResourceManager g_resources;
|
||||||
|
|
||||||
|
ResourceManager::ResourceManager()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourceManager::~ResourceManager()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
/* 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 RESOURCEMANAGER_H
|
||||||
|
#define RESOURCEMANAGER_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
class ResourceManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ResourceManager();
|
||||||
|
~ResourceManager();
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
|
||||||
|
extern ResourceManager g_resources;
|
||||||
|
|
||||||
|
#endif // RESOURCEMANAGER_H
|
|
@ -21,6 +21,7 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
21
src/util.h
21
src/util.h
|
@ -21,13 +21,34 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef UTIL_H
|
#ifndef UTIL_H
|
||||||
#define UTIL_H
|
#define UTIL_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
|
|
||||||
|
/// Formatting like printf for std::string, va_list input version
|
||||||
std::string vformat(const char *format, va_list args);
|
std::string vformat(const char *format, va_list args);
|
||||||
|
|
||||||
|
/// Formatting like printf for std::string
|
||||||
std::string format(const char *format, ...);
|
std::string format(const char *format, ...);
|
||||||
|
|
||||||
|
/// Convert int/float like types to std::string
|
||||||
|
template<typename T>
|
||||||
|
inline std::string castToString(const T& x) {
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << x;
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline T castFromString(const std::string& s) {
|
||||||
|
std::istringstream ss(s);
|
||||||
|
T x = 0;
|
||||||
|
ss >> x;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -21,6 +21,7 @@
|
||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include "engine.h"
|
#include "engine.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
@ -708,3 +709,13 @@ bool Platform::isWindowVisible()
|
||||||
{
|
{
|
||||||
return x11.visible;
|
return x11.visible;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Platform::getWindowWidth()
|
||||||
|
{
|
||||||
|
return x11.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Platform::getWindowHeight()
|
||||||
|
{
|
||||||
|
return x11.height;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue