diff --git a/src/sago/GameStateInterface.hpp b/src/sago/GameStateInterface.hpp index 2e86782..3cb0067 100644 --- a/src/sago/GameStateInterface.hpp +++ b/src/sago/GameStateInterface.hpp @@ -36,17 +36,17 @@ public: * @return true if active */ virtual bool IsActive() = 0; - + /** * Tells the state to draw itself to target * @param target The RenderWindow to draw to */ virtual void Draw(SDL_Renderer* target) = 0; - - virtual void ProcessInput(const SDL_Event& event, bool& processed) = 0; - + + virtual void ProcessInput(const SDL_Event& event, bool &processed) = 0; + virtual void Update() {} - + virtual ~GameStateInterface() {} }; diff --git a/src/sago/SagoDataHolder.cpp b/src/sago/SagoDataHolder.cpp index d88f940..9fc5a5f 100644 --- a/src/sago/SagoDataHolder.cpp +++ b/src/sago/SagoDataHolder.cpp @@ -31,6 +31,12 @@ SOFTWARE. #include #include #include "SagoMiscSdl2.hpp" +#include "SagoMisc.hpp" + +#if PHYSFS_VER_MAJOR < 3 +#define PHYSFS_readBytes(X,Y,Z) PHYSFS_read(X,Y,1,Z) +#define PHYSFS_writeBytes(X,Y,Z) PHYSFS_write(X,Y,1,Z) +#endif namespace sago { @@ -110,21 +116,13 @@ SDL_Texture* SagoDataHolder::getTexturePtr(const std::string& textureName) const if (!PHYSFS_exists(path.c_str())) { sago::SagoFatalErrorF("getTextureFailed - Texture does not exist: %s", path.c_str()); } - PHYSFS_file* myfile = PHYSFS_openRead(path.c_str()); - unsigned int m_size = PHYSFS_fileLength(myfile); - std::unique_ptr m_data(new char[m_size]); - int length_read = PHYSFS_read (myfile, m_data.get(), 1, m_size); - if (length_read != (int)m_size) { - PHYSFS_close(myfile); - std::cerr << "Error: Curropt data file: " << path << "\n"; - return ret; - } - PHYSFS_close(myfile); + unsigned int m_size = 0; + std::unique_ptr m_data; + ReadBytesFromFile(path.c_str(), m_data, m_size); SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size); //The above might fail an return null. if (!rw) { - PHYSFS_close(myfile); - std::cerr << "Error. Curropt data file!\n"; + std::cerr << "Error. Corrupt data file!\n"; return NULL; } SDL_Surface* surface = IMG_Load_RW(rw,true); @@ -152,23 +150,15 @@ TTF_Font* SagoDataHolder::getFontPtr(const std::string& fontName, int ptsize) co std::cerr << "getFontPtr - Font does not exists: " << path << "\n"; return ret; } - PHYSFS_file* myfile = PHYSFS_openRead(path.c_str()); - unsigned int m_size = PHYSFS_fileLength(myfile); - std::unique_ptr m_data(new char[m_size]); - int length_read = PHYSFS_read (myfile, m_data.get(), 1, m_size); - if (length_read != (int)m_size) { - PHYSFS_close(myfile); - std::cerr << "Error: Curropt data file: " << path << "\n"; - return ret; - } - PHYSFS_close(myfile); + unsigned int m_size = 0; + std::unique_ptr m_data; + ReadBytesFromFile(path.c_str(), m_data, m_size); SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size); //The above might fail an return null. if (!rw) { - PHYSFS_close(myfile); - std::cerr << "Error: Curropt data file!\n"; + std::cerr << "Error: Corrupt data file!\n"; return ret; } @@ -195,22 +185,14 @@ Mix_Music* SagoDataHolder::getMusicPtr(const std::string& musicName) const { std::cerr << "getMusicPtr - Music file does not exists: " << path << "\n"; return ret; } - PHYSFS_file* myfile = PHYSFS_openRead(path.c_str()); - unsigned int m_size = PHYSFS_fileLength(myfile); - std::unique_ptr m_data(new char[m_size]); - int length_read = PHYSFS_read (myfile, m_data.get(), 1, m_size); - if (length_read != (int)m_size) { - PHYSFS_close(myfile); - std::cerr << "Error: Curropt data file: " << path << "\n"; - return ret; - } - PHYSFS_close(myfile); + unsigned int m_size = 0; + std::unique_ptr m_data; + ReadBytesFromFile(path.c_str(), m_data, m_size); SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size); //The above might fail an return null. if (!rw) { - PHYSFS_close(myfile); - std::cerr << "Error. Curropt data file!\n"; + std::cerr << "Error. Corrupt data file!\n"; return NULL; } @@ -238,22 +220,14 @@ Mix_Chunk* SagoDataHolder::getSoundPtr(const std::string& soundName) const { std::cerr << "getSoundPtr - Sound file does not exists: " << path << "\n"; return ret; } - PHYSFS_file* myfile = PHYSFS_openRead(path.c_str()); - unsigned int m_size = PHYSFS_fileLength(myfile); - std::unique_ptr m_data(new char[m_size]); - int length_read = PHYSFS_read (myfile, m_data.get(), 1, m_size); - if (length_read != (int)m_size) { - PHYSFS_close(myfile); - std::cerr << "Error: Curropt data file: " << path << "\n"; - return ret; - } - PHYSFS_close(myfile); + unsigned int m_size = 0; + std::unique_ptr m_data; + ReadBytesFromFile(path.c_str(), m_data, m_size); SDL_RWops* rw = SDL_RWFromMem (m_data.get(), m_size); //The above might fail an return null. if (!rw) { - PHYSFS_close(myfile); - std::cerr << "Error. Curropt data file!\n"; + std::cerr << "Error. Corrupt data file!\n"; return NULL; } @@ -271,7 +245,7 @@ Uint64 SagoDataHolder::getVersion() const { return data->version; } -TextureHandler::TextureHandler(const SagoDataHolder* holder, const std::string& textureName) { +TextureHandler::TextureHandler(const SagoDataHolder* holder, const std::string &textureName) { this->holder = holder; this->version = 0; this->textureName = textureName; @@ -318,15 +292,15 @@ Mix_Chunk* SoundHandler::get() { } -TextureHandler SagoDataHolder::getTextureHandler(const std::string& textureName) const { +TextureHandler SagoDataHolder::getTextureHandler(const std::string &textureName) const { return TextureHandler(this, textureName); } -MusicHandler SagoDataHolder::getMusicHandler(const std::string& musicName) const { +MusicHandler SagoDataHolder::getMusicHandler(const std::string &musicName) const { return MusicHandler(this, musicName); } -SoundHandler SagoDataHolder::getSoundHandler(const std::string& soundName) const { +SoundHandler SagoDataHolder::getSoundHandler(const std::string &soundName) const { return SoundHandler(this, soundName); } diff --git a/src/sago/SagoDataHolder.hpp b/src/sago/SagoDataHolder.hpp index 373d12c..3c126aa 100644 --- a/src/sago/SagoDataHolder.hpp +++ b/src/sago/SagoDataHolder.hpp @@ -30,7 +30,7 @@ SOFTWARE. #include #ifndef TEXTUREHOLDER_HPP -#define TEXTUREHOLDER_HPP +#define TEXTUREHOLDER_HPP namespace sago { @@ -39,7 +39,7 @@ class SagoDataHolder; class TextureHandler { public: TextureHandler() {}; - TextureHandler(const SagoDataHolder* holder, const std::string& textureName); + TextureHandler(const SagoDataHolder* holder, const std::string &textureName); SDL_Texture* get(); private: std::string textureName; @@ -52,7 +52,7 @@ private: class MusicHandler final { public: MusicHandler() {}; - MusicHandler(const SagoDataHolder* holder, const std::string& musicName); + MusicHandler(const SagoDataHolder* holder, const std::string &musicName); Mix_Music* get(); private: std::string musicName; @@ -65,7 +65,7 @@ private: class SoundHandler final { public: SoundHandler() {}; - SoundHandler(const SagoDataHolder* holder, const std::string& soundName); + SoundHandler(const SagoDataHolder* holder, const std::string &soundName); Mix_Chunk* get(); private: std::string soundName; @@ -87,13 +87,13 @@ public: * @param textureName Name of the texture * @return Pointer to the loaded texture */ - SDL_Texture* getTexturePtr(const std::string& textureName) const; - TextureHandler getTextureHandler(const std::string& textureName) const; - TTF_Font* getFontPtr(const std::string& fontName, int ptsize) const; - Mix_Music* getMusicPtr(const std::string& musicName) const; - MusicHandler getMusicHandler(const std::string& musicName) const; - Mix_Chunk* getSoundPtr(const std::string& soundName) const; - SoundHandler getSoundHandler(const std::string& soundName) const; + SDL_Texture* getTexturePtr(const std::string &textureName) const; + TextureHandler getTextureHandler(const std::string &textureName) const; + TTF_Font* getFontPtr(const std::string &fontName, int ptsize) const; + Mix_Music* getMusicPtr(const std::string &musicName) const; + MusicHandler getMusicHandler(const std::string &musicName) const; + Mix_Chunk* getSoundPtr(const std::string &soundName) const; + SoundHandler getSoundHandler(const std::string &soundName) const; void setVerbose(bool value); /** * Invalidates all pointers returned by any of the get variables @@ -117,10 +117,10 @@ private: SagoDataHolder(const SagoDataHolder& base) = delete; SagoDataHolder& operator=(const SagoDataHolder& base) = delete; struct SagoDataHolderData; - mutable SagoDataHolderData* data; + mutable SagoDataHolderData *data; }; } //namespace sago -#endif /* TEXTUREHOLDER_HPP */ +#endif /* TEXTUREHOLDER_HPP */ diff --git a/src/sago/SagoMisc.cpp b/src/sago/SagoMisc.cpp index 3a2230b..6942cd1 100644 --- a/src/sago/SagoMisc.cpp +++ b/src/sago/SagoMisc.cpp @@ -27,7 +27,11 @@ SOFTWARE. #include #include #include -#include + +#if PHYSFS_VER_MAJOR < 3 +#define PHYSFS_readBytes(X,Y,Z) PHYSFS_read(X,Y,1,Z) +#define PHYSFS_writeBytes(X,Y,Z) PHYSFS_write(X,Y,1,Z) +#endif using std::string; using std::cerr; @@ -50,22 +54,35 @@ bool FileExists(const char* filename) { return PHYSFS_exists(filename); } -std::string GetFileContent(const char* filename) { - string ret; +void ReadBytesFromFile(const char* filename, std::unique_ptr& dest, unsigned int& bytes) { + bytes = 0; if (!PHYSFS_exists(filename)) { - cerr << "GetFileContent - File does not exists: " << filename << "\n"; - return ret; + cerr << "ReadBytesFromFile - File does not exists: " << filename << "\n"; + return; } PHYSFS_file* myfile = PHYSFS_openRead(filename); unsigned int m_size = PHYSFS_fileLength(myfile); std::unique_ptr m_data(new char[m_size]); - int length_read = PHYSFS_read (myfile, m_data.get(), 1, m_size); + int length_read = PHYSFS_readBytes (myfile, m_data.get(), m_size); if (length_read != (int)m_size) { PHYSFS_close(myfile); cerr << "Error: Curropt data file: " << filename << "\n"; - return ret; + return; } PHYSFS_close(myfile); + std::swap(m_data, dest); + bytes = m_size; +} + +std::string GetFileContent(const char* filename) { + string ret; + if (!PHYSFS_exists(filename)) { + cerr << "GetFileContent - File does not exists: " << filename << "\n"; + return ret; + } + unsigned int m_size = 0; + std::unique_ptr m_data; + ReadBytesFromFile(filename, m_data, m_size); //Now create a std::string ret = string(m_data.get(), m_data.get()+m_size); return ret; @@ -85,10 +102,15 @@ void WriteFileContent(const char* filename, const std::string& content) { CreatePathToFile(filename); PHYSFS_file* myfile = PHYSFS_openWrite(filename); if (!myfile) { - cerr << "Failed to open file for writing, " << PHYSFS_getLastError() << "\n"; +#if PHYSFS_VER_MAJOR > 2 + PHYSFS_ErrorCode code = PHYSFS_getLastErrorCode(); + std::cerr << "Failed to open file for writing, " << PHYSFS_getErrorByCode(code) << " (" << code << ")\n"; +#else + std::cerr << "Failed to open file for writing, " << PHYSFS_getLastError() << "\n"; +#endif return; } - PHYSFS_write(myfile, content.c_str(), sizeof(char), content.length()); + PHYSFS_writeBytes(myfile, content.c_str(), sizeof(char)*content.length()); PHYSFS_close(myfile); } diff --git a/src/sago/SagoMisc.hpp b/src/sago/SagoMisc.hpp index c85de52..347bc75 100644 --- a/src/sago/SagoMisc.hpp +++ b/src/sago/SagoMisc.hpp @@ -23,52 +23,61 @@ SOFTWARE. */ #ifndef SAGOMISC_HPP -#define SAGOMISC_HPP +#define SAGOMISC_HPP #include #include +#include namespace sago { -/** - * Returns a vector with all filenames in a given directory. - * PHYSFS must be setup before hand. The directory is relative to the PHYSFS base - * @param dir The directory to list - * @return A vector with the filenames in the given directory. If empty the directory was empty or did not exist - */ -std::vector GetFileList(const char* dir); - -/** - * Reads an entire file into memory. - * PHYSFS must be setup before hand - * @param filename The file to read - * @return The content of the file. If empty either the file was empty, did not exist or could not be opened - */ -std::string GetFileContent(const char* filename); - -/** - * Reads an entire file into memory. - * PHYSFS must be setup before hand - * @param filename The file to read - * @return The content of the file. If empty either the file was empty, did not exist or could not be opened - */ -inline std::string GetFileContent(const std::string& filename) { - return GetFileContent(filename.c_str()); -}; - -bool FileExists(const char* filename); - -void WriteFileContent(const char* filename, const std::string& content); - -/** - * This functions converts a string on a best effort basis - * Unlike atol this does NOT cause undefined behavior if out of range - * @param c_string A string that may contain a number - * @return A number between LONG_MIN and LONG_MAX (both inclusive) - */ -long int StrToLong(const char* c_string); + /** + * Returns a vector with all filenames in a given directory. + * PHYSFS must be setup before hand. The directory is relative to the PHYSFS base + * @param dir The directory to list + * @return A vector with the filenames in the given directory. If empty the directory was empty or did not exist + */ + std::vector GetFileList(const char* dir); + + /** + * Reads an entire file into memory. + * PHYSFS must be setup before hand + * @param filename The file to read + * @param dest The unique pointer in which the bytes will be written + * @param bytes Number of bytes written + * @return The content of the file. If empty either the file was empty, did not exist or could not be opened + */ + void ReadBytesFromFile(const char* filename, std::unique_ptr& dest, unsigned int& bytes); + + /** + * Reads an entire file into memory. + * PHYSFS must be setup before hand + * @param filename The file to read + * @return The content of the file. If empty either the file was empty, did not exist or could not be opened + */ + std::string GetFileContent(const char* filename); + + /** + * Reads an entire file into memory. + * PHYSFS must be setup before hand + * @param filename The file to read + * @return The content of the file. If empty either the file was empty, did not exist or could not be opened + */ + inline std::string GetFileContent(const std::string& filename) { return GetFileContent(filename.c_str()); }; + + bool FileExists(const char* filename); + + void WriteFileContent(const char* filename, const std::string& content); + + /** + * This functions converts a string on a best effort basis + * Unlike atol this does NOT cause undefined behavior if out of range + * @param c_string A string that may contain a number + * @return A number between LONG_MIN and LONG_MAX (both inclusive) + */ + long int StrToLong(const char* c_string); } //namespace sago -#endif /* SAGOMISC_HPP */ +#endif /* SAGOMISC_HPP */ diff --git a/src/sago/SagoMiscSdl2.hpp b/src/sago/SagoMiscSdl2.hpp index c2d700b..1bc115b 100644 --- a/src/sago/SagoMiscSdl2.hpp +++ b/src/sago/SagoMiscSdl2.hpp @@ -26,20 +26,20 @@ SOFTWARE. #define SAGOMISCSDL2_HPP namespace sago { - -/** - * Writes an error message to the screen and aborts the program - * @param errorMsg The message displayed in a pop-up box to the user. - */ -void SagoFatalError(const char* errorMsg); - -/** - * Writes an error message to the screen and aborts the program - * @param fmt A printf-style format string - * @param ... Parameters to the format string - */ -void SagoFatalErrorF(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); - + + /** + * Writes an error message to the screen and aborts the program + * @param errorMsg The message displayed in a pop-up box to the user. + */ + void SagoFatalError(const char* errorMsg); + + /** + * Writes an error message to the screen and aborts the program + * @param fmt A printf-style format string + * @param ... Parameters to the format string + */ + void SagoFatalErrorF(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); + } #endif /* SAGOMISCSDL2_HPP */ diff --git a/src/sago/SagoSprite.hpp b/src/sago/SagoSprite.hpp index 63a2feb..688770a 100644 --- a/src/sago/SagoSprite.hpp +++ b/src/sago/SagoSprite.hpp @@ -23,7 +23,7 @@ SOFTWARE. */ #ifndef SAGOSPRITE_HPP -#define SAGOSPRITE_HPP +#define SAGOSPRITE_HPP #include "SagoDataHolder.hpp" @@ -32,7 +32,7 @@ namespace sago { class SagoSprite final { public: SagoSprite(); - SagoSprite(const SagoDataHolder& texHolder, const std::string& texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength); + SagoSprite(const SagoDataHolder &texHolder, const std::string &texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength); /** * Draws the sprite to a given render window * @param target The render window to draw on @@ -47,7 +47,7 @@ public: * @param frameTime The time in milliseonds since gamestart. Used to determen the place in the animation * @param x Place to draw the sprite * @param y Place to draw the sprite - * @param part the part of the sprite that should be drawn. + * @param part the part of the sprite that should be drawn. */ void Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part) const; /** @@ -59,17 +59,17 @@ public: * @param bounds A recagular area that we must not draw outside. */ void DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const;/** - * Draws the sprite to a given render window - * @param target The render window to draw on - * @param progress A float with value from 0.0f to 1.0f. Tells how far in the animation that we got - * @param x Place to draw the sprite - * @param y Place to draw the sprite - */ + * Draws the sprite to a given render window + * @param target The render window to draw on + * @param progress A float with value from 0.0f to 1.0f. Tells how far in the animation that we got + * @param x Place to draw the sprite + * @param y Place to draw the sprite + */ void DrawProgressive(SDL_Renderer* target, float progress, int x, int y) const; void DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h) const; /** * Set a different origin. Normally it is the top left cornor. But in some cases you might want to center the origin or tranform it for other reasons - * @param newOrigin the coordinates that should be the new origin. Call with {0,0} to reset to default + * @param newOrigin the coordinates that should be the new origin. Call with {0,0} to reset to default */ void SetOrigin(const SDL_Rect& newOrigin); SagoSprite(const SagoSprite& base); @@ -79,10 +79,10 @@ public: ~SagoSprite(); private: struct SagoSpriteData; - SagoSpriteData* data; + SagoSpriteData *data; }; } -#endif /* SAGOSPRITE_HPP */ +#endif /* SAGOSPRITE_HPP */ diff --git a/src/sago/SagoSpriteHolder.hpp b/src/sago/SagoSpriteHolder.hpp index 8a76dcd..21b093e 100644 --- a/src/sago/SagoSpriteHolder.hpp +++ b/src/sago/SagoSpriteHolder.hpp @@ -23,7 +23,7 @@ SOFTWARE. */ #ifndef SAGOSPRITEHOLDER_HPP -#define SAGOSPRITEHOLDER_HPP +#define SAGOSPRITEHOLDER_HPP #include "SagoDataHolder.hpp" #include "SagoSprite.hpp" @@ -32,20 +32,20 @@ namespace sago { class SagoSpriteHolder final { public: - explicit SagoSpriteHolder(const SagoDataHolder& texHolder); + explicit SagoSpriteHolder(const SagoDataHolder &texHolder); ~SagoSpriteHolder(); void ReadSprites(); - const sago::SagoSprite& GetSprite(const std::string& spritename) const; + const sago::SagoSprite& GetSprite(const std::string &spritename) const; const SagoDataHolder& GetDataHolder() const; private: SagoSpriteHolder(const SagoSpriteHolder& base) = delete; SagoSpriteHolder& operator=(const SagoSpriteHolder& base) = delete; - void ReadSpriteFile(const std::string& filename); + void ReadSpriteFile(const std::string &filename); struct SagoSpriteHolderData; - SagoSpriteHolderData* data; + SagoSpriteHolderData *data; }; } -#endif /* SAGOSPRITEHOLDER_HPP */ +#endif /* SAGOSPRITEHOLDER_HPP */ diff --git a/src/sago/SagoTextBox.cpp b/src/sago/SagoTextBox.cpp index 8ad56fb..2f98a20 100644 --- a/src/sago/SagoTextBox.cpp +++ b/src/sago/SagoTextBox.cpp @@ -161,13 +161,14 @@ void SagoTextBox::UpdateCache() { std::cerr << "FATAL: SagoTextBox::UpdateCache - DataHolder not set!\n"; abort(); } - TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); + TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize); const char delim = '\n'; const std::string& s = data->text; auto start = 0U; auto end = s.find(delim); data->lines.clear(); - while (end != std::string::npos) { + while (end != std::string::npos) + { const std::string& theSubString = s.substr(start, end - start); SplitAndAppendLineToCache(font, theSubString); start = end + 1; @@ -181,7 +182,7 @@ void SagoTextBox::Draw(SDL_Renderer* target, int x, int y, SagoTextField::Alignm if (data->text != data->renderedText) { UpdateCache(); } - TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); + TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize); int lineSkip = TTF_FontLineSkip(font); for (size_t i = 0; i < data->lines.size(); ++i) { data->lines[i].Draw(target, x, y+i*lineSkip, alignment); diff --git a/src/sago/SagoTextBox.hpp b/src/sago/SagoTextBox.hpp index 42c0d81..e0b7096 100644 --- a/src/sago/SagoTextBox.hpp +++ b/src/sago/SagoTextBox.hpp @@ -38,7 +38,7 @@ public: void SetText(const char* text); void SetText(const std::string& text); void SetColor(const SDL_Color& color); - + /** * Set the name of the font. Must be known to the data holder. * The name could for instance be "freeserif". @@ -64,7 +64,7 @@ private: SagoTextBox(const SagoTextBox& orig) = delete; SagoTextBox& operator=(const SagoTextBox& base) = delete; struct SagoTextBoxData; - SagoTextBoxData* data; + SagoTextBoxData *data; }; } //namespace sago diff --git a/src/sago/SagoTextField.cpp b/src/sago/SagoTextField.cpp index 85e63fa..666de12 100644 --- a/src/sago/SagoTextField.cpp +++ b/src/sago/SagoTextField.cpp @@ -28,36 +28,36 @@ SOFTWARE. namespace sago { -class OutlineHandler { - TTF_Font* font; - int originalOutline = 0; - int targetOutline; - bool doChange = false; -public: - OutlineHandler(TTF_Font* font, int outline) : font{font}, targetOutline{outline} { - originalOutline = TTF_GetFontOutline(font); - if (originalOutline == targetOutline) { - return; + class OutlineHandler { + TTF_Font* font; + int originalOutline = 0; + int targetOutline; + bool doChange = false; + public: + OutlineHandler(TTF_Font* font, int outline) : font{font}, targetOutline{outline} { + originalOutline = TTF_GetFontOutline(font); + if (originalOutline == targetOutline) { + return; + } + doChange = true; + TTF_SetFontOutline(font, targetOutline); + }; + + void reset() { + if (doChange) { + TTF_SetFontOutline(font,originalOutline); + doChange = false; + } } - doChange = true; - TTF_SetFontOutline(font, targetOutline); - }; - - void reset() { - if (doChange) { - TTF_SetFontOutline(font,originalOutline); - doChange = false; + + ~OutlineHandler() { + reset(); } - } - - ~OutlineHandler() { - reset(); - } -private: - OutlineHandler(const OutlineHandler& orig) = delete; - OutlineHandler& operator=(const OutlineHandler& base) = delete; -}; - + private: + OutlineHandler(const OutlineHandler& orig) = delete; + OutlineHandler& operator=(const OutlineHandler& base) = delete; + }; + struct SagoTextField::SagoTextFieldData { const sago::SagoDataHolder* tex = nullptr; SDL_Surface* textSurface = nullptr; @@ -73,7 +73,7 @@ struct SagoTextField::SagoTextFieldData { std::string renderedText = ""; Uint64 renderedVersion = 0; }; - + SagoTextField::SagoTextField() { data = new SagoTextFieldData(); } @@ -93,15 +93,14 @@ SagoTextField& SagoTextField::CopyFrom(const SagoTextField& base) { data->textSurface = nullptr; data->texture = nullptr; return *this; - } - catch (...) { + } catch (...) { delete data; throw; } } SagoTextField::~SagoTextField() { - if (!data) { + if(!data) { return; } ClearCache(); @@ -166,7 +165,7 @@ void SagoTextField::UpdateCache(SDL_Renderer* target) { abort(); } ClearCache(); - TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); + TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize); data->textSurface = TTF_RenderUTF8_Blended (font, data->text.c_str(), data->color); data->texture = SDL_CreateTextureFromSurface(target, data->textSurface); if (data->outline > 0) { @@ -180,7 +179,7 @@ void SagoTextField::UpdateCache(SDL_Renderer* target) { } void SagoTextField::GetRenderedSize(const char* text, int* w, int* h) { - TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); + TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize); int ret = TTF_SizeUTF8(font, text, w, h); if (ret) { if (w) { diff --git a/src/sago/SagoTextField.hpp b/src/sago/SagoTextField.hpp index cc434db..902ee16 100644 --- a/src/sago/SagoTextField.hpp +++ b/src/sago/SagoTextField.hpp @@ -33,7 +33,7 @@ namespace sago { * This is a text field. * It represents a line of text to be drawn on screen. It is not possible to have line breaks. * If line breaks are needed use SagoTextBox instead. - * + * * This object renderes to a texture and cahces the texture. The texture will be automatically refreshed if the text changes, the SagoDataHolder is invalidated or ClearCache is called. * Normally all values will be set at the beginning before text is drawn. * SetHolder MUST be called before the field is drawn! @@ -48,7 +48,7 @@ public: /** * This method creates a copy of a given font. * The cache will not be copied. - * This is ALMOST like the "= operator" but given its own name to prevent implicit calling. + * This is ALMOST like the "= operator" but given its own name to prevent implicit calling. * @param base The object to copy from * @return A reference to this object. */ @@ -115,7 +115,7 @@ public: private: SagoTextField(const SagoTextField& orig) = delete; struct SagoTextFieldData; - SagoTextFieldData* data; + SagoTextFieldData *data; }; } //namespace sago diff --git a/src/sago/platform_folders.cpp b/src/sago/platform_folders.cpp index d93db8c..1531e91 100644 --- a/src/sago/platform_folders.cpp +++ b/src/sago/platform_folders.cpp @@ -52,8 +52,16 @@ static std::string getHome() { res = homeEnv; return res; } - struct passwd* pw = getpwuid(uid); - if (!pw) { + struct passwd* pw = nullptr; + struct passwd pwd; + long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); + if (bufsize < 0) { + bufsize = 16384; + } + std::vector buffer; + buffer.resize(bufsize); + int error_code = getpwuid_r(uid, &pwd, buffer.data(), buffer.size(), &pw); + if (error_code) { throw std::runtime_error("Unable to get passwd struct."); } const char* tempRes = pw->pw_dir; @@ -90,7 +98,7 @@ std::string win32_utf16_to_utf8(const wchar_t* wstr) { if (actualSize > 0) { //If the converted UTF-8 string could not be in the initial buffer. Allocate one that can hold it. std::vector buffer(actualSize); - actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], buffer.size(), nullptr, nullptr); + actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], static_cast(buffer.size()), nullptr, nullptr); res = buffer.data(); } if (actualSize == 0) { diff --git a/src/sago/platform_folders.h b/src/sago/platform_folders.h index 646b794..4563410 100644 --- a/src/sago/platform_folders.h +++ b/src/sago/platform_folders.h @@ -39,14 +39,13 @@ namespace sago { #ifndef DOXYGEN_SHOULD_SKIP_THIS namespace internal { - #if !defined(_WIN32) && !defined(__APPLE__) - void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector& folders); - #endif - #ifdef _WIN32 - std::string win32_utf16_to_utf8(const wchar_t* wstr); - #endif +#if !defined(_WIN32) && !defined(__APPLE__) +void appendExtraFoldersTokenizer(const char* envName, const char* envValue, std::vector& folders); +#endif +#ifdef _WIN32 +std::string win32_utf16_to_utf8(const wchar_t* wstr); +#endif } - #endif //DOXYGEN_SHOULD_SKIP_THIS /** @@ -226,7 +225,7 @@ public: */ std::string getPicturesFolder() const; /** - * Use sago::getPublicFolder() instead! + * Use sago::getPublicFolder() instead! */ std::string getPublicFolder() const; /**