generating VAO and VBO only once

This commit is contained in:
devZoGok
2026-04-24 19:08:26 +03:00
parent a92ca606af
commit 6c5e0b2737
2 changed files with 19 additions and 17 deletions
+17 -16
View File
@@ -16,9 +16,9 @@
using namespace std; using namespace std;
namespace vb01{ 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); applyFont(fontPath, firstChar, lastChar);
setText(entry);
} }
Text::~Text(){ Text::~Text(){
@@ -26,6 +26,21 @@ namespace vb01{
glDeleteBuffers(1, &VBO); 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 //TODO ensure destruction of textures when unloading the asset
void Text::applyFont(string fontPath, u16 firstChar, u16 lastChar){ void Text::applyFont(string fontPath, u16 firstChar, u16 lastChar){
characters.clear(); characters.clear();
@@ -120,20 +135,6 @@ namespace vb01{
glDrawArrays(GL_TRIANGLES, 0, 6); 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){ Text::Glyph* Text::getGlyph(u16 ch){
Glyph *glyph = nullptr; Glyph *glyph = nullptr;
+2 -1
View File
@@ -26,7 +26,7 @@ namespace vb01{
Text(std::string, std::wstring, u16 = 0, u16 = 256); Text(std::string, std::wstring, u16 = 0, u16 = 256);
~Text(); ~Text();
void update(); void update();
void setText(std::wstring); void setText(std::wstring e){this->entry = e;}
inline void setScale(float s){this->scale = s;} inline void setScale(float s){this->scale = s;}
inline float getScale(){return scale;} inline float getScale(){return scale;}
inline int getLength(){return entry.length();} inline int getLength(){return entry.length();}
@@ -39,6 +39,7 @@ namespace vb01{
inline void setMaterial(Material *mat){this->material = mat;} inline void setMaterial(Material *mat){this->material = mat;}
Glyph* getGlyph(u16); Glyph* getGlyph(u16);
private: private:
void init();
void applyFont(std::string, u16, u16 = 256); void applyFont(std::string, u16, u16 = 256);
void clearFont(); void clearFont();
void prepareGlyphs(Glyph, int, Vector2); void prepareGlyphs(Glyph, int, Vector2);