summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/README.txt7
-rw-r--r--src/common/event.h63
-rw-r--r--src/common/iman.cpp15
-rw-r--r--src/common/iman.h8
-rw-r--r--src/common/logger.cpp16
-rw-r--r--src/common/logger.h16
-rw-r--r--src/common/profile.cpp111
-rw-r--r--src/common/profile.h98
-rw-r--r--src/common/stringutils.cpp10
-rw-r--r--src/common/test/CMakeLists.txt8
-rw-r--r--src/common/test/colobot.ini15
-rw-r--r--src/common/test/profile_test.cpp43
12 files changed, 280 insertions, 130 deletions
diff --git a/src/common/README.txt b/src/common/README.txt
index 36653cc..73d65b7 100644
--- a/src/common/README.txt
+++ b/src/common/README.txt
@@ -1,3 +1,4 @@
-src/common
-
-Contains headers and modules with common structs and enums.
+/**
+ * \dir common
+ * \brief Structs and utils shared throughout the application
+ */
diff --git a/src/common/event.h b/src/common/event.h
index 0d9aa7c..1cfab35 100644
--- a/src/common/event.h
+++ b/src/common/event.h
@@ -19,8 +19,8 @@
#pragma once
-#include "common/key.h"
-#include "math/point.h"
+#include <common/key.h>
+#include <math/point.h>
#include <string.h>
@@ -40,9 +40,10 @@ enum EventType
EVENT_NULL = 0,
//! Event sent on user or system quit request
- EVENT_QUIT = 1,
+ EVENT_QUIT = 1,
- //? EVENT_FRAME = 2,
+ //! Frame update event
+ EVENT_FRAME = 2,
//! Event sent after pressing a mouse button
EVENT_MOUSE_BUTTON_DOWN = 3,
@@ -669,29 +670,41 @@ struct Event
//! If true, the event was produced by system (SDL); else, it has come from user interface
bool systemEvent;
- //! Additional data for EVENT_KEY_DOWN and EVENT_KEY_UP
- KeyEventData key;
- //! Additional data for EVENT_MOUSE_BUTTON_DOWN and EVENT_MOUSE_BUTTON_UP
- MouseButtonEventData mouseButton;
- //! Additional data for EVENT_MOUSE_MOVE
- MouseMoveEventData mouseMove;
- //! Additional data for EVENT_JOY
- JoyAxisEventData joyAxis;
- //! Additional data for EVENT_JOY_AXIS
- JoyButtonEventData joyButton;
- //! Additional data for EVENT_ACTIVE
- ActiveEventData active;
-
- //? long param; // parameter
- //? Math::Point pos; // mouse position (0 .. 1)
- //? float axeX; // control the X axis (-1 .. 1)
- //? float axeY; // control of the Y axis (-1 .. 1)
- //? float axeZ; // control the Z axis (-1 .. 1)
- //? short keyState; // state of the keyboard (KS_ *)
- //? float rTime; // relative time
+ union
+ {
+ //! Additional data for EVENT_KEY_DOWN and EVENT_KEY_UP
+ KeyEventData key;
+ //! Additional data for EVENT_MOUSE_BUTTON_DOWN and EVENT_MOUSE_BUTTON_UP
+ MouseButtonEventData mouseButton;
+ //! Additional data for EVENT_MOUSE_MOVE
+ MouseMoveEventData mouseMove;
+ //! Additional data for EVENT_JOY
+ JoyAxisEventData joyAxis;
+ //! Additional data for EVENT_JOY_AXIS
+ JoyButtonEventData joyButton;
+ //! Additional data for EVENT_ACTIVE
+ ActiveEventData active;
+ };
+
+ // TODO: refactor/rewrite
+ long param; // parameter
+ Math::Point pos; // mouse position (0 .. 1)
+ float axeX; // control the X axis (-1 .. 1)
+ float axeY; // control of the Y axis (-1 .. 1)
+ float axeZ; // control the Z axis (-1 .. 1)
+ short keyState; // state of the keyboard (KS_ *)
+ float rTime; // relative time
Event(EventType aType = EVENT_NULL)
- : type(aType), systemEvent(false) {}
+ {
+ type = aType;
+ systemEvent = false;
+
+ param = 0;
+ axeX = axeY = axeZ = 0.0f;
+ keyState = 0;
+ rTime = 0.0f;
+ }
};
diff --git a/src/common/iman.cpp b/src/common/iman.cpp
index 90b0c1e..4b89ecf 100644
--- a/src/common/iman.cpp
+++ b/src/common/iman.cpp
@@ -23,6 +23,21 @@
#include "common/iman.h"
+template<> CInstanceManager* CSingleton<CInstanceManager>::mInstance = nullptr;
+
+
+CInstanceManager& CInstanceManager::GetInstance()
+{
+ assert(mInstance);
+ return *mInstance;
+}
+
+
+CInstanceManager* CInstanceManager::GetInstancePointer()
+{
+ assert(mInstance);
+ return mInstance;
+}
// Object's constructor.
diff --git a/src/common/iman.h b/src/common/iman.h
index 7a7b499..89b5206 100644
--- a/src/common/iman.h
+++ b/src/common/iman.h
@@ -18,8 +18,8 @@
#pragma once
-
-#include "common/misc.h"
+#include <common/singleton.h>
+#include <common/misc.h>
@@ -32,7 +32,7 @@ struct BaseClass
-class CInstanceManager
+class CInstanceManager : public CSingleton<CInstanceManager>
{
public:
CInstanceManager();
@@ -44,6 +44,8 @@ public:
bool DeleteInstance(ClassType classType, void* pointer);
void* SearchInstance(ClassType classType, int rank=0);
+ static CInstanceManager& GetInstance();
+ static CInstanceManager* GetInstancePointer();
protected:
void Compress(ClassType classType);
diff --git a/src/common/logger.cpp b/src/common/logger.cpp
index f24726e..be73ec7 100644
--- a/src/common/logger.cpp
+++ b/src/common/logger.cpp
@@ -21,21 +21,7 @@
#include <stdio.h>
-template<> CLogger* CSingleton<CLogger>::mInstance = 0;
-
-
-CLogger& CLogger::GetInstance()
-{
- assert(mInstance);
- return *mInstance;
-}
-
-
-CLogger* CLogger::GetInstancePointer()
-{
- assert(mInstance);
- return mInstance;
-}
+template<> CLogger* CSingleton<CLogger>::mInstance = nullptr;
CLogger::CLogger()
diff --git a/src/common/logger.h b/src/common/logger.h
index a67aefe..f126e52 100644
--- a/src/common/logger.h
+++ b/src/common/logger.h
@@ -21,6 +21,7 @@
#include <string>
#include <cstdarg>
+#include <cstdio>
#include <common/singleton.h>
@@ -57,42 +58,39 @@ class CLogger : public CSingleton<CLogger>
~CLogger();
/** Write message to console or file
- * @param const char str - message to write
+ * @param str - message to write
* @param ... - additional arguments
*/
void Message(const char *str, ...);
/** Write message to console or file with LOG_INFO level
- * @param const char str - message to write
+ * @param str - message to write
* @param ... - additional arguments
*/
void Info(const char *str, ...);
/** Write message to console or file with LOG_WARN level
- * @param const char str - message to write
+ * @param str - message to write
* @param ... - additional arguments
*/
void Warn(const char *str, ...);
/** Write message to console or file with LOG_ERROR level
- * @param const char str - message to write
+ * @param str - message to write
* @param ... - additional arguments
*/
void Error(const char *str, ...);
/** Set output file to write logs to
- * @param std::string filename - output file to write to
+ * @param filename - output file to write to
*/
void SetOutputFile(std::string filename);
/** Set log level. Logs with level below will not be shown
- * @param LogType level - minimum log level to write
+ * @param level - minimum log level to write
*/
void SetLogLevel(LogType level);
- static CLogger& GetInstance();
- static CLogger* GetInstancePointer();
-
private:
std::string mFilename;
FILE *mFile;
diff --git a/src/common/profile.cpp b/src/common/profile.cpp
index d921d34..29a68e1 100644
--- a/src/common/profile.cpp
+++ b/src/common/profile.cpp
@@ -17,98 +17,89 @@
// profile.cpp
-#include <stdio.h>
-#include <d3d.h>
-#include <stdlib.h>
+#include <common/profile.h>
-#include "common/language.h"
-#include "common/struct.h"
-#include "common/profile.h"
+template<> CProfile* CSingleton<CProfile>::mInstance = nullptr;
-static char g_filename[100];
-
+CProfile::CProfile()
+{
+ m_ini = new CSimpleIniA();
+ m_ini->SetUnicode();
+ m_ini->SetMultiKey();
+}
-bool InitCurrentDirectory()
+CProfile::~CProfile()
{
-#if _SCHOOL
- _fullpath(g_filename, "ceebot.ini", 100);
-#else
- _fullpath(g_filename, "colobot.ini", 100);
-#endif
- return true;
+ m_ini->Reset();
+ delete m_ini;
}
-bool SetLocalProfileString(char* section, char* key, char* string)
+bool CProfile::InitCurrentDirectory()
{
- WritePrivateProfileString(section, key, string, g_filename);
- return true;
+ bool result = m_ini->LoadFile("colobot.ini") == SI_OK;
+ return result;
}
-bool GetLocalProfileString(char* section, char* key, char* buffer, int max)
+
+bool CProfile::SetLocalProfileString(std::string section, std::string key, std::string value)
{
- int nb;
+ return (m_ini->SetValue(section.c_str(), key.c_str(), value.c_str()) == SI_OK);
+}
- nb = GetPrivateProfileString(section, key, "", buffer, max, g_filename);
- if ( nb == 0 )
- {
- buffer[0] = 0;
- return false;
+
+bool CProfile::GetLocalProfileString(std::string section, std::string key, std::string &buffer)
+{
+ const char* value = m_ini->GetValue(section.c_str(), key.c_str(), nullptr);
+ if (strlen(value) > 0) {
+ buffer = std::string(value);
+ return true;
}
- return true;
+
+ return false;
}
-bool SetLocalProfileInt(char* section, char* key, int value)
+bool CProfile::SetLocalProfileInt(std::string section, std::string key, int value)
{
- char s[20];
-
- sprintf(s, "%d", value);
- WritePrivateProfileString(section, key, s, g_filename);
- return true;
+ return (m_ini->SetLongValue(section.c_str(), key.c_str(), value) == SI_OK);
}
-bool GetLocalProfileInt(char* section, char* key, int &value)
+
+bool CProfile::GetLocalProfileInt(std::string section, std::string key, int &value)
{
- char s[20];
- int nb;
-
- nb = GetPrivateProfileString(section, key, "", s, 20, g_filename);
- if ( nb == 0 )
- {
- value = 0;
- return false;
- }
- sscanf(s, "%d", &value);
+ value = m_ini->GetLongValue(section.c_str(), key.c_str(), 0L);
return true;
}
-bool SetLocalProfileFloat(char* section, char* key, float value)
+bool CProfile::SetLocalProfileFloat(std::string section, std::string key, float value)
{
- char s[20];
-
- sprintf(s, "%.2f", value);
- WritePrivateProfileString(section, key, s, g_filename);
- return true;
+ return (m_ini->SetDoubleValue(section.c_str(), key.c_str(), value) == SI_OK);
}
-bool GetLocalProfileFloat(char* section, char* key, float &value)
+
+bool CProfile::GetLocalProfileFloat(std::string section, std::string key, float &value)
{
- char s[20];
- int nb;
-
- nb = GetPrivateProfileString(section, key, "", s, 20, g_filename);
- if ( nb == 0 )
- {
- value = 0.0f;
- return false;
- }
- sscanf(s, "%f", &value);
+ value = m_ini->GetDoubleValue(section.c_str(), key.c_str(), 0.0d);
return true;
}
+std::vector< std::string > CProfile::GetLocalProfileSection(std::string section, std::string key)
+{
+ std::vector< std::string > ret_list;
+
+ CSimpleIniA::TNamesDepend values;
+ m_ini->GetAllValues(section.c_str(), key.c_str(), values);
+ values.sort(CSimpleIniA::Entry::LoadOrder());
+
+ for (auto item : values) {
+ ret_list.push_back(item.pItem);
+ }
+
+ return ret_list;
+}
diff --git a/src/common/profile.h b/src/common/profile.h
index 2c76a0b..0886522 100644
--- a/src/common/profile.h
+++ b/src/common/profile.h
@@ -18,13 +18,97 @@
#pragma once
+#include <cstdlib>
+#include <vector>
+#include <utility>
-extern bool InitCurrentDirectory();
-extern bool SetLocalProfileString(char* section, char* key, char* string);
-extern bool GetLocalProfileString(char* section, char* key, char* buffer, int max);
-extern bool SetLocalProfileInt(char* section, char* key, int value);
-extern bool GetLocalProfileInt(char* section, char* key, int &value);
-extern bool SetLocalProfileFloat(char* section, char* key, float value);
-extern bool GetLocalProfileFloat(char* section, char* key, float &value);
+#include <lib/simpleini/SimpleIni.h>
+#include <common/singleton.h>
+/**
+ * @file common/profile.h
+ * @brief Class for loading profile (currently for loading ini config file)
+ */
+
+
+/**
+* @class CProfile
+*
+* @brief Class for loading profile (currently for loading ini config file)
+*
+*/
+class CProfile : public CSingleton<CProfile>
+{
+ public:
+ CProfile();
+ ~CProfile();
+
+ /** Loads colobot.ini from current directory
+ * @return return true on success
+ */
+ bool InitCurrentDirectory();
+
+ /** Sets string value in section under specified key
+ * @param std::string section
+ * @param std::string key
+ * @param std::string value
+ * @return return true on success
+ */
+ bool SetLocalProfileString(std::string section, std::string key, std::string value);
+
+ /** Gets string value in section under specified key
+ * @param std::string section
+ * @param std::string key
+ * @param std::string& buffer
+ * @return return true on success
+ */
+ bool GetLocalProfileString(std::string section, std::string key, std::string& buffer);
+
+ /** Sets int value in section under specified key
+ * @param std::string section
+ * @param std::string key
+ * @param int value
+ * @return return true on success
+ */
+ bool SetLocalProfileInt(std::string section, std::string key, int value);
+
+ /** Gets int value in section under specified key
+ * @param std::string section
+ * @param std::string key
+ * @param int& value
+ * @return return true on success
+ */
+ bool GetLocalProfileInt(std::string section, std::string key, int &value);
+
+ /** Sets float value in section under specified key
+ * @param std::string section
+ * @param std::string key
+ * @param float value
+ * @return return true on success
+ */
+ bool SetLocalProfileFloat(std::string section, std::string key, float value);
+
+ /** Gets float value in section under specified key
+ * @param std::string section
+ * @param std::string key
+ * @param float& value
+ * @return return true on success
+ */
+ bool GetLocalProfileFloat(std::string section, std::string key, float &value);
+
+ /** Gets all values in section under specified key
+ * @param std::string section
+ * @param std::string key
+ * @return vector of values
+ */
+ std::vector< std::string > GetLocalProfileSection(std::string section, std::string key);
+
+ private:
+ CSimpleIniA *m_ini;
+};
+
+//! Global function to get profile instance
+inline CProfile* GetProfile() {
+ return CProfile::GetInstancePointer();
+}
diff --git a/src/common/stringutils.cpp b/src/common/stringutils.cpp
index 585bb46..12a3179 100644
--- a/src/common/stringutils.cpp
+++ b/src/common/stringutils.cpp
@@ -135,15 +135,11 @@ int StrUtils::Utf8CharSizeAt(const std::string &str, unsigned int pos)
size_t StrUtils::Utf8StringLength(const std::string &str)
{
size_t result = 0;
- for (unsigned int i = 0; i < str.size(); ++i)
+ unsigned int i = 0;
+ while (i < str.size())
{
- char ch = str[i];
- if ((ch & 0x80) == 0)
+ i += Utf8CharSizeAt(str, i);
++result;
- else if ((ch & 0xC0) == 0xC0)
- result += 2;
- else
- result += 3;
}
return result;
}
diff --git a/src/common/test/CMakeLists.txt b/src/common/test/CMakeLists.txt
index 680116c..d81acab 100644
--- a/src/common/test/CMakeLists.txt
+++ b/src/common/test/CMakeLists.txt
@@ -1,6 +1,12 @@
cmake_minimum_required(VERSION 2.8)
set(CMAKE_BUILD_TYPE debug)
-set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0")
+set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0 -std=c++11")
+
+include_directories("../../")
+include_directories("../../../")
add_executable(image_test ../image.cpp image_test.cpp)
+add_executable(profile_test ../profile.cpp profile_test.cpp)
+
+add_test(profile_test ./profile_test)
diff --git a/src/common/test/colobot.ini b/src/common/test/colobot.ini
new file mode 100644
index 0000000..f6a5f96
--- /dev/null
+++ b/src/common/test/colobot.ini
@@ -0,0 +1,15 @@
+[test_float]
+float_value=1.5
+
+[test_string]
+string_value=Hello world
+
+[test_int]
+int_value=42
+
+[test_multi]
+entry=1
+entry=2
+entry=3
+entry=4
+entry=5
diff --git a/src/common/test/profile_test.cpp b/src/common/test/profile_test.cpp
new file mode 100644
index 0000000..65e20c5
--- /dev/null
+++ b/src/common/test/profile_test.cpp
@@ -0,0 +1,43 @@
+#include "../profile.h"
+
+#include <iostream>
+#include <string>
+#include <vector>
+
+using namespace std;
+
+int main()
+{
+ CProfile profile;
+ profile.InitCurrentDirectory(); // load colobot.ini file
+
+ string result;
+ profile.GetLocalProfileString("test_string", "string_value", result);
+ if (result != "Hello world") {
+ cout << "GetLocalProfileString failed!" << endl;
+ return 1;
+ }
+
+ int int_value;
+ profile.GetLocalProfileInt("test_int", "int_value", int_value);
+ if (int_value != 42) {
+ cout << "GetLocalProfileInt failed!" << endl;
+ return 1;
+ }
+
+ float float_value;
+ profile.GetLocalProfileFloat("test_float", "float_value", float_value);
+ if (float_value != 1.5) {
+ cout << "GetLocalProfileFloat failed!" << endl;
+ return 1;
+ }
+
+ vector<string> list;
+ list = profile.GetLocalProfileSection("test_multi", "entry");
+ if (list.size() != 5) {
+ cout << "GetLocalProfileSection failed!" << endl;
+ return 1;
+ }
+
+ return 0;
+}