From ac817c1c55b61387df289773fb3761fee8f462c0 Mon Sep 17 00:00:00 2001 From: TotallyNotElite <1yourexperiment@protonmail.com> Date: Tue, 18 Dec 2018 21:52:07 +0100 Subject: [PATCH] Refactor and deconstruct fix (memory leak if not shutdown correctly) --- include/glez/detail/font.hpp | 8 ++++---- include/glez/font.hpp | 2 +- src/detail/font.cpp | 35 ++++++++++++++++++----------------- src/detail/texture.cpp | 10 ++++------ src/draw.cpp | 4 ++-- src/font.cpp | 2 +- 6 files changed, 30 insertions(+), 31 deletions(-) diff --git a/include/glez/detail/font.hpp b/include/glez/detail/font.hpp index baa786a..ad91a9f 100644 --- a/include/glez/detail/font.hpp +++ b/include/glez/detail/font.hpp @@ -12,7 +12,7 @@ namespace glez::detail::font { -struct font +struct ifont { void load(const std::string &path, float size); void unload(); @@ -20,7 +20,7 @@ struct font bool init{ false }; - texture_font_t *font{ nullptr }; + texture_font_t *m_font{ nullptr }; texture_atlas_t *atlas{ nullptr }; }; @@ -28,5 +28,5 @@ void init(); void shutdown(); unsigned create(); -font &get(unsigned handle); -} // namespace glez::detail::font \ No newline at end of file +ifont &get(unsigned handle); +} // namespace glez::detail::font diff --git a/include/glez/font.hpp b/include/glez/font.hpp index 751146d..c4ee2a1 100644 --- a/include/glez/font.hpp +++ b/include/glez/font.hpp @@ -42,4 +42,4 @@ protected: unsigned handle{ std::numeric_limits::max() }; }; -} // namespace glez \ No newline at end of file +} // namespace glez diff --git a/src/detail/font.cpp b/src/detail/font.cpp index 728baca..bbdf2b4 100644 --- a/src/detail/font.cpp +++ b/src/detail/font.cpp @@ -8,59 +8,60 @@ #include #include -static std::unique_ptr> cache{ nullptr }; - namespace glez::detail::font { +std::vector *cache = nullptr; + void init() { - cache = std::make_unique>(); + cache = new std::vector; } void shutdown() { - cache.reset(nullptr); + delete cache; } -void font::load(const std::string &path, float size) +void ifont::load(const std::string &path, float size) { assert(size > 0); atlas = texture_atlas_new(1024, 1024, 1); assert(atlas != nullptr); - font = texture_font_new_from_file(atlas, size, path.c_str()); - assert(font != nullptr); + m_font = texture_font_new_from_file(atlas, size, path.c_str()); + assert(m_font != nullptr); init = true; } -void font::unload() +void ifont::unload() { if (!init) return; - texture_atlas_delete(atlas); - if (font) - texture_font_delete(font); + if (atlas) + texture_atlas_delete(atlas); + if (m_font) + texture_font_delete(m_font); init = false; } -void font::stringSize(const std::string &string, float *width, float *height) +void ifont::stringSize(const std::string &string, float *width, float *height) { float penX = 0; float size_x = 0; float size_y = 0; - texture_font_load_glyphs(font, string.c_str()); + texture_font_load_glyphs(m_font, string.c_str()); const char *sstring = string.c_str(); for (size_t i = 0; i < string.size(); ++i) { // c_str guarantees a NULL terminator - texture_glyph_t *glyph = texture_font_find_glyph(font, &sstring[i]); + texture_glyph_t *glyph = texture_font_find_glyph(m_font, &sstring[i]); if (glyph == nullptr) continue; @@ -85,12 +86,12 @@ unsigned create() if (not(*cache)[i].init) return i; auto result = cache->size(); - cache->push_back(font{}); + cache->push_back(ifont{}); return result; } -font &get(unsigned handle) +ifont &get(unsigned handle) { - return (*cache)[handle]; + return cache->at(handle); } } // namespace glez::detail::font diff --git a/src/detail/texture.cpp b/src/detail/texture.cpp index 313f11d..e610990 100644 --- a/src/detail/texture.cpp +++ b/src/detail/texture.cpp @@ -12,21 +12,19 @@ #include // required to load the file -static std::unique_ptr> cache{ - nullptr -}; +static std::vector *cache{ nullptr }; namespace glez::detail::texture { void init() { - cache = std::make_unique>(); + cache = new std::vector(); } void shutdown() { - cache.reset(nullptr); + delete cache; } void texture::bind() @@ -102,4 +100,4 @@ unsigned create() cache->push_back(texture{}); return result; } -} // namespace glez::detail::texture \ No newline at end of file +} // namespace glez::detail::texture diff --git a/src/draw.cpp b/src/draw.cpp index a1758ca..8b7a550 100644 --- a/src/draw.cpp +++ b/src/draw.cpp @@ -233,7 +233,7 @@ void string(float x, float y, const std::string &string, font &font, rgba color, if (!font.isLoaded()) font.load(); - auto fnt = glez::detail::font::get(font.getHandle()).font; + auto fnt = glez::detail::font::get(font.getHandle()).m_font; fnt->rendermode = RENDER_NORMAL; fnt->outline_thickness = 0.0f; internal_draw_string(x, y, string, fnt, color, width, height); @@ -245,7 +245,7 @@ void outlined_string(float x, float y, const std::string &string, font &font, if (!font.isLoaded()) font.load(); - auto fnt = glez::detail::font::get(font.getHandle()).font; + auto fnt = glez::detail::font::get(font.getHandle()).m_font; fnt->rendermode = RENDER_OUTLINE_POSITIVE; fnt->outline_thickness = 1.0f; internal_draw_string(x, y, string, fnt, outline, width, height); diff --git a/src/font.cpp b/src/font.cpp index 05da048..87adf65 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -38,4 +38,4 @@ void font::stringSize(const std::string &string, float *width, float *height) auto &font = detail::font::get(handle); font.stringSize(string, width, height); } -} // namespace glez \ No newline at end of file +} // namespace glez