From cf64ce9a1312de5e9a76bb3f43105a4eac59eb15 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 2 Jan 2022 01:32:02 +0000 Subject: Use Cache system to persist font rendering for Text --- ui/font.cpp | 8 ++++++-- ui/font.h | 12 ++++++++---- ui/text.cpp | 8 ++++++-- 3 files changed, 20 insertions(+), 8 deletions(-) (limited to 'ui') diff --git a/ui/font.cpp b/ui/font.cpp index 5596f20..712818d 100644 --- a/ui/font.cpp +++ b/ui/font.cpp @@ -1,13 +1,16 @@ #include "font.h" #include +#include #include #include #include FT_FREETYPE_H +#include "glArrays.h" #include #include #include #include #include +#include #include #include // IWYU pragma: no_forward_declare FT_LibraryRec_ @@ -56,7 +59,9 @@ using Face = glRef; -Font::Font(const char * const p, unsigned s) : path {p}, size {getTextureSize(s)} +Cache Font::cachedFontRenderings; + +Font::Font(std::filesystem::path p, unsigned s) : path {std::move(p)}, size {getTextureSize(s)} { generateChars(BASIC_CHARS); } @@ -115,7 +120,6 @@ Font::getTextureWithSpace(unsigned int adv) const } auto & texture = fontTextures.emplace_back(); - glGenTextures(1, &texture.texture); glBindTexture(GL_TEXTURE_2D, texture.texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, static_cast(size.x), static_cast(size.y), 0, GL_RED, GL_UNSIGNED_BYTE, nullptr); diff --git a/ui/font.h b/ui/font.h index c9d834a..4b44ee3 100644 --- a/ui/font.h +++ b/ui/font.h @@ -3,17 +3,21 @@ #include #include +#include #include #include +#include +#include #include #include -#include #include #include class Font { public: - Font(const char * const path, unsigned int height); + static Cache cachedFontRenderings; + + Font(std::filesystem::path path, unsigned int height); using Quad = std::array; using Quads = std::vector; @@ -28,7 +32,7 @@ public: long advance; }; struct FontTexture { - GLuint texture; + glTexture texture; unsigned int used; }; @@ -39,7 +43,7 @@ protected: const CharData getChar(char) const; std::size_t getTextureWithSpace(unsigned int adv) const; - std::string path; + std::filesystem::path path; glm::uvec3 size; mutable std::map charsData; mutable std::vector fontTextures; diff --git a/ui/text.cpp b/ui/text.cpp index 2acfb7e..0c372a1 100644 --- a/ui/text.cpp +++ b/ui/text.cpp @@ -3,15 +3,19 @@ #include "gfx/gl/uiShader.h" #include "uiComponent.h" #include +#include +#include #include #include #include +#include #include -const auto font {"/usr/share/fonts/hack/Hack-Regular.ttf"}; +const std::filesystem::path font {"/usr/share/fonts/hack/Hack-Regular.ttf"}; Text::Text(std::string_view s, Position pos, glm::vec3 c) : UIComponent {pos}, colour {c} { - for (const auto & textureQuads : Font {font, static_cast(pos.size.y)}.render(s)) { + for (const auto & textureQuads : + Font::cachedFontRenderings.get(font, static_cast(pos.size.y))->render(s)) { auto & rendering = models.emplace_back(textureQuads.first, static_cast(6 * textureQuads.second.size())); glBindVertexArray(rendering.vao); -- cgit v1.2.3