From 6c5e0b27372167f92d97c458fcf8c3cd9efe6bfb Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 24 Apr 2026 19:08:26 +0300 Subject: [PATCH] generating VAO and VBO only once --- text.cpp | 33 +++++++++++++++++---------------- text.h | 3 ++- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/text.cpp b/text.cpp index 6e12df8..2d0c334 100755 --- a/text.cpp +++ b/text.cpp @@ -16,9 +16,9 @@ using namespace std; namespace vb01{ - Text::Text(string fontPath, wstring entry, u16 firstChar, u16 lastChar){ + Text::Text(string fontPath, wstring e, u16 firstChar, u16 lastChar) : entry(e){ + init(); applyFont(fontPath, firstChar, lastChar); - setText(entry); } Text::~Text(){ @@ -26,6 +26,21 @@ namespace vb01{ glDeleteBuffers(1, &VBO); } + void Text::init(){ + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + + glBindVertexArray(VAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + + glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + } + //TODO ensure destruction of textures when unloading the asset void Text::applyFont(string fontPath, u16 firstChar, u16 lastChar){ characters.clear(); @@ -120,20 +135,6 @@ namespace vb01{ glDrawArrays(GL_TRIANGLES, 0, 6); } - void Text::setText(wstring text){ - this->entry = text; - - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - glBindVertexArray(VAO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); - } - Text::Glyph* Text::getGlyph(u16 ch){ Glyph *glyph = nullptr; diff --git a/text.h b/text.h index 9d1780d..e19a481 100755 --- a/text.h +++ b/text.h @@ -26,7 +26,7 @@ namespace vb01{ Text(std::string, std::wstring, u16 = 0, u16 = 256); ~Text(); void update(); - void setText(std::wstring); + void setText(std::wstring e){this->entry = e;} inline void setScale(float s){this->scale = s;} inline float getScale(){return scale;} inline int getLength(){return entry.length();} @@ -39,6 +39,7 @@ namespace vb01{ inline void setMaterial(Material *mat){this->material = mat;} Glyph* getGlyph(u16); private: + void init(); void applyFont(std::string, u16, u16 = 256); void clearFont(); void prepareGlyphs(Glyph, int, Vector2);