From ae327b96a5935b157f9efdffed3d9246e329a69a Mon Sep 17 00:00:00 2001 From: Redacted Date: Fri, 12 Jul 2024 01:47:49 -0400 Subject: [PATCH] Cleanup --- include/JGL/FontCache.h | 4 ++-- include/JGL/JGL.h | 4 +++- src/FontCache.cpp | 15 +++++++++------ src/JGL.cpp | 9 +++++++++ src/JGL/TextRendering.cpp | 17 +++++++++++++---- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/include/JGL/FontCache.h b/include/JGL/FontCache.h index aaf1e50..86576d4 100644 --- a/include/JGL/FontCache.h +++ b/include/JGL/FontCache.h @@ -36,7 +36,7 @@ public: unsigned int getFontSize(); unsigned int getFontIndex(); CachedGlyph* getGlyph(char c); - std::vector* getGlyphs(); + std::vector getGlyphs(); CachedFont(unsigned int font_size, unsigned int font_index); }; @@ -44,7 +44,7 @@ class JGL::FontCache { private: std::vector cachedFonts = {}; public: - std::vector* getFonts(); + std::vector getFonts(); CachedFont* getFont(unsigned int font_size, unsigned int font_index); void appendFont(CachedFont* font); void newFont(unsigned int font_size, unsigned int font_index); diff --git a/include/JGL/JGL.h b/include/JGL/JGL.h index dbc6951..eb76585 100644 --- a/include/JGL/JGL.h +++ b/include/JGL/JGL.h @@ -54,6 +54,7 @@ namespace JGL { bool Update(const Vector2& window_size); bool InitTextEngine(); int LoadFont(const std::string& font_path); + void PurgeFontCache(); void UnloadFont(int font_index); // TODO: implement correct coloring @@ -130,7 +131,8 @@ namespace JGL { // TODO: Implement an overload that simply takes 3 Vector3's /// Draws a text string on the screen with a given point-size and font. - void DrawString(const Color3& color, std::string text, float x, float y, float scale, u32 size, unsigned int font_index); + void DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index); + void DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index); // TODO: Implement the following: void FillTexturedTriangle(); diff --git a/src/FontCache.cpp b/src/FontCache.cpp index f005c89..77e94a6 100644 --- a/src/FontCache.cpp +++ b/src/FontCache.cpp @@ -1,4 +1,5 @@ #include +#include using namespace JGL; @@ -21,6 +22,9 @@ CachedGlyph::CachedGlyph(GLuint texture_id, char c, float x2offset, float y2offs this->advanceY = advanceY; } +//TODO +//Because most things shown would be english characters. We can cut down on the iteration time significantly +//by putting each english character at the beginning of the list in order of how often they usually occur in text. void JGL::CachedFont::appendGlyph(JGL::CachedGlyph* glyph) { glyphs.push_back(glyph); } @@ -72,8 +76,8 @@ void CachedFont::eraseGlyph(GLuint texture_id) { glyphs.erase(glyphs.begin() + i); } -std::vector* CachedFont::getGlyphs() { - return &glyphs; +std::vector CachedFont::getGlyphs() { + return glyphs; } void FontCache::appendFont(CachedFont* font) { @@ -88,7 +92,7 @@ void FontCache::newFont(unsigned int font_size, unsigned int font_index) { void FontCache::eraseFont(CachedFont* font) { for (int i = 0; i < cachedFonts.size(); i++) { if (cachedFonts[i] == font) { - for (auto& g: *cachedFonts[i]->getGlyphs()) + for (auto& g: cachedFonts[i]->getGlyphs()) cachedFonts[i]->eraseGlyph(g); delete cachedFonts[i]; @@ -101,11 +105,10 @@ void FontCache::purgeCache() { //Remove every font from the cache. for (const auto& font : cachedFonts) eraseFont(font); - cachedFonts = {}; } -std::vector* FontCache::getFonts() { - return &cachedFonts; +std::vector FontCache::getFonts() { + return cachedFonts; } CachedFont* FontCache::getFont(unsigned int font_size, unsigned int font_index) { diff --git a/src/JGL.cpp b/src/JGL.cpp index cf4ebb2..52d92dd 100644 --- a/src/JGL.cpp +++ b/src/JGL.cpp @@ -17,6 +17,7 @@ bool wasVertexArraysEnabled = false; bool wasCullFaceEnabled = false; bool wasBlendEnabled = false; bool wasColorArrayEnabled = false; +GLint activeTextureUnit = 0; namespace JGL { using namespace J3ML; @@ -36,6 +37,11 @@ namespace JGL { glPushMatrix(); glLoadIdentity(); + glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit); + activeTextureUnit = activeTextureUnit - GL_TEXTURE0; + if (activeTextureUnit != 0) + glActiveTexture(GL_TEXTURE0); + wasDepthTestEnabled = false; if (glIsEnabled(GL_DEPTH_TEST)) wasDepthTestEnabled = true, @@ -106,6 +112,9 @@ namespace JGL { if (wasColorArrayEnabled) glEnable(GL_COLOR_ARRAY); + //Select whatever texture unit was selected before. + glActiveTexture(GL_TEXTURE0 + activeTextureUnit); + //Put the draw color back how it was before. glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]); diff --git a/src/JGL/TextRendering.cpp b/src/JGL/TextRendering.cpp index c3e4e1f..3752f68 100644 --- a/src/JGL/TextRendering.cpp +++ b/src/JGL/TextRendering.cpp @@ -52,7 +52,12 @@ namespace JGL { } FontCache fontCache; - void J2D::DrawString(const Color3& color, std::string text, float x, float y, float scale, u32 size, unsigned int font_index) { + void PurgeFontCache() { + fontCache.purgeCache(); + } + + //TODO multi-texturing for 8x speedup. I tried. We'll come back to it later. + void J2D::DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index) { glUseProgram(0); // Fixed-function pipeline. Font font{}; @@ -71,10 +76,10 @@ namespace JGL { if (font.face == nullptr) return; - glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, 1.0f); - FT_Set_Pixel_Sizes(font.face, 0, size); + glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f); + std::vector textures(text.length()); //For each character @@ -124,8 +129,8 @@ namespace JGL { } GLfloat vertices[12] = {x2, y2, x2, y2 + h, x2 + w, y2 + h,x2, y2, x2 + w, y2 + h, x2 + w, y2}; - GLfloat textureCoordinates[12] = {0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0}; + glTexCoordPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &textureCoordinates); glVertexPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &vertices); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -133,6 +138,10 @@ namespace JGL { glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture } + void J2D::DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index) { + J2D::DrawString(Color4::FromColor3(color, 255), text, x, y, scale, size, font_index); + } + void J3D::DrawString(const Color3& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, unsigned int font_index) { //TODO figure out what the scale should actually be mathematically. scale = scale * 0.002f;