Cleanup font on destruction

This commit is contained in:
Vsevolod Kremianskii 2021-03-17 10:03:54 +07:00
parent d38e19471e
commit 0beb844668
3 changed files with 32 additions and 6 deletions

View file

@ -80,8 +80,8 @@ void Font::load(const shared_ptr<Texture> &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) {

View file

@ -40,9 +40,12 @@ enum class TextGravity {
class Font {
public:
Font() = default;
~Font();
void init();
void deinit();
void load(const std::shared_ptr<Texture> &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<float> _vertices;
std::vector<uint16_t> _indices;
int _glyphCount { 0 };

View file

@ -55,7 +55,7 @@ shared_ptr<Font> Fonts::doGet(string resRef) {
auto font = make_shared<Font>();
font->load(texture);
if (font) {
font->initGL();
font->init();
}
return move(font);