diff options
author | Piotr Dziwinski <piotrdz@gmail.com> | 2013-03-30 15:01:00 +0100 |
---|---|---|
committer | Piotr Dziwinski <piotrdz@gmail.com> | 2013-03-30 15:23:05 +0100 |
commit | e218dcfdf2c58f8841e7ebd220527d08e870a6d5 (patch) | |
tree | a567db04a8a51e2db87f80e259faf222a7d4becd /src/common | |
parent | bfc701e286259d9a7aa2ff8240704eab2ba3a237 (diff) | |
parent | e607fed265070be8c518d4302d8b76f738421fb3 (diff) | |
download | colobot-e218dcfdf2c58f8841e7ebd220527d08e870a6d5.tar.gz colobot-e218dcfdf2c58f8841e7ebd220527d08e870a6d5.tar.bz2 colobot-e218dcfdf2c58f8841e7ebd220527d08e870a6d5.zip |
Merge branch 'dev'
Merging content from dev
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/config.h.cmake | 13 | ||||
-rw-r--r-- | src/common/event.cpp | 7 | ||||
-rw-r--r-- | src/common/event.h | 9 | ||||
-rw-r--r-- | src/common/global.h | 1 | ||||
-rw-r--r-- | src/common/image.cpp | 13 | ||||
-rw-r--r-- | src/common/image.h | 3 | ||||
-rw-r--r-- | src/common/iman.cpp | 15 | ||||
-rw-r--r-- | src/common/iman.h | 67 | ||||
-rw-r--r-- | src/common/key.h | 2 | ||||
-rw-r--r-- | src/common/logger.cpp | 84 | ||||
-rw-r--r-- | src/common/logger.h | 131 | ||||
-rw-r--r-- | src/common/misc.cpp | 124 | ||||
-rw-r--r-- | src/common/misc.h | 3 | ||||
-rw-r--r-- | src/common/profile.cpp | 53 | ||||
-rw-r--r-- | src/common/profile.h | 25 | ||||
-rw-r--r-- | src/common/restext.cpp | 4 | ||||
-rw-r--r-- | src/common/singleton.h | 77 | ||||
-rw-r--r-- | src/common/test/CMakeLists.txt | 22 | ||||
-rw-r--r-- | src/common/test/colobot.ini | 15 | ||||
-rw-r--r-- | src/common/test/image_test.cpp | 57 | ||||
-rw-r--r-- | src/common/test/profile_test.cpp | 44 |
21 files changed, 308 insertions, 461 deletions
diff --git a/src/common/config.h.cmake b/src/common/config.h.cmake index 022bb69..d5a03b4 100644 --- a/src/common/config.h.cmake +++ b/src/common/config.h.cmake @@ -5,14 +5,21 @@ #cmakedefine PLATFORM_LINUX @PLATFORM_LINUX@ #cmakedefine PLATFORM_OTHER @PLATFORM_OTHER@ -#cmakedefine USE_GLEW @USE_GLEW@ #cmakedefine GLEW_STATIC +#cmakedefine OPENAL_SOUND + +#cmakedefine USE_SDL_MAIN @USE_SDL_MAIN@ + +#ifdef USE_SDL_MAIN +#define SDL_MAIN_FUNC SDL_main +#else +#define SDL_MAIN_FUNC main +#endif + #define COLOBOT_VERSION "@COLOBOT_VERSION_FULL@" #define COLOBOT_CODENAME "@COLOBOT_VERSION_CODENAME@" #define COLOBOT_FULLNAME "Colobot @COLOBOT_VERSION_CODENAME@" #define COLOBOT_DEFAULT_DATADIR "@COLOBOT_INSTALL_DATA_DIR@" #define COLOBOT_I18N_DIR "@COLOBOT_INSTALL_I18N_DIR@" - -#cmakedefine OPENAL_SOUND diff --git a/src/common/event.cpp b/src/common/event.cpp index b078dc5..ff3fbc7 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -17,12 +17,12 @@ #include "common/event.h" -#include "common/iman.h" #include "common/logger.h" static EventType g_uniqueEventType = EVENT_USER; + EventType GetUniqueEventType() { int i = static_cast<int>(g_uniqueEventType+1); @@ -32,11 +32,8 @@ EventType GetUniqueEventType() -CEventQueue::CEventQueue(CInstanceManager* iMan) +CEventQueue::CEventQueue() { - m_iMan = iMan; - m_iMan->AddInstance(CLASS_EVENT, this); - Flush(); } diff --git a/src/common/event.h b/src/common/event.h index 169f0d0..153b732 100644 --- a/src/common/event.h +++ b/src/common/event.h @@ -27,8 +27,6 @@ #include "math/point.h" #include "math/vector.h" -class CInstanceManager; - /** \enum EventType @@ -411,7 +409,8 @@ enum EventType EVENT_OBJECT_BNUCLEAR = 1060, EVENT_OBJECT_BPARA = 1061, EVENT_OBJECT_BINFO = 1062, - EVENT_OBJECT_BXXXX = 1063, + EVENT_OBJECT_BDESTROYER = 1063, + //EVENT_OBJECT_BXXXX = 1063, EVENT_OBJECT_GFLAT = 1070, EVENT_OBJECT_FCREATE = 1071, EVENT_OBJECT_FDELETE = 1072, @@ -445,6 +444,7 @@ enum EventType EVENT_OBJECT_TERRAFORM = 1201, EVENT_OBJECT_FIRE = 1202, EVENT_OBJECT_FIREANT = 1203, + EVENT_OBJECT_SPIDEREXPLO= 1204, EVENT_OBJECT_RECOVER = 1220, EVENT_OBJECT_BEGSHIELD = 1221, EVENT_OBJECT_ENDSHIELD = 1222, @@ -761,7 +761,7 @@ public: public: //! Object's constructor - CEventQueue(CInstanceManager* iMan); + CEventQueue(); //! Object's destructor ~CEventQueue(); @@ -773,7 +773,6 @@ public: bool GetEvent(Event &event); protected: - CInstanceManager* m_iMan; Event m_fifo[MAX_EVENT_QUEUE]; int m_head; int m_tail; diff --git a/src/common/global.h b/src/common/global.h index 0b2d8ec..7a5fdfd 100644 --- a/src/common/global.h +++ b/src/common/global.h @@ -213,6 +213,7 @@ enum BuildType BUILD_LABO = (1<<10), //! < laboratory BUILD_PARA = (1<<11), //! < lightning protection BUILD_INFO = (1<<12), //! < information terminal + BUILD_DESTROYER = (1<<13), //! < Destroyer BUILD_GFLAT = (1<<16), //! < flat floor BUILD_FLAG = (1<<17) //! < puts / removes colored flag }; diff --git a/src/common/image.cpp b/src/common/image.cpp index ef8097e..db14797 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -22,8 +22,8 @@ #include <string.h> #include <assert.h> -#include <SDL/SDL.h> -#include <SDL/SDL_image.h> +#include <SDL.h> +#include <SDL_image.h> #include <png.h> @@ -190,6 +190,15 @@ Math::IntPoint CImage::GetSize() const return Math::IntPoint(m_data->surface->w, m_data->surface->h); } +/** Image must be valid. */ +void CImage::Fill(Gfx::IntColor color) +{ + assert(m_data != nullptr); + + Uint32 c = SDL_MapRGBA(m_data->surface->format, color.r, color.g, color.b, color.a); + SDL_FillRect(m_data->surface, nullptr, c); +} + /** * Image must be valid and pixel coords in valid range. * diff --git a/src/common/image.h b/src/common/image.h index d23a6fa..d9da75b 100644 --- a/src/common/image.h +++ b/src/common/image.h @@ -79,6 +79,9 @@ public: //! Returns the image size Math::IntPoint GetSize() const; + //! Fills the whole image with given color + void Fill(Gfx::IntColor color); + //! Sets the color at given pixel void SetPixel(Math::IntPoint pixel, Gfx::Color color); diff --git a/src/common/iman.cpp b/src/common/iman.cpp index 6a0a9eb..e1400fd 100644 --- a/src/common/iman.cpp +++ b/src/common/iman.cpp @@ -20,22 +20,9 @@ #include <cassert> -template<> CInstanceManager* CSingleton<CInstanceManager>::mInstance = nullptr; +template<> CInstanceManager* CSingleton<CInstanceManager>::m_instance = nullptr; -CInstanceManager& CInstanceManager::GetInstance() -{ - assert(mInstance); - return *mInstance; -} - - -CInstanceManager* CInstanceManager::GetInstancePointer() -{ - assert(mInstance); - return mInstance; -} - CInstanceManager::CInstanceManager() { for (int i = 0; i < CLASS_MAX; i++) diff --git a/src/common/iman.h b/src/common/iman.h index 53caed7..faabd0c 100644 --- a/src/common/iman.h +++ b/src/common/iman.h @@ -30,67 +30,23 @@ * \brief Type of class managed by CInstanceManager */ -// TODO: remove unnecessary, refactor to singletons, move to CRobotMain, keep others? - +/* + * TODO: Non-unique classes have already been removed. + * The other class instances along with CInstanceManager will be removed in due course. + */ enum ManagedClassType { - //! CEventQueue - CLASS_EVENT = 1, - //! Ui::CInterface - CLASS_INTERFACE = 2, - //! CRobotMain - CLASS_MAIN = 3, - //! Gfx::CEngine - CLASS_ENGINE = 4, - //! Gfx::CTerrain - CLASS_TERRAIN = 5, //! CObject - CLASS_OBJECT = 6, + CLASS_OBJECT = 0, //! CPhysics - CLASS_PHYSICS = 7, + CLASS_PHYSICS = 1, //! CBrain - CLASS_BRAIN = 8, - //! Gfx::CCamera - CLASS_CAMERA = 9, - //! Gfx::CLightManager - CLASS_LIGHT = 10, - //! Gfx::CParticle - CLASS_PARTICULE = 11, - //! CAuto; TODO: remove (unused) - CLASS_AUTO = 12, - //! Ui::CDisplayText - CLASS_DISPLAYTEXT = 13, + CLASS_BRAIN = 2, //! Gfx::CPyro - CLASS_PYRO = 14, - //! Ui::CScript; TODO: remove (unused) - CLASS_SCRIPT = 15, - //! Gfx::CText - CLASS_TEXT = 16, - //! Ui::CStudio, Ui::CDisplayText; TODO: remove (unused) - CLASS_STUDIO = 17, - //! Gfx::CWater - CLASS_WATER = 18, - //! Gfx::CCloud; TODO: remove (unused) - CLASS_CLOUD = 19, - //! CMotion; TODO: remove (unused) - CLASS_MOTION = 20, - //! CSoundInterface - CLASS_SOUND = 21, - //! Gfx::CPlanet - CLASS_PLANET = 22, - //! CTaskManager; TODO: remove (unused) - CLASS_TASKMANAGER = 23, - //! Ui::CMainDialog; TODO: remove (unused) - CLASS_DIALOG = 24, - //! Ui::CMainMap; TODO: remove (unused) - CLASS_MAP = 25, - //! Ui::CMainShort, CMainMovie; TODO: remove (unused) - CLASS_SHORT = 26, - //! Gfx::CLightning; TODO: remove (unused) - CLASS_BLITZ = 27, + CLASS_PYRO = 3, //! Maximum (number of managed classes) - CLASS_MAX = 30 + CLASS_MAX = 4 }; @@ -116,7 +72,7 @@ class CInstanceManager : public CSingleton<CInstanceManager> { public: CInstanceManager(); - ~CInstanceManager(); + virtual ~CInstanceManager(); //! Remove all managed instances void Flush(); @@ -129,9 +85,6 @@ public: //! Seeks a class instance of given type void* SearchInstance(ManagedClassType classType, int rank=0); - static CInstanceManager& GetInstance(); - static CInstanceManager* GetInstancePointer(); - protected: //! Fills holes in instance table void Compress(ManagedClassType classType); diff --git a/src/common/key.h b/src/common/key.h index 196f66d..84ee618 100644 --- a/src/common/key.h +++ b/src/common/key.h @@ -22,7 +22,7 @@ #pragma once -#include "SDL/SDL_keysym.h" +#include <SDL_keysym.h> /* Key definitions are specially defined here so that it is clear in other parts of the code that these are used. It is to avoid having SDL-related enum values or #defines lying around diff --git a/src/common/logger.cpp b/src/common/logger.cpp index 5a78433..8bc4cef 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -20,7 +20,7 @@ #include <stdio.h> -template<> CLogger* CSingleton<CLogger>::mInstance = nullptr; +template<> CLogger* CSingleton<CLogger>::m_instance = nullptr; CLogger::CLogger() @@ -36,25 +36,37 @@ CLogger::~CLogger() } -void CLogger::Log(LogType type, const char *str, va_list args) +void CLogger::Log(LogLevel type, const char* str, va_list args) { if (type < mLogLevel) return; - switch (type) { - case LOG_TRACE: fprintf(IsOpened() ? mFile : stderr, "[TRACE]: "); break; - case LOG_DEBUG: fprintf(IsOpened() ? mFile : stderr, "[DEBUG]: "); break; - case LOG_WARN: fprintf(IsOpened() ? mFile : stderr, "[WARN]: "); break; - case LOG_INFO: fprintf(IsOpened() ? mFile : stderr, "[INFO]: "); break; - case LOG_ERROR: fprintf(IsOpened() ? mFile : stderr, "[ERROR]: "); break; - default: break; + switch (type) + { + case LOG_TRACE: + fprintf(IsOpened() ? mFile : stderr, "[TRACE]: "); + break; + case LOG_DEBUG: + fprintf(IsOpened() ? mFile : stderr, "[DEBUG]: "); + break; + case LOG_WARN: + fprintf(IsOpened() ? mFile : stderr, "[WARN]: "); + break; + case LOG_INFO: + fprintf(IsOpened() ? mFile : stderr, "[INFO]: "); + break; + case LOG_ERROR: + fprintf(IsOpened() ? mFile : stderr, "[ERROR]: "); + break; + default: + break; } vfprintf(IsOpened() ? mFile : stderr, str, args); } -void CLogger::Trace(const char *str, ...) +void CLogger::Trace(const char* str, ...) { va_list args; va_start(args, str); @@ -63,7 +75,7 @@ void CLogger::Trace(const char *str, ...) } -void CLogger::Debug(const char *str, ...) +void CLogger::Debug(const char* str, ...) { va_list args; va_start(args, str); @@ -72,7 +84,7 @@ void CLogger::Debug(const char *str, ...) } -void CLogger::Info(const char *str, ...) +void CLogger::Info(const char* str, ...) { va_list args; va_start(args, str); @@ -81,7 +93,7 @@ void CLogger::Info(const char *str, ...) } -void CLogger::Warn(const char *str, ...) +void CLogger::Warn(const char* str, ...) { va_list args; va_start(args, str); @@ -90,7 +102,7 @@ void CLogger::Warn(const char *str, ...) } -void CLogger::Error(const char *str, ...) +void CLogger::Error(const char* str, ...) { va_list args; va_start(args, str); @@ -99,7 +111,7 @@ void CLogger::Error(const char *str, ...) } -void CLogger::Message(const char *str, ...) +void CLogger::Message(const char* str, ...) { va_list args; va_start(args, str); @@ -118,6 +130,7 @@ void CLogger::SetOutputFile(std::string filename) void CLogger::Open() { mFile = fopen(mFilename.c_str(), "w"); + if (mFile == NULL) fprintf(stderr, "Could not create file %s\n", mFilename.c_str()); } @@ -136,6 +149,45 @@ bool CLogger::IsOpened() } -void CLogger::SetLogLevel(LogType type) { +void CLogger::SetLogLevel(LogLevel type) +{ mLogLevel = type; } + + +bool CLogger::ParseLogLevel(const std::string& str, LogLevel& logLevel) +{ + if (str == "trace") + { + logLevel = LOG_TRACE; + return true; + } + else if (str == "debug") + { + logLevel = LOG_DEBUG; + return true; + } + else if (str == "info") + { + logLevel = LOG_INFO; + return true; + } + else if (str == "warn") + { + logLevel = LOG_WARN; + return true; + } + else if (str == "error") + { + logLevel = LOG_ERROR; + return true; + } + else if (str == "none") + { + logLevel = LOG_NONE; + return true; + } + + return false; +} + diff --git a/src/common/logger.h b/src/common/logger.h index 198e5e5..769f548 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -31,10 +31,10 @@ /** * \public - * \enum LogType common/logger.h + * \enum LogLevel common/logger.h * \brief Enum representing log level **/ -enum LogType +enum LogLevel { LOG_TRACE = 1, /*!< lowest level, execution tracing */ LOG_DEBUG = 2, /*!< debugging messages */ @@ -53,65 +53,74 @@ enum LogType */ class CLogger : public CSingleton<CLogger> { - public: - CLogger(); - ~CLogger(); - - /** Write message to console or file - * \param str - message to write - * \param ... - additional arguments - */ - void Message(const char *str, ...); - - /** Write message to console or file with LOG_TRACE level - * \param str - message to write - * \param ... - additional arguments - */ - void Trace(const char *str, ...); - - /** Write message to console or file with LOG_DEBUG level - * \param str - message to write - * \param ... - additional arguments - */ - void Debug(const char *str, ...); - - /** Write message to console or file with LOG_INFO level - * \param str - message to write - * \param ... - additional arguments - */ - void Info(const char *str, ...); - - /** Write message to console or file with LOG_WARN level - * \param str - message to write - * \param ... - additional arguments - */ - void Warn(const char *str, ...); - - /** Write message to console or file with LOG_ERROR level - * \param str - message to write - * \param ... - additional arguments - */ - void Error(const char *str, ...); - - /** Set output file to write logs 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 level - minimum log level to write - */ - void SetLogLevel(LogType level); - - private: - std::string mFilename; - FILE *mFile; - LogType mLogLevel; - - void Open(); - void Close(); - bool IsOpened(); - void Log(LogType type, const char* str, va_list args); +public: + CLogger(); + ~CLogger(); + + /** Write message to console or file + * \param str - message to write + * \param ... - additional arguments + */ + void Message(const char *str, ...); + + /** Write message to console or file with LOG_TRACE level + * \param str - message to write + * \param ... - additional arguments + */ + void Trace(const char *str, ...); + + /** Write message to console or file with LOG_DEBUG level + * \param str - message to write + * \param ... - additional arguments + */ + void Debug(const char *str, ...); + + /** Write message to console or file with LOG_INFO level + * \param str - message to write + * \param ... - additional arguments + */ + void Info(const char *str, ...); + + /** Write message to console or file with LOG_WARN level + * \param str - message to write + * \param ... - additional arguments + */ + void Warn(const char *str, ...); + + /** Write message to console or file with LOG_ERROR level + * \param str - message to write + * \param ... - additional arguments + */ + void Error(const char *str, ...); + + /** Set output file to write logs 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 level - minimum log level to write + */ + void SetLogLevel(LogLevel level); + + /** Parses string as a log level + * \param str string to parse + * \param logLevel result log level + * + * Valid values are "trace", "debug", "info", "warn", "error" and "none". + * On invalid value, returns \c false. + */ + static bool ParseLogLevel(const std::string& str, LogLevel& logLevel); + +private: + std::string mFilename; + FILE *mFile; + LogLevel mLogLevel; + + void Open(); + void Close(); + bool IsOpened(); + void Log(LogLevel type, const char* str, va_list args); }; diff --git a/src/common/misc.cpp b/src/common/misc.cpp index 2bce3b8..b96abca 100644 --- a/src/common/misc.cpp +++ b/src/common/misc.cpp @@ -25,10 +25,6 @@ #include <time.h> -static bool g_bUserDir = false; -static char g_userDir[100] = ""; - - // Returns a non-accented letter. char GetNoAccent(char letter) @@ -234,72 +230,11 @@ void TimeToAscii(time_t time, char *buffer) #endif*/ } - -// Makes a copy of a file. - -bool Xfer(char* src, char* dst) -{ - FILE *fs, *fd; - char *buffer; - int len; - - fs = fopen(src, "rb"); - if ( fs == 0 ) - { - return false; - } - - fd = fopen(dst, "wb"); - if ( fd == 0 ) - { - fclose(fs); - return false; - } - - buffer = static_cast<char*>(malloc(10000)); - - while ( true ) - { - len = fread(buffer, 1, 10000, fs); - if ( len == 0 ) break; - fwrite(buffer, 1, len, fd); - } - - free(buffer); - fclose(fs); - fclose(fd); - return true; -} - -// Copy a file into the temporary folder. - -bool CopyFileToTemp(char* filename) -{ - char src[100]; - char dst[100]; - char save[100]; - - UserDir(src, filename, "textures"); - - strcpy(save, g_userDir); - strcpy(g_userDir, "temp"); - UserDir(dst, filename, "textures"); - strcpy(g_userDir, save); - - //_mkdir("temp"); - system("mkdir temp"); - - if ( !Xfer(src, dst) ) return false; - - strcpy(filename, dst); - return true; -} - // Copy a list of numbered files into the temporary folder. bool CopyFileListToTemp(char* filename, int* list, int total) { - char name[100]; + /*char name[100]; char ext[10]; char file[100]; char save[100]; @@ -329,8 +264,8 @@ bool CopyFileListToTemp(char* filename, int* list, int total) UserDir(file, filename, "textures"); strcpy(filename, file); strcpy(g_userDir, save); - - return true; +*/ + return false; } @@ -342,56 +277,3 @@ void AddExt(char* filename, const char* ext) strcat(filename, ext); } - -// Specifies the user folder. - -void UserDir(bool bUser, const char* dir) -{ - g_bUserDir = bUser; - strcpy(g_userDir, dir); -} - -// Replaces the string %user% by the user folder. -// in: dir = "%user%toto.txt" -// def = "abc\" -// out: buffer = "abc\toto.txt" - -void UserDir(char* buffer, const char* dir, const char* def) -{ - char ddir[100]; - const char* add; - - if ( strstr(dir, "\\") == 0 && def[0] != 0 ) - { - sprintf(ddir, "%s\\%s", def, dir); - } - else - { - strcpy(ddir, dir); - } - dir = ddir; - - while ( *dir != 0 ) - { - if ( dir[0] == '%' && - dir[1] == 'u' && - dir[2] == 's' && - dir[3] == 'e' && - dir[4] == 'r' && - dir[5] == '%' ) // %user% ? - { - if ( g_bUserDir ) add = g_userDir; - else add = def; - - while ( *add != 0 ) - { - *buffer++ = *add++; - } - dir += 6; // jumps to %user% - continue; - } - - *buffer++ = *dir++; - } - *buffer = 0; -} diff --git a/src/common/misc.h b/src/common/misc.h index f210706..e2ddc44 100644 --- a/src/common/misc.h +++ b/src/common/misc.h @@ -29,8 +29,5 @@ extern char GetToLower(char letter); extern void TimeToAscii(time_t time, char *buffer); -extern bool CopyFileToTemp(char* filename); extern bool CopyFileListToTemp(char* filename, int* list, int total); extern void AddExt(char* filename, const char* ext); -extern void UserDir(bool bUser, const char* dir); -extern void UserDir(char* buffer, const char* dir, const char* def); diff --git a/src/common/profile.cpp b/src/common/profile.cpp index 5432489..654648d 100644 --- a/src/common/profile.cpp +++ b/src/common/profile.cpp @@ -19,13 +19,15 @@ #include "common/logger.h" +#include "app/system.h" + #include <utility> #include <cstring> #include <boost/property_tree/ini_parser.hpp> #include <boost/regex.hpp> -template<> CProfile* CSingleton<CProfile>::mInstance = nullptr; +template<> CProfile* CSingleton<CProfile>::m_instance = nullptr; namespace bp = boost::property_tree; @@ -41,7 +43,7 @@ CProfile::~CProfile() { try { - bp::ini_parser::write_ini("colobot.ini", m_propertyTree); + bp::ini_parser::write_ini(GetSystemUtils()->profileFileLocation(), m_propertyTree); } catch (std::exception & e) { @@ -55,7 +57,7 @@ bool CProfile::InitCurrentDirectory() { try { - bp::ini_parser::read_ini("colobot.ini", m_propertyTree); + bp::ini_parser::read_ini(GetSystemUtils()->profileFileLocation(), m_propertyTree); } catch (std::exception & e) { @@ -182,3 +184,48 @@ std::vector< std::string > CProfile::GetLocalProfileSection(std::string section, return ret_list; } + + +void CProfile::SetUserDir(std::string dir) +{ + m_userDirectory = dir; +} + + +std::string CProfile::GetUserBasedPath(std::string dir, std::string default_dir) +{ + std::string path = dir; + boost::replace_all(path, "\\", "/"); + if (dir.find("/") == std::string::npos) { + path = default_dir + "/" + dir; + } + + if (m_userDirectory.length() > 0) { + boost::replace_all(path, "%user%", m_userDirectory); + } else { + boost::replace_all(path, "%user%", default_dir); + } + + return fs::path(path).make_preferred().string(); +} + + +bool CProfile::CopyFileToTemp(std::string filename) +{ + std::string src, dst; + std::string tmp_user_dir = m_userDirectory; + + src = GetUserBasedPath(filename, "textures"); + SetUserDir("temp"); + dst = GetUserBasedPath(filename, "textures"); + SetUserDir(tmp_user_dir); + + fs::create_directory(fs::path(dst).parent_path().make_preferred().string()); + fs::copy_file(src, dst, fs::copy_option::overwrite_if_exists); + if (fs::exists(dst)) { + return true; + } + + return false; +} + diff --git a/src/common/profile.h b/src/common/profile.h index 9bc6c37..7f99d81 100644 --- a/src/common/profile.h +++ b/src/common/profile.h @@ -21,14 +21,17 @@ #pragma once - #include "common/singleton.h" #include <boost/property_tree/ptree.hpp> +#include <boost/filesystem.hpp> +#include <boost/algorithm/string/replace.hpp> #include <string> #include <vector> +namespace fs = boost::filesystem; + /** * \class CProfile @@ -101,10 +104,30 @@ class CProfile : public CSingleton<CProfile> * \return vector of values */ std::vector< std::string > GetLocalProfileSection(std::string section, std::string key); + + /** Sets current user directory + * \param dir + */ + void SetUserDir(std::string dir); + + /** Returns path based on current user. Replaces %user% in path with current user dir or + * uses default_dir param if no user dir is specified + * \param dir + * \param default_dir + * \return path + */ + std::string GetUserBasedPath(std::string dir, std::string default_dir); + + /** opy a file into the temporary folder. + * \param filename + * \return true on success + */ + bool CopyFileToTemp(std::string filename); private: boost::property_tree::ptree m_propertyTree; bool m_profileNeedSave; + std::string m_userDirectory; }; //! Global function to get profile instance diff --git a/src/common/restext.cpp b/src/common/restext.cpp index 4c56ae5..729a883 100644 --- a/src/common/restext.cpp +++ b/src/common/restext.cpp @@ -30,7 +30,7 @@ #include "object/robotmain.h" #include <libintl.h> -#include <SDL/SDL_keyboard.h> +#include <SDL_keyboard.h> const char* stringsText[RT_MAX] = { nullptr }; const char* stringsEvent[EVENT_STD_MAX] = { nullptr }; @@ -308,6 +308,7 @@ void InitializeRestext() stringsEvent[EVENT_OBJECT_BNUCLEAR] = "Build a nuclear power plant"; stringsEvent[EVENT_OBJECT_BPARA] = "Build a lightning conductor"; stringsEvent[EVENT_OBJECT_BINFO] = "Build a exchange post"; + stringsEvent[EVENT_OBJECT_BDESTROYER] = "Build a destroyer"; stringsEvent[EVENT_OBJECT_GFLAT] = "Show if the ground is flat"; stringsEvent[EVENT_OBJECT_FCREATE] = "Plant a flag"; stringsEvent[EVENT_OBJECT_FDELETE] = "Remove a flag"; @@ -351,6 +352,7 @@ void InitializeRestext() stringsEvent[EVENT_OBJECT_SEARCH] = "Sniff (\\key action;)"; stringsEvent[EVENT_OBJECT_TERRAFORM] = "Thump (\\key action;)"; stringsEvent[EVENT_OBJECT_FIRE] = "Shoot (\\key action;)"; + stringsEvent[EVENT_OBJECT_SPIDEREXPLO] = "Explode (\\key action;)"; stringsEvent[EVENT_OBJECT_RECOVER] = "Recycle (\\key action;)"; stringsEvent[EVENT_OBJECT_BEGSHIELD] = "Extend shield (\\key action;)"; stringsEvent[EVENT_OBJECT_ENDSHIELD] = "Withdraw shield (\\key action;)"; diff --git a/src/common/singleton.h b/src/common/singleton.h index c1b28d9..25e1648 100644 --- a/src/common/singleton.h +++ b/src/common/singleton.h @@ -26,34 +26,51 @@ template<typename T> class CSingleton { - protected: - static T* mInstance; - - public: - static T& GetInstance() { - assert(mInstance != nullptr); - return *mInstance; - } - - static T* GetInstancePointer() { - assert(mInstance != nullptr); - return mInstance; - } - - static bool IsCreated() { - return mInstance != nullptr; - } - - CSingleton() { - assert(mInstance == nullptr); - mInstance = static_cast<T *>(this); - } - - virtual ~CSingleton() { - mInstance = nullptr; - } - - private: - CSingleton& operator=(const CSingleton<T> &); - CSingleton(const CSingleton<T> &); +protected: + static T* m_instance; + +public: + static T& GetInstance() + { + assert(m_instance != nullptr); + return *m_instance; + } + + static T* GetInstancePointer() + { + assert(m_instance != nullptr); + return m_instance; + } + + static bool IsCreated() + { + return m_instance != nullptr; + } + + CSingleton() + { + assert(m_instance == nullptr); + m_instance = static_cast<T *>(this); + } + + virtual ~CSingleton() + { + m_instance = nullptr; + } + + #ifdef TESTS + static void ReplaceInstance(T* newInstance) + { + assert(newInstance != nullptr); + + if (m_instance != nullptr) + delete m_instance; + + m_instance = newInstance; + } + #endif + +private: + CSingleton& operator=(const CSingleton<T> &); + CSingleton(const CSingleton<T> &); }; diff --git a/src/common/test/CMakeLists.txt b/src/common/test/CMakeLists.txt deleted file mode 100644 index 70dac1f..0000000 --- a/src/common/test/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE debug) -endif(NOT CMAKE_BUILD_TYPE) -set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") - -include_directories( -. -../.. -../../.. -${GTEST_INCLUDE_DIR} -) - - -add_executable(image_test ../image.cpp image_test.cpp) -target_link_libraries(image_test ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${PNG_LIBRARIES}) - -#add_executable(profile_test ../profile.cpp ../logger.cpp profile_test.cpp) -#target_link_libraries(profile_test gtest ${Boost_LIBRARIES}) - -#add_test(profile_test ./profile_test) diff --git a/src/common/test/colobot.ini b/src/common/test/colobot.ini deleted file mode 100644 index 2ca37ee..0000000 --- a/src/common/test/colobot.ini +++ /dev/null @@ -1,15 +0,0 @@ -[test_float] -float_value=1.5 - -[test_string] -string_value=Hello world - -[test_int] -int_value=42 - -[test_multi] -entry1=1 -entry2=2 -entry3=3 -entry4=4 -entry5=5 diff --git a/src/common/test/image_test.cpp b/src/common/test/image_test.cpp deleted file mode 100644 index a98c9cc..0000000 --- a/src/common/test/image_test.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "../image.h" - -#include <SDL/SDL.h> -#include <stdio.h> - -/* For now, just a simple test: loading a file from image - * and saving it to another in PNG. */ - -int main(int argc, char *argv[]) -{ - if (argc != 3) - { - printf("Usage: %s in_image out_image\n", argv[0]); - return 0; - } - - CImage image; - - if (! image.Load(argv[1])) - { - std::string err = image.GetError(); - printf("Error loading '%s': %s\n", err.c_str()); - return 1; - } - Gfx::Color color; - std::string str; - - color = image.GetPixel(Math::IntPoint(0, 0)); - str = color.ToString(); - printf("pixel @ (0,0): %s\n", str.c_str()); - - color = image.GetPixel(Math::IntPoint(0, 1)); - str = color.ToString(); - printf("pixel @ (0,1): %s\n", str.c_str()); - - color = image.GetPixel(Math::IntPoint(1, 0)); - str = color.ToString(); - printf("pixel @ (1,0): %s\n", str.c_str()); - - color = image.GetPixel(Math::IntPoint(1, 1)); - str = color.ToString(); - printf("pixel @ (1,1): %s\n", str.c_str()); - - image.SetPixel(Math::IntPoint(0, 0), Gfx::Color(0.1f, 0.2f, 0.3f, 0.0f)); - image.SetPixel(Math::IntPoint(1, 0), Gfx::Color(0.3f, 0.2f, 0.1f, 1.0f)); - image.SetPixel(Math::IntPoint(0, 1), Gfx::Color(1.0f, 1.0f, 1.0f, 1.0f)); - image.SetPixel(Math::IntPoint(1, 1), Gfx::Color(0.0f, 0.0f, 0.0f, 1.0f)); - - if (! image.SavePNG(argv[2])) - { - std::string err = image.GetError(); - printf("Error saving PNG '%s': %s\n", err.c_str()); - return 2; - } - - return 0; -} diff --git a/src/common/test/profile_test.cpp b/src/common/test/profile_test.cpp deleted file mode 100644 index 6236083..0000000 --- a/src/common/test/profile_test.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "../profile.h" -#include "../logger.h" - -#include <iostream> -#include <string> -#include <vector> -#include <gtest/gtest.h> - - -class CProfileTest : public testing::Test -{ -protected: - CLogger m_logger; - CProfile m_profile; - -}; - -TEST_F(CProfileTest, ReadTest) -{ - ASSERT_TRUE(m_profile.InitCurrentDirectory()); // load colobot.ini file - - std::string result; - ASSERT_TRUE(m_profile.GetLocalProfileString("test_string", "string_value", result)); - ASSERT_STREQ("Hello world", result.c_str()); - - int int_value; - ASSERT_TRUE(m_profile.GetLocalProfileInt("test_int", "int_value", int_value)); - ASSERT_EQ(42, int_value); - - float float_value; - ASSERT_TRUE(m_profile.GetLocalProfileFloat("test_float", "float_value", float_value)); - ASSERT_FLOAT_EQ(1.5, float_value); - - std::vector<std::string> list; - list = m_profile.GetLocalProfileSection("test_multi", "entry"); - ASSERT_EQ(5u, list.size()); -} - -int main(int argc, char *argv[]) -{ - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} - |