From 01e3403abf86050e4096ecf60466de4139ac78e2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 7 Nov 2021 08:25:36 +0100 Subject: Fix text rendering getting corrupt at copy/move constructor --- depends/mgl | 2 +- include/mglpp/graphics/Text.hpp | 3 +++ include/mglpp/window/Event.hpp | 2 ++ src/graphics/Text.cpp | 24 +++++++++++++++++++++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/depends/mgl b/depends/mgl index 2143364..16b0ce3 160000 --- a/depends/mgl +++ b/depends/mgl @@ -1 +1 @@ -Subproject commit 214336492da0d184d5ad4ac64c31920954c5f7e7 +Subproject commit 16b0ce3748f1b3ea788bbaf4caaeb342a8f58d6f diff --git a/include/mglpp/graphics/Text.hpp b/include/mglpp/graphics/Text.hpp index 6166087..648ecac 100644 --- a/include/mglpp/graphics/Text.hpp +++ b/include/mglpp/graphics/Text.hpp @@ -17,6 +17,8 @@ namespace mgl { Text(std::string str, Font &font); Text(std::string str, vec2f position, Font &font); Text(const Text &other); + Text& operator=(const Text &other); + Text(Text &&other); ~Text(); void set_position(vec2f position) override; @@ -26,6 +28,7 @@ namespace mgl { FloatRect get_bounds() const; void set_string(std::string str); const std::string& get_string() const; + void append_string(const std::string &str); // Returns the visual position of a character from its index. // If the index is out of range, then the position of the end of the string is returned. diff --git a/include/mglpp/window/Event.hpp b/include/mglpp/window/Event.hpp index 5b7da45..5b9e357 100644 --- a/include/mglpp/window/Event.hpp +++ b/include/mglpp/window/Event.hpp @@ -17,6 +17,8 @@ namespace mgl { struct TextEvent { uint32_t codepoint; + int size; + char str[5]; /* utf8, null terminated */ }; struct KeyEvent { diff --git a/src/graphics/Text.cpp b/src/graphics/Text.cpp index 49829e6..953833b 100644 --- a/src/graphics/Text.cpp +++ b/src/graphics/Text.cpp @@ -18,9 +18,26 @@ namespace mgl { } Text::Text(const Text &other) { + *this = other; + } + + Text& Text::operator=(const Text &other) { font = other.font; - mgl_text_init(&text, font ? font->internal_font() : nullptr, other.str.c_str(), other.str.size()); + str = other.str; + mgl_text_init(&text, font ? font->internal_font() : nullptr, str.c_str(), str.size()); mgl_text_set_position(&text, { other.text.position.x, other.text.position.y }); + return *this; + } + + Text::Text(Text &&other) { + font = std::move(other.font); + str = std::move(other.str); + text = std::move(other.text); + mgl_text_init(&text, font ? font->internal_font() : nullptr, str.c_str(), str.size()); + + other.font = nullptr; + other.str.clear(); + mgl_text_deinit(&other.text); } Text::~Text() { @@ -54,6 +71,11 @@ namespace mgl { return str; } + void Text::append_string(const std::string &str) { + this->str += str; + mgl_text_set_string(&text, this->str.c_str(), this->str.size()); + } + // TODO: Implement vec2f Text::find_character_pos(size_t index) { return vec2f(); -- cgit v1.2.3