diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-01-02 01:32:02 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-01-02 01:32:02 +0000 |
commit | cf64ce9a1312de5e9a76bb3f43105a4eac59eb15 (patch) | |
tree | 7ddf3d3bfc7a7add7a8eacde3862206cd2fd5ec4 | |
parent | Cache allows multiple key parts (diff) | |
download | ilt-cf64ce9a1312de5e9a76bb3f43105a4eac59eb15.tar.bz2 ilt-cf64ce9a1312de5e9a76bb3f43105a4eac59eb15.tar.xz ilt-cf64ce9a1312de5e9a76bb3f43105a4eac59eb15.zip |
Use Cache system to persist font rendering for Text
-rw-r--r-- | ui/font.cpp | 8 | ||||
-rw-r--r-- | ui/font.h | 12 | ||||
-rw-r--r-- | ui/text.cpp | 8 |
3 files changed, 20 insertions, 8 deletions
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 <algorithm> +#include <cache.h> #include <cctype> #include <ft2build.h> #include FT_FREETYPE_H +#include "glArrays.h" #include <glRef.hpp> #include <maths.h> #include <memory> #include <optional> #include <stdexcept> +#include <string> #include <unicode.h> #include <utility> // IWYU pragma: no_forward_declare FT_LibraryRec_ @@ -56,7 +59,9 @@ using Face = glRef<FT_Face, }, FT_Done_Face>; -Font::Font(const char * const p, unsigned s) : path {p}, size {getTextureSize(s)} +Cache<Font, std::filesystem::path, unsigned int> 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<GLsizei>(size.x), static_cast<GLsizei>(size.y), 0, GL_RED, GL_UNSIGNED_BYTE, nullptr); @@ -3,17 +3,21 @@ #include <GL/glew.h> #include <array> +#include <cache.h> #include <cstddef> #include <cstdint> +#include <filesystem> +#include <glArrays.h> #include <glm/glm.hpp> #include <map> -#include <string> #include <string_view> #include <vector> class Font { public: - Font(const char * const path, unsigned int height); + static Cache<Font, std::filesystem::path, unsigned int> cachedFontRenderings; + + Font(std::filesystem::path path, unsigned int height); using Quad = std::array<glm::vec4, 4>; using Quads = std::vector<Quad>; @@ -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<uint32_t, CharData> charsData; mutable std::vector<FontTexture> 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 <array> +#include <cache.h> +#include <filesystem> #include <glArrays.h> #include <glm/gtc/type_ptr.hpp> #include <map> +#include <memory> #include <utility> -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<unsigned int>(pos.size.y)}.render(s)) { + for (const auto & textureQuads : + Font::cachedFontRenderings.get(font, static_cast<unsigned int>(pos.size.y))->render(s)) { auto & rendering = models.emplace_back(textureQuads.first, static_cast<GLsizei>(6 * textureQuads.second.size())); glBindVertexArray(rendering.vao); |