From 73f67a3f472cdc248e0d9e021d4a0a830cd246af Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 30 Jan 2021 21:32:08 +0100 Subject: [PATCH] SDLPP: Use FontConfiguration to store font configuration With this it is possible to only store 1 configuration object as opposed to storing font configuration inside every TextRenderer --- sdlpp/sdlpp_fontconfiguration.hpp | 51 +++++++++++++++++++++++++++++++ sdlpp/sdlpp_textrenderer.cpp | 43 +++++++++++++++++++------- sdlpp/sdlpp_textrenderer.hpp | 15 ++++++--- 3 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 sdlpp/sdlpp_fontconfiguration.hpp diff --git a/sdlpp/sdlpp_fontconfiguration.hpp b/sdlpp/sdlpp_fontconfiguration.hpp new file mode 100644 index 0000000..08863de --- /dev/null +++ b/sdlpp/sdlpp_fontconfiguration.hpp @@ -0,0 +1,51 @@ +#ifndef SDLPP_HPP_FONT_CONFIGURATION +#define SDLPP_HPP_FONT_CONFIGURATION + +#include "sdlpp_common.hpp" +#include "sdlpp_font.hpp" + +namespace SDLPP { +class SDLPPSCOPE FontConfiguration { +public: + FontConfiguration() = delete; + FontConfiguration( std::shared_ptr< Font > font, const std::string &color, + const std::string &outline_color, double outline_size ) { + _font = font; + _color = color; + _outline_color = outline_color; + _outline_size = outline_size; + } + + const std::shared_ptr< Font > &getFont() { + return _font; + } + + const std::string &getColor() { + return _color; + } + + const std::string &getOutlineColor() { + return _outline_color; + } + + const double &getOutlineSize() { + return _outline_size; + } + + void setColor( const std::string &color ) { + _color = color; + } + + void setOutlineColor( const std::string &outline_color ) { + _outline_color = outline_color; + } + +private: + std::shared_ptr< Font > _font; + std::string _color; + std::string _outline_color; + double _outline_size; +}; +} // namespace SDLPP + +#endif diff --git a/sdlpp/sdlpp_textrenderer.cpp b/sdlpp/sdlpp_textrenderer.cpp index 816c48c..d5a0f3f 100644 --- a/sdlpp/sdlpp_textrenderer.cpp +++ b/sdlpp/sdlpp_textrenderer.cpp @@ -12,8 +12,18 @@ TextRenderer::TextRenderer( double x, double y, double w, double h, double outline_size, int flags ) : RectangleRender( x, y, w, h, r ) { position_flags = flags; - setText( font, text, color, outline_color, outline_size ); saveFontConfig( font, color, outline_color, outline_size ); + changeText( text ); +} +TextRenderer::TextRenderer( double x, double y, double w, double h, + std::shared_ptr< Renderer > &r, + const std::string &text, + std::shared_ptr< FontConfiguration > config, + int flags ) + : RectangleRender( x, y, w, h, r ) { + position_flags = flags; + saveFontConfig( config ); + changeText( text ); } void TextRenderer::setText( std::shared_ptr< Font > font, const std::string &text, const std::string &color, @@ -22,13 +32,21 @@ void TextRenderer::setText( std::shared_ptr< Font > font, _text = text; setTextColor( font, color, outline_color, outline_size ); } +void TextRenderer::setText( const std::string &text, + std::shared_ptr< FontConfiguration > config ) { + _text = text; + setTextColor( config ); +} void TextRenderer::setTextColor( std::shared_ptr< Font > font, const std::string &color, const std::string &outline_color, double outline_size ) { saveFontConfig( font, color, outline_color, outline_size ); updateSizeAndPosition(); - updateDstRect(); +} +void TextRenderer::setTextColor( std::shared_ptr< FontConfiguration > config ) { + saveFontConfig( config ); + updateSizeAndPosition(); } void TextRenderer::changeText( const std::string &text ) { _text = text; @@ -55,13 +73,14 @@ void TextRenderer::updateSizeAndPosition() { RectangleRender::updateSizeAndPosition(); int fontSize = 0.6 * getRect().h; - last_font->changeFontSize( fontSize ); - int intOutline = last_outline_size; + _config->getFont()->changeFontSize( fontSize ); + int intOutline = _config->getOutlineSize(); if ( intOutline != -1 ) { - intOutline = last_outline_size * fontSize; + intOutline = _config->getOutlineSize() * fontSize; } - setTexture( *last_font, _text, last_color, last_outline_color, intOutline ); - last_font->revertSize(); + setTexture( *_config->getFont(), _text, _config->getColor(), + _config->getOutlineColor(), intOutline ); + _config->getFont()->revertSize(); updateDstRect(); } @@ -118,9 +137,11 @@ void TextRenderer::saveFontConfig( std::shared_ptr< Font > font, const std::string &color, const std::string &outline_color, double outline_size ) { - last_font = font; - last_color = color; - last_outline_color = outline_color; - last_outline_size = outline_size; + _config = std::make_shared< FontConfiguration >( font, color, outline_color, + outline_size ); +} +void TextRenderer::saveFontConfig( + std::shared_ptr< FontConfiguration > config ) { + _config = config; } } // namespace SDLPP diff --git a/sdlpp/sdlpp_textrenderer.hpp b/sdlpp/sdlpp_textrenderer.hpp index db0716d..0654663 100644 --- a/sdlpp/sdlpp_textrenderer.hpp +++ b/sdlpp/sdlpp_textrenderer.hpp @@ -3,6 +3,7 @@ #include "sdlpp_common.hpp" #include "sdlpp_rectrenderer.hpp" +#include "sdlpp_fontconfiguration.hpp" #include #include @@ -18,14 +19,21 @@ public: const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", double outline_size = -1, int flags = SDLPP_TEXT_CENTER ); + TextRenderer( double x, double y, double w, double h, + std::shared_ptr< Renderer > &r, const std::string &text, + std::shared_ptr< FontConfiguration > config, + int flags = SDLPP_TEXT_CENTER ); void setText( std::shared_ptr< Font > font, const std::string &text, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", double outline_size = -1 ); + void setText( const std::string &text, + std::shared_ptr< FontConfiguration > config ); void setTextColor( std::shared_ptr< Font > font, const std::string &color = "FFFFFF", const std::string &outline_color = "000000", double outline_size = -1 ); + void setTextColor( std::shared_ptr< FontConfiguration > config ); void changeText( const std::string &text ); void setFlags( int flags ); virtual void render() override; @@ -38,14 +46,11 @@ private: void saveFontConfig( std::shared_ptr< Font > font, const std::string &color, const std::string &outline_color, double outline_size ); - + void saveFontConfig( std::shared_ptr< FontConfiguration > config ); std::string _text{}; int position_flags = 0; SDL_Rect dst_rect{}; - std::shared_ptr< Font > last_font; - std::string last_color; - std::string last_outline_color; - double last_outline_size; + std::shared_ptr< FontConfiguration > _config; }; } // end of namespace SDLPP #endif