diff --git a/src/sago/SagoSprite.cpp b/src/sago/SagoSprite.cpp index 5456ca4..3d59b20 100644 --- a/src/sago/SagoSprite.cpp +++ b/src/sago/SagoSprite.cpp @@ -29,8 +29,8 @@ namespace sago { struct SagoSprite::SagoSpriteData { TextureHandler tex; - SDL_Rect imgCord{}; - SDL_Rect origin{}; + SDL_Rect imgCord = {}; + SDL_Rect origin = {}; int aniFrames = 0; int aniFrameTime = 0; }; diff --git a/src/sago/SagoSpriteHolder.cpp b/src/sago/SagoSpriteHolder.cpp index 138b3e0..2b24e6a 100644 --- a/src/sago/SagoSpriteHolder.cpp +++ b/src/sago/SagoSpriteHolder.cpp @@ -64,7 +64,7 @@ SagoSpriteHolder::~SagoSpriteHolder() { static int getDefaultValue(const rapidjson::Value& value, const char* name, int defaultValue) { assert(value.IsObject()); const auto& t = value.GetObject().FindMember(name); - if (t->value.IsInt()) { + if (t != value.MemberEnd() && t->value.IsInt()) { return t->value.GetInt(); } return defaultValue; @@ -103,8 +103,12 @@ void SagoSpriteHolder::ReadSpriteFile(const std::string& filename) { int width = getDefaultValue(m.value, "width",0); int number_of_frames = getDefaultValue(m.value, "number_of_frames",1); int frame_time = getDefaultValue(m.value, "frame_time",1); - int originx = getDefaultValue(m.value, "originx",0); - int originy = getDefaultValue(m.value, "originy",0); + SDL_Rect origin = {}; + origin.x = getDefaultValue(m.value, "originx",0); + origin.y = getDefaultValue(m.value, "originy",0); + if (origin.x != 0) { + cerr << "Origin: " << origin.x << ", " << origin.y << "\n"; + } if (number_of_frames < 1) { number_of_frames = 1; } @@ -112,7 +116,7 @@ void SagoSpriteHolder::ReadSpriteFile(const std::string& filename) { frame_time = 1; } std::shared_ptr ptr(new SagoSprite(*(data->tex),textureName, {topx,topy,width,height},number_of_frames,frame_time)); - ptr->SetOrigin({originx,originy, 0, 0}); + ptr->SetOrigin(origin); this->data->sprites[std::string(spriteName)] = ptr; } } diff --git a/src/sago/SagoTextBox.cpp b/src/sago/SagoTextBox.cpp index 1e8243f..01694ac 100644 --- a/src/sago/SagoTextBox.cpp +++ b/src/sago/SagoTextBox.cpp @@ -32,7 +32,7 @@ SOFTWARE. namespace sago { struct SagoTextBox::SagoTextBoxData { - sago::SagoDataHolder* tex = nullptr; + const sago::SagoDataHolder* tex = nullptr; std::string fontName = "freeserif"; SDL_Color color = { 255, 255, 255, 0 }; SDL_Color outlineColor = { 255, 255, 0, 0 }; @@ -43,7 +43,7 @@ struct SagoTextBox::SagoTextBoxData { std::vector lines; int maxWidth = 0; }; - + SagoTextBox::SagoTextBox() { data = new SagoTextBoxData(); } @@ -52,7 +52,7 @@ SagoTextBox::~SagoTextBox() { delete data; } -void SagoTextBox::SetHolder(SagoDataHolder* holder) { +void SagoTextBox::SetHolder(const SagoDataHolder* holder) { data->tex = holder; } @@ -144,6 +144,10 @@ void SagoTextBox::SplitAndAppendLineToCache(TTF_Font* font, const std::string& t } std::string firstPart(text.begin(), splitLocation); AppendLineToCache(firstPart); + while (splitLocation != text.end() && *splitLocation == ' ') { + //Trim spaces after an automatic line break. + ++splitLocation; + } if (splitLocation == text.end()) { return; } @@ -157,6 +161,7 @@ void SagoTextBox::UpdateCache() { const std::string& s = data->text; auto start = 0U; auto end = s.find(delim); + data->lines.clear(); while (end != std::string::npos) { const std::string& theSubString = s.substr(start, end - start); diff --git a/src/sago/SagoTextBox.hpp b/src/sago/SagoTextBox.hpp index a1d4c38..6fe19d9 100644 --- a/src/sago/SagoTextBox.hpp +++ b/src/sago/SagoTextBox.hpp @@ -34,8 +34,8 @@ class SagoTextBox { public: SagoTextBox(); virtual ~SagoTextBox(); - void SetHolder(SagoDataHolder* holder); - void SetText(const char* text); + void SetHolder(const SagoDataHolder* holder); + void SetText(const char* text); void SetColor(const SDL_Color& color); void SetFont(const char* fontName); void SetFontSize(int fontSize); @@ -48,7 +48,7 @@ public: * @param width The maximum width before forcing a line break */ void SetMaxWidth(int width); - const std::string& GetText() const; + const std::string& GetText() const; void Draw(SDL_Renderer* target, int x, int y); void UpdateCache(); private: diff --git a/src/sago/SagoTextField.cpp b/src/sago/SagoTextField.cpp index 2a3641c..831d3f1 100644 --- a/src/sago/SagoTextField.cpp +++ b/src/sago/SagoTextField.cpp @@ -59,7 +59,7 @@ namespace sago { }; struct SagoTextField::SagoTextFieldData { - sago::SagoDataHolder* tex = nullptr; + const sago::SagoDataHolder* tex = nullptr; SDL_Surface* textSurface = nullptr; SDL_Texture* texture = nullptr; SDL_Surface* outlineTextSurface = nullptr; @@ -91,7 +91,7 @@ SagoTextField::~SagoTextField() { delete data; } -void SagoTextField::SetHolder(SagoDataHolder* holder) { +void SagoTextField::SetHolder(const SagoDataHolder* holder) { data->tex = holder; } diff --git a/src/sago/SagoTextField.hpp b/src/sago/SagoTextField.hpp index b725199..ab05c49 100644 --- a/src/sago/SagoTextField.hpp +++ b/src/sago/SagoTextField.hpp @@ -48,12 +48,12 @@ public: * Sets the data holder. This is MANDATORY * @param holder The data holder to fetch the fonts from */ - void SetHolder(SagoDataHolder* holder); + void SetHolder(const SagoDataHolder* holder); /** * Set the text to display. * @param text The actual UTF-8 encoded text */ - void SetText(const char* text); + void SetText(const char* text); void SetColor(const SDL_Color& color); /** * Set the name of the font. Must be known to the data holder. @@ -71,7 +71,7 @@ public: * Get the text we are currently drawing * @return The text */ - const std::string& GetText() const; + const std::string& GetText() const; void Draw(SDL_Renderer* target, int x, int y); /** * Updates the cache.