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 --- src/graphics/Text.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/graphics') 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