From 0beb84466809bb66df84b94782212eebd349a65a Mon Sep 17 00:00:00 2001 From: Vsevolod Kremianskii Date: Wed, 17 Mar 2021 10:03:54 +0700 Subject: [PATCH] Cleanup font on destruction --- src/render/font.cpp | 29 ++++++++++++++++++++++++++--- src/render/font.h | 7 +++++-- src/render/fonts.cpp | 2 +- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/render/font.cpp b/src/render/font.cpp index 5f042061..34f4cff1 100644 --- a/src/render/font.cpp +++ b/src/render/font.cpp @@ -80,8 +80,8 @@ void Font::load(const shared_ptr &texture) { } } -void Font::initGL() { - if (_glInited) return; +void Font::init() { + if (_inited) return; glGenBuffers(1, &_vertexBufferId); glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferId); @@ -107,7 +107,30 @@ void Font::initGL() { glBindVertexArray(0); - _glInited = true; + _inited = true; +} + +Font::~Font() { + deinit(); +} + +void Font::deinit() { + if (!_inited) return; + + if (_vertexArrayId) { + glDeleteVertexArrays(1, &_vertexArrayId); + _vertexArrayId = 0; + } + if (_vertexBufferId) { + glDeleteBuffers(1, &_vertexBufferId); + _vertexBufferId = 0; + } + if (_indexBufferId) { + glDeleteBuffers(1, &_indexBufferId); + _indexBufferId = 0; + } + + _inited = false; } void Font::draw(const string &text, const glm::mat4 &transform, const glm::vec3 &color, TextGravity gravity) { diff --git a/src/render/font.h b/src/render/font.h index 0d1e2dea..b004fe75 100644 --- a/src/render/font.h +++ b/src/render/font.h @@ -40,9 +40,12 @@ enum class TextGravity { class Font { public: Font() = default; + ~Font(); + + void init(); + void deinit(); void load(const std::shared_ptr &texture); - void initGL(); void draw( const std::string &text, @@ -55,7 +58,7 @@ public: float height() const { return _height; } private: - bool _glInited { false }; + bool _inited { false }; std::vector _vertices; std::vector _indices; int _glyphCount { 0 }; diff --git a/src/render/fonts.cpp b/src/render/fonts.cpp index ee6d1508..8ab59b52 100644 --- a/src/render/fonts.cpp +++ b/src/render/fonts.cpp @@ -55,7 +55,7 @@ shared_ptr Fonts::doGet(string resRef) { auto font = make_shared(); font->load(texture); if (font) { - font->initGL(); + font->init(); } return move(font);