Browse Source

config manager using yaml

logger improvements
more documentation
Eduardo Bart 10 years ago
parent
commit
d121154932
19 changed files with 483 additions and 41 deletions
  1. 31
    15
      CMakeLists.txt
  2. 40
    0
      cmake/FindYamlCpp.cmake
  3. 153
    0
      src/configmanager.cpp
  4. 61
    0
      src/configmanager.h
  5. 1
    0
      src/const.h
  6. 9
    2
      src/engine.cpp
  7. 7
    2
      src/engine.h
  8. 1
    0
      src/graphics.cpp
  9. 5
    0
      src/graphics.h
  10. 1
    0
      src/input.h
  11. 8
    13
      src/logger.cpp
  12. 26
    8
      src/logger.h
  13. 16
    0
      src/main.cpp
  14. 12
    1
      src/platform.h
  15. 37
    0
      src/resourcemanager.cpp
  16. 42
    0
      src/resourcemanager.h
  17. 1
    0
      src/util.cpp
  18. 21
    0
      src/util.h
  19. 11
    0
      src/x11platform.cpp

+ 31
- 15
CMakeLists.txt View File

@@ -1,11 +1,16 @@
1 1
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
2 2
 PROJECT(otclient)
3 3
 
4
+SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
5
+
4 6
 # find needed packages
5 7
 SET(Boost_USE_STATIC_LIBS ON)
6
-FIND_PACKAGE(Boost COMPONENTS thread filesystem REQUIRED)
8
+FIND_PACKAGE(Boost REQUIRED)
7 9
 FIND_PACKAGE(OpenGL REQUIRED)
8 10
 FIND_PACKAGE(Lua51 REQUIRED)
11
+FIND_PACKAGE(YamlCpp REQUIRED)
12
+FIND_PACKAGE(PhysFS REQUIRED)
13
+FIND_PACKAGE(PNG REQUIRED)
9 14
 
10 15
 # choose a default build type if not specified
11 16
 IF(NOT CMAKE_BUILD_TYPE)
@@ -15,19 +20,26 @@ MESSAGE(STATUS "BUILD TYPE: " ${CMAKE_BUILD_TYPE})
15 20
 
16 21
 # setup compiler options
17 22
 IF(CMAKE_COMPILER_IS_GNUCXX)
18
-    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wl,--as-needed")
23
+    SET(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -Wno-unused-parameter")
19 24
     SET(CMAKE_CXX_FLAGS_DEBUG "-O1 -g -ggdb -fno-inline")
20 25
     SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wl,-s")
21 26
     SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
27
+    SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -Wl,--as-needed")
22 28
 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
23 29
 
24 30
 INCLUDE_DIRECTORIES(
25
-${LUA_INCLUDE_DIRS}
26
-${Boost_INCLUDE_DIRS})
31
+    ${Boost_INCLUDE_DIRS}
32
+    ${LUA_INCLUDE_DIRS}
33
+    ${YAMLCPP_INCLUDE_DIRS}
34
+    ${PHYSFS_INCLUDE_DIRS}
35
+    ${PNG_INCLUDE_DIRS})
27 36
 
28 37
 LINK_DIRECTORIES(
29
-${Boost_LIBRARY_DIRS}
30
-${LUA_LIBRARY_DIRS})
38
+    ${Boost_LIBRARY_DIRS}
39
+    ${LUA_LIBRARY_DIRS}
40
+    ${PHYSFS_LIBRARY_DIRS}
41
+    ${YAMLCPP_LIBRARY_DIRS}
42
+    ${PNG_LIBRARY_DIRS})
31 43
 
32 44
 # setup definitions
33 45
 ADD_DEFINITIONS(-D_REENTRANT)
@@ -38,11 +50,13 @@ ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
38 50
 
39 51
 # find sources
40 52
 SET(SOURCES
41
-src/main.cpp
42
-src/engine.cpp
43
-src/graphics.cpp
44
-src/logger.cpp
45
-src/util.cpp)
53
+    src/configmanager.cpp
54
+    src/resourcemanager.cpp
55
+    src/main.cpp
56
+    src/engine.cpp
57
+    src/graphics.cpp
58
+    src/logger.cpp
59
+    src/util.cpp)
46 60
 
47 61
 IF(WIN32)
48 62
     SET(SOURCES ${SOURCES} src/win32platform.cpp)
@@ -55,7 +69,9 @@ ADD_EXECUTABLE(otclient ${SOURCES})
55 69
 
56 70
 # target link libraries
57 71
 TARGET_LINK_LIBRARIES(otclient
58
-${Boost_LIBRARIES}
59
-${OPENGL_LIBRARY}
60
-${LUA51_LIBRARY})
61
-
72
+    ${Boost_LIBRARIES}
73
+    ${OPENGL_LIBRARY}
74
+    ${LUA51_LIBRARY}
75
+    ${YAMLCPP_LIBRARY}
76
+    ${PHYSFS_LIBRARY}
77
+    ${PNG_LIBRARY})

+ 40
- 0
cmake/FindYamlCpp.cmake View File

@@ -0,0 +1,40 @@
1
+# - Try to find yaml-cpp
2
+# Once done, this will define
3
+#
4
+# YAMLCPP_LIBRARY, link these to use yaml-cpp
5
+# YAMLCPP_FOUND - system has yaml-cpp
6
+# YAMLCPP_INCLUDE_DIR, the yaml-cpp include directories
7
+
8
+FIND_PATH(YAMLCPP_INCLUDE_DIR yaml-cpp/yaml.h
9
+HINTS
10
+$ENV{PHYSFSDIR}
11
+PATH_SUFFIXES include/yaml-cpp include
12
+PATHS
13
+~/Library/Frameworks
14
+/Library/Frameworks
15
+/usr/local
16
+/usr
17
+/sw # Fink
18
+/opt/local # DarwinPorts
19
+/opt/csw # Blastwave
20
+/opt
21
+)
22
+
23
+FIND_LIBRARY(YAMLCPP_LIBRARY
24
+NAMES yaml-cpp
25
+HINTS
26
+$ENV{PHYSFSDIR}
27
+PATH_SUFFIXES lib64 lib
28
+PATHS
29
+~/Library/Frameworks
30
+/Library/Frameworks
31
+/usr/local
32
+/usr
33
+/sw
34
+/opt/local
35
+/opt/csw
36
+/opt
37
+)
38
+
39
+INCLUDE("${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake")
40
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(YamlCpp DEFAULT_MSG YAMLCPP_LIBRARY YAMLCPP_INCLUDE_DIR)

+ 153
- 0
src/configmanager.cpp View File

@@ -0,0 +1,153 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#include "configmanager.h"
26
+#include "logger.h"
27
+#include "util.h"
28
+
29
+#include <fstream>
30
+#include <yaml-cpp/yaml.h>
31
+
32
+ConfigManager g_config;
33
+
34
+ConfigManager::ConfigManager()
35
+{
36
+
37
+}
38
+
39
+ConfigManager::~ConfigManager()
40
+{
41
+
42
+}
43
+
44
+bool ConfigManager::load(const std::string& fileName)
45
+{
46
+    std::ifstream fin(fileName.c_str());
47
+    if(!fin.good())
48
+        return false;
49
+
50
+    m_fileName = fileName;
51
+
52
+    try {
53
+        YAML::Parser parser(fin);
54
+
55
+        YAML::Node doc;
56
+        parser.GetNextDocument(doc);
57
+
58
+        for(YAML::Iterator it=doc.begin(); it != doc.end(); ++it) {
59
+            std::string key, value;
60
+            it.first() >> key;
61
+            it.second() >> value;
62
+            m_confsMap[key] = value;
63
+        }
64
+    } catch (YAML::ParserException& e) {
65
+        error("Malformed configuration file!");
66
+        return false;
67
+    }
68
+
69
+    return true;
70
+}
71
+
72
+void ConfigManager::save()
73
+{
74
+    std::ofstream fout(m_fileName.c_str());
75
+    if(!fout.good()) {
76
+        error("Failed to save configuration file %s", m_fileName.c_str());
77
+        return;
78
+    }
79
+
80
+    YAML::Emitter out;
81
+    out << m_confsMap;
82
+
83
+    fout << out.c_str();
84
+}
85
+
86
+void ConfigManager::setValue(const std::string &key, const std::string &value)
87
+{
88
+    m_confsMap[key] = value;
89
+}
90
+
91
+void ConfigManager::setValue(const std::string &key, const char *value)
92
+{
93
+    m_confsMap[key] = value;
94
+}
95
+
96
+void ConfigManager::setValue(const std::string &key, int value)
97
+{
98
+    setValue(key, castToString<int>(value));
99
+}
100
+
101
+void ConfigManager::setValue(const std::string &key, float value)
102
+{
103
+    setValue(key, castToString<float>(value));
104
+}
105
+
106
+void ConfigManager::setValue(const std::string &key, bool value)
107
+{
108
+    if(value)
109
+        setValue(key,"true");
110
+    else
111
+        setValue(key,"false");
112
+}
113
+
114
+const std::string &ConfigManager::getString(const std::string &key)
115
+{
116
+    std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
117
+    if(iter == m_confsMap.end()) {
118
+        warning("Config value %s not found", key.c_str());
119
+        static std::string emptystr;
120
+        return emptystr;
121
+    }
122
+    return iter->second;
123
+}
124
+
125
+float ConfigManager::getFloat(const std::string &key)
126
+{
127
+    std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
128
+    if(iter == m_confsMap.end()) {
129
+        warning("Config value %s not found", key.c_str());
130
+        return 0;
131
+    }
132
+    return castFromString<float>(iter->second);
133
+}
134
+
135
+bool ConfigManager::getBoolean(const std::string &key)
136
+{
137
+    std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
138
+    if(iter == m_confsMap.end()) {
139
+        warning("Config value %s not found", key.c_str());
140
+        return 0;
141
+    }
142
+    return (iter->second == std::string("true"));
143
+}
144
+
145
+int ConfigManager::getInteger(const std::string &key)
146
+{
147
+    std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
148
+    if(iter == m_confsMap.end()) {
149
+        warning("Config value %s not found", key.c_str());
150
+        return 0;
151
+    }
152
+    return castFromString<int>(iter->second);
153
+}

+ 61
- 0
src/configmanager.h View File

@@ -0,0 +1,61 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#ifndef CONFIGMANAGER_H
26
+#define CONFIGMANAGER_H
27
+
28
+#include <string>
29
+#include <map>
30
+
31
+class ConfigManager
32
+{
33
+public:
34
+    ConfigManager();
35
+    ~ConfigManager();
36
+
37
+    /// Read configuration file and parse all settings to memory
38
+    bool load(const std::string& fileName);
39
+
40
+    /// Dump all settings to configuration file
41
+    void save();
42
+
43
+    void setValue(const std::string &key, const std::string &value);
44
+    void setValue(const std::string &key, const char *value);
45
+    void setValue(const std::string &key, float value);
46
+    void setValue(const std::string &key, bool value);
47
+    void setValue(const std::string &key, int value);
48
+
49
+    const std::string &getString(const std::string &key);
50
+    float getFloat(const std::string &key);
51
+    bool getBoolean(const std::string &key);
52
+    int getInteger(const std::string &key);
53
+
54
+private:
55
+    std::string m_fileName;
56
+    std::map<std::string, std::string> m_confsMap;
57
+};
58
+
59
+extern ConfigManager g_config;
60
+
61
+#endif // CONFIGMANAGER_H

+ 1
- 0
src/const.h View File

@@ -21,6 +21,7 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #ifndef VERSION_H
25 26
 #define VERSION_H
26 27
 

+ 9
- 2
src/engine.cpp View File

@@ -21,11 +21,14 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #include "engine.h"
25 26
 #include "platform.h"
26 27
 #include "graphics.h"
27 28
 #include "const.h"
28 29
 #include "input.h"
30
+#include "configmanager.h"
31
+#include "logger.h"
29 32
 
30 33
 Engine g_engine;
31 34
 
@@ -44,8 +47,8 @@ void Engine::init()
44 47
 {
45 48
     Platform::init();
46 49
 
47
-    int width = 640;
48
-    int height = 480;
50
+    int width = g_config.getInteger("width");
51
+    int height = g_config.getInteger("height");
49 52
 
50 53
     // create the window
51 54
     Platform::createWindow(width, height, 550, 450);
@@ -63,6 +66,10 @@ void Engine::init()
63 66
 
64 67
 void Engine::terminate()
65 68
 {
69
+    // save configs
70
+    g_config.setValue("width", Platform::getWindowWidth());
71
+    g_config.setValue("height", Platform::getWindowHeight());
72
+
66 73
     Platform::showMouseCursor();
67 74
     Platform::terminate();
68 75
     g_graphics.terminate();

+ 7
- 2
src/engine.h View File

@@ -21,6 +21,7 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #ifndef ENGINE_H
25 26
 #define ENGINE_H
26 27
 
@@ -35,20 +36,24 @@ public:
35 36
     void init();
36 37
     void terminate();
37 38
 
39
+    /// Main loop
38 40
     void run();
39 41
     void stop();
40 42
 
41 43
     bool isRunning() const { return m_running; }
42 44
     bool isStopping() const { return m_stopping; }
43
-    unsigned long getLastFrameTicks() const { return m_lastFrameTicks; }
44 45
 
45
-    // events fired by platform
46
+    /// Fired by platform on window close
46 47
     void onClose();
48
+    /// Fired by platform on window resize
47 49
     void onResize(int width, int height);
50
+    /// Fired by platform on mouse/keyboard input
48 51
     void onInputEvent(InputEvent *event);
49 52
 
50 53
 private:
54
+    /// Called to render every frame
51 55
     void render();
56
+    /// Called between renders
52 57
     void update(int elapsedTicks);
53 58
 
54 59
     bool m_stopping;

+ 1
- 0
src/graphics.cpp View File

@@ -21,6 +21,7 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #include "graphics.h"
25 26
 #include "logger.h"
26 27
 

+ 5
- 0
src/graphics.h View File

@@ -21,6 +21,7 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #ifndef GRAPHICS_H
25 26
 #define GRAPHICS_H
26 27
 
@@ -33,9 +34,13 @@ public:
33 34
     void init();
34 35
     void terminate();
35 36
 
37
+    ///Called after every window resize
36 38
     void resize(int width, int height);
37 39
 
40
+    ///Called before every render
38 41
     void beginRender();
42
+
43
+    ///Called after every render
39 44
     void endRender();
40 45
 };
41 46
 

+ 1
- 0
src/input.h View File

@@ -21,6 +21,7 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #ifndef INPUT_H
25 26
 #define INPUT_H
26 27
 

+ 8
- 13
src/logger.cpp View File

@@ -21,19 +21,17 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #include "logger.h"
25 26
 #include "util.h"
26 27
 
27
-#include <iostream>
28
-#include <sstream>
29 28
 #include <cstdarg>
30 29
 #include <cstdlib>
31 30
 #include <boost/algorithm/string.hpp>
32 31
 
33
-void _log(int level, const char *trace, const char *format, ...)
32
+void Logger::log(int level, const char *trace, const char *format, ...)
34 33
 {
35 34
     va_list args;
36
-    std::stringstream out;
37 35
     std::string strace;
38 36
 
39 37
     va_start(args, format);
@@ -51,25 +49,22 @@ void _log(int level, const char *trace, const char *format, ...)
51 49
     static char const *colors[] = { "\033[01;31m ", "\033[01;31m", "\033[01;33m", "\033[0;32m", "\033[01;34m" };
52 50
     static bool colored = getenv("COLORED_OUTPUT");
53 51
     if(colored)
54
-        out << colors[level];
52
+        std::cout << colors[level];
55 53
 #endif
56 54
 
57 55
     if(!strace.empty())
58
-        out << "[" << strace << "] ";
56
+        std::cout << "[" << strace << "] ";
59 57
 
60 58
     static char const *prefixes[] = { "FATAL ERROR: ", "ERROR: ", "WARNING: ", "", "", "" };
61
-    out << prefixes[level];
62
-    out << text;
59
+    std::cout << prefixes[level];
60
+    std::cout << text;
63 61
 
64 62
 #ifdef linux
65 63
     if(colored)
66
-        out << "\033[0m";
64
+        std::cout << "\033[0m";
67 65
 #endif
68 66
 
69
-    if(level <= LWARNING)
70
-        std::cerr << out.str() << std::endl;
71
-    else
72
-        std::cout << out.str() << std::endl;
67
+    std::cout << std::endl;
73 68
 
74 69
     if(level == LFATAL)
75 70
         exit(-1);

+ 26
- 8
src/logger.h View File

@@ -21,11 +21,16 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #ifndef LOGGER_H
25 26
 #define LOGGER_H
26 27
 
28
+#include <iostream>
29
+#include <sstream>
27 30
 #include <string>
28 31
 
32
+namespace Logger {
33
+
29 34
 enum ELogLevel {
30 35
     LFATAL = 0,
31 36
     LERROR,
@@ -34,15 +39,28 @@ enum ELogLevel {
34 39
     LDEBUG
35 40
 };
36 41
 
37
-void _log(int level, const char *trace, const char *format, ...);
42
+void log(int level, const char *trace, const char *format, ...);
43
+
44
+}
38 45
 
39
-#define fatal(...) _log(LFATAL, NULL, __VA_ARGS__)
40
-#define error(...) _log(LERROR, NULL, __VA_ARGS__)
41
-#define warning(...) _log(LWARNING, NULL, __VA_ARGS__)
42
-#define debug(...) _log(LDEBUG, NULL, __VA_ARGS__)
43
-#define notice(...) _log(LNOTICE, NULL, __VA_ARGS__)
46
+#define fatal(...) Logger::log(Logger::LFATAL, NULL, __VA_ARGS__)
47
+#define error(...) Logger::log(Logger::LERROR, NULL, __VA_ARGS__)
48
+#define warning(...) Logger::log(Logger::LWARNING, NULL, __VA_ARGS__)
49
+#define debug(...) Logger::log(Logger::LDEBUG, NULL, __VA_ARGS__)
50
+#define notice(...) Logger::log(Logger::LNOTICE, NULL, __VA_ARGS__)
51
+
52
+#define trace() Logger::log(Logger::LDEBUG, __PRETTY_FUNCTION__, "")
53
+#define tdebug(...) Logger::log(Logger::LDEBUG, __PRETTY_FUNCTION__, __VA_ARGS__)
54
+
55
+struct Dump {
56
+public:
57
+    Dump() { }
58
+    ~Dump() { debug(m_buf.str().c_str()); }
59
+    template<class T>  Dump &operator<<(const T &x) { m_buf << x << " "; return *this; }
60
+private:
61
+    std::ostringstream m_buf;
62
+};
44 63
 
45
-#define trace() _log(LDEBUG, __PRETTY_FUNCTION__, "")
46
-#define tdebug(...) _log(LDEBUG, __PRETTY_FUNCTION__, __VA_ARGS__)
64
+#define dump() Dump()
47 65
 
48 66
 #endif

+ 16
- 0
src/main.cpp View File

@@ -21,11 +21,14 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #include "engine.h"
25 26
 #include "const.h"
26 27
 #include "logger.h"
27 28
 
28 29
 #include <csignal>
30
+#include "configmanager.h"
31
+#include "util.h"
29 32
 
30 33
 // catches terminate signals to exit nicely
31 34
 void signal_handler(int sig)
@@ -45,6 +48,13 @@ void signal_handler(int sig)
45 48
     }
46 49
 }
47 50
 
51
+/// Default otclient configurations
52
+void setDefaultConfigs()
53
+{
54
+    g_config.setValue("width", 640);
55
+    g_config.setValue("height", 480);
56
+}
57
+
48 58
 int main(int argc, const char *argv[])
49 59
 {
50 60
     // install our signal handler
@@ -52,11 +62,17 @@ int main(int argc, const char *argv[])
52 62
     signal(SIGINT, signal_handler);
53 63
     signal(SIGQUIT, signal_handler);
54 64
 
65
+    setDefaultConfigs();
66
+    if(!g_config.load("config.yml"))
67
+        notice("Could not read configuration file, default configurations will be used.");
68
+
55 69
     notice(APP_LONGNAME);
56 70
 
57 71
     // setup the engine and run
58 72
     g_engine.init();
59 73
     g_engine.run();
60 74
     g_engine.terminate();
75
+
76
+    g_config.save();
61 77
     return 0;
62 78
 }

+ 12
- 1
src/platform.h View File

@@ -21,17 +21,22 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #ifndef PLATFORM_H
25 26
 #define PLATFORM_H
26 27
 
28
+// namespace with platform specific stuff
27 29
 namespace Platform
28 30
 {
29 31
     void init();
30 32
     void terminate();
31 33
 
34
+    /// Poll platform input/window events
32 35
     void poll();
33 36
 
37
+    /// Get current time in milliseconds since first frame render
34 38
     unsigned long getTicks();
39
+    /// Sleep in current thread
35 40
     void sleep(unsigned long miliseconds);
36 41
 
37 42
     bool createWindow(int width, int height, int minWidth, int minHeight);
@@ -40,8 +45,12 @@ namespace Platform
40 45
     void setWindowTitle(const char *title);
41 46
     bool isWindowFocused();
42 47
     bool isWindowVisible();
48
+    int getWindowWidth();
49
+    int getWindowHeight();
43 50
 
51
+    /// Get GL extension function address
44 52
     void *getExtensionProcAddress(const char *ext);
53
+    /// Check if GL extension is supported
45 54
     bool isExtensionSupported(const char *ext);
46 55
 
47 56
     const char *getTextFromClipboard();
@@ -50,8 +59,10 @@ namespace Platform
50 59
     void hideMouseCursor();
51 60
     void showMouseCursor();
52 61
 
62
+    /// Enable/disable vertical synchronization
53 63
     void setVsync(bool enable = true);
64
+    /// Swap GL buffers
54 65
     void swapBuffers();
55
-};
66
+}
56 67
 
57 68
 #endif // PLATFORM_H

+ 37
- 0
src/resourcemanager.cpp View File

@@ -0,0 +1,37 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#include "resourcemanager.h"
26
+
27
+ResourceManager g_resources;
28
+
29
+ResourceManager::ResourceManager()
30
+{
31
+
32
+}
33
+
34
+ResourceManager::~ResourceManager()
35
+{
36
+
37
+}

+ 42
- 0
src/resourcemanager.h View File

@@ -0,0 +1,42 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#ifndef RESOURCEMANAGER_H
26
+#define RESOURCEMANAGER_H
27
+
28
+#include <string>
29
+#include <list>
30
+
31
+class ResourceManager
32
+{
33
+public:
34
+    ResourceManager();
35
+    ~ResourceManager();
36
+
37
+private:
38
+};
39
+
40
+extern ResourceManager g_resources;
41
+
42
+#endif // RESOURCEMANAGER_H

+ 1
- 0
src/util.cpp View File

@@ -21,6 +21,7 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #include "util.h"
25 26
 
26 27
 #include <cstdio>

+ 21
- 0
src/util.h View File

@@ -21,13 +21,34 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #ifndef UTIL_H
25 26
 #define UTIL_H
26 27
 
27 28
 #include <string>
29
+#include <sstream>
28 30
 #include <cstdarg>
29 31
 
32
+/// Formatting like printf for std::string, va_list input version
30 33
 std::string vformat(const char *format, va_list args);
34
+
35
+/// Formatting like printf for std::string
31 36
 std::string format(const char *format, ...);
32 37
 
38
+/// Convert int/float like types to std::string
39
+template<typename T>
40
+inline std::string castToString(const T& x) {
41
+    std::ostringstream ss;
42
+    ss << x;
43
+    return ss.str();
44
+}
45
+
46
+template<typename T>
47
+inline T castFromString(const std::string& s) {
48
+    std::istringstream ss(s);
49
+    T x = 0;
50
+    ss >> x;
51
+    return x;
52
+}
53
+
33 54
 #endif

+ 11
- 0
src/x11platform.cpp View File

@@ -21,6 +21,7 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
+
24 25
 #include "platform.h"
25 26
 #include "engine.h"
26 27
 #include "input.h"
@@ -708,3 +709,13 @@ bool Platform::isWindowVisible()
708 709
 {
709 710
     return x11.visible;
710 711
 }
712
+
713
+int Platform::getWindowWidth()
714
+{
715
+    return x11.width;
716
+}
717
+
718
+int Platform::getWindowHeight()
719
+{
720
+    return x11.height;
721
+}

Loading…
Cancel
Save