diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d17fe7..ea01742 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,4 +38,4 @@ add_library(${PROJECT_NAME} source/gui/input/ControllerManager.cpp source/gui/system/SDLSystem.cpp source/gui/video/SDL_FontCache.cpp - ) + include/gui-sdl/gui/GuiFont.h source/gui/GuiFont.cpp) diff --git a/include/gui-sdl/gui/GuiFont.h b/include/gui-sdl/gui/GuiFont.h new file mode 100644 index 0000000..0c18829 --- /dev/null +++ b/include/gui-sdl/gui/GuiFont.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include +#include +#include + +class GuiFont { +public: + GuiFont(const uint8_t *buffer, size_t buffer_size, Renderer *renderer); + ~GuiFont(); + + FC_Font *getFont(int32_t size); + + +private: + std::map font_cache; + std::map ttf_font_cache; + SDL_RWops *rw; + Renderer *renderer; +}; diff --git a/include/gui-sdl/gui/GuiText.h b/include/gui-sdl/gui/GuiText.h index 93a85e2..05534b2 100644 --- a/include/gui-sdl/gui/GuiText.h +++ b/include/gui-sdl/gui/GuiText.h @@ -20,17 +20,31 @@ #include #include #include +#include #include #include //!Display, manage, and manipulate text in the GUI class GuiText : public GuiElement { public: + //!\param t Text + explicit GuiText(const std::string &t); + + //!\param t Text + //!\param s Font size + GuiText(const std::string &t, int32_t s); //!\param t Text //!\param s Font size //!\param c Font color - GuiText(const std::string &t, SDL_Color c, FC_Font *font); + GuiText(const std::string &t, int32_t s, SDL_Color c); + + //!\param t Text + //!\param s Font size + //!\param c Font color + //!\param font Font + GuiText(const std::string &t, int32_t s, SDL_Color c, GuiFont *font); + ~GuiText() override; void draw(Renderer *pVideo) override; @@ -39,9 +53,15 @@ public: void setMaxWidth(float width); + static void setPresetFont(GuiFont *f); + + static void setPresets(int32_t sz, const SDL_Color & c); + + void setFontSize(int32_t size); + protected: GuiImage texture; - GuiTextureData* textureData = nullptr; + GuiTextureData *textureData = nullptr; std::string text; SDL_Color color; @@ -53,4 +73,10 @@ protected: void updateSize(); void updateTexture(Renderer *renderer); + + GuiFont *gFont = nullptr; + + static GuiFont *presetFont; + static SDL_Color presetColor; + static int32_t presetSize; }; diff --git a/source/gui/GuiFont.cpp b/source/gui/GuiFont.cpp new file mode 100644 index 0000000..7a3f1d9 --- /dev/null +++ b/source/gui/GuiFont.cpp @@ -0,0 +1,46 @@ +#include + +GuiFont::GuiFont(const uint8_t *buffer, size_t buffer_size, Renderer *renderer) { + this->rw = SDL_RWFromMem((void *) buffer, buffer_size); + this->renderer = renderer; +} + +GuiFont::~GuiFont() { + for (auto const&[key, val] : font_cache) { + FC_FreeFont(val); + } + for (auto const&[key, val] : ttf_font_cache) { + TTF_CloseFont(val); + } + SDL_RWclose(rw); +} + +FC_Font *GuiFont::getFont(int32_t size) { + if (font_cache.count(size) > 0) { + return font_cache[size]; + } + + TTF_Font *font = TTF_OpenFontRW(rw, 0, size); + if (!font) { + return nullptr; + } + + FC_Font *fc_font = FC_CreateFont(); + if (!fc_font) { + TTF_CloseFont(font); + font = nullptr; + return nullptr; + } + + auto res = FC_LoadFontFromTTF(fc_font, renderer->getRenderer(), font, {255, 255, 255, 255}); + if (!res) { + TTF_CloseFont(font); + font = nullptr; + return nullptr; + } + + font_cache[size] = fc_font; + ttf_font_cache[size] = font; + + return fc_font; +} \ No newline at end of file diff --git a/source/gui/GuiText.cpp b/source/gui/GuiText.cpp index 05c71e8..acf4da5 100644 --- a/source/gui/GuiText.cpp +++ b/source/gui/GuiText.cpp @@ -17,36 +17,41 @@ #include #include #include -#include -/** - * Constructor for the GuiText class. - */ +GuiFont *GuiText::presetFont = nullptr; +int32_t GuiText::presetSize = 28; +SDL_Color GuiText::presetColor = (SDL_Color) { + 255, 255, 255, 255 +}; -GuiText::GuiText(const std::string& text, SDL_Color c, FC_Font* gFont) { - this->text = text; - this->color = c; - this->fc_font = gFont; - this->doUpdateTexture = true; - this->texture.setParent(this); +GuiText::GuiText(const std::string &t) + : GuiText(t, presetSize) { +} + +GuiText::GuiText(const std::string &t, int32_t s) + : GuiText(t, s, presetColor) { +} + +GuiText::GuiText(const std::string &t, int32_t s, SDL_Color c) + : GuiText(t, s, c, presetFont) { +} + +GuiText::GuiText(const std::string &text, int32_t size, SDL_Color c, GuiFont *gFont) : + text(text), + color(c) { + this->gFont = gFont; + this->fc_font = gFont->getFont(size); + this->doUpdateTexture = true; + this->texture.setParent(this); } GuiText::~GuiText() { delete textureData; } -void GuiText::draw(Renderer *renderer) { - if (!this->isVisible()) { - return; - } - - updateTexture(renderer); - - texture.draw(renderer); -} - -void GuiText::process() { - GuiElement::process(); +void GuiText::setFontSize(int32_t size) { + this->fc_font = this->gFont->getFont(size); + this->doUpdateTexture = true; } void GuiText::setMaxWidth(float width) { @@ -85,7 +90,7 @@ void GuiText::updateTexture(Renderer *renderer) { SDL_RenderClear(renderer->getRenderer()); // Draw text to texture - FC_DrawColumn(fc_font, renderer->getRenderer(), 0, 0, maxWidth, text.c_str()); + FC_DrawColumnColor(fc_font, renderer->getRenderer(), 0, 0, maxWidth, color, text.c_str()); // Restore render target SDL_SetRenderTarget(renderer->getRenderer(), nullptr); @@ -95,3 +100,26 @@ void GuiText::updateTexture(Renderer *renderer) { doUpdateTexture = false; } } + +void GuiText::draw(Renderer *renderer) { + if (!this->isVisible()) { + return; + } + + updateTexture(renderer); + + texture.draw(renderer); +} + +void GuiText::process() { + GuiElement::process(); +} + +void GuiText::setPresets(int32_t sz, const SDL_Color &c) { + GuiText::presetSize = sz; + GuiText::presetColor = c; +} + +void GuiText::setPresetFont(GuiFont *f) { + GuiText::presetFont = f; +}