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
This commit is contained in:
zv0n 2021-01-30 21:32:08 +01:00
parent 1f7a80d7d2
commit 73f67a3f47
3 changed files with 93 additions and 16 deletions

View File

@ -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

View File

@ -12,8 +12,18 @@ TextRenderer::TextRenderer( double x, double y, double w, double h,
double outline_size, int flags ) double outline_size, int flags )
: RectangleRender( x, y, w, h, r ) { : RectangleRender( x, y, w, h, r ) {
position_flags = flags; position_flags = flags;
setText( font, text, color, outline_color, outline_size );
saveFontConfig( font, 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, void TextRenderer::setText( std::shared_ptr< Font > font,
const std::string &text, const std::string &color, const std::string &text, const std::string &color,
@ -22,13 +32,21 @@ void TextRenderer::setText( std::shared_ptr< Font > font,
_text = text; _text = text;
setTextColor( font, color, outline_color, outline_size ); 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, void TextRenderer::setTextColor( std::shared_ptr< Font > font,
const std::string &color, const std::string &color,
const std::string &outline_color, const std::string &outline_color,
double outline_size ) { double outline_size ) {
saveFontConfig( font, color, outline_color, outline_size ); saveFontConfig( font, color, outline_color, outline_size );
updateSizeAndPosition(); updateSizeAndPosition();
updateDstRect(); }
void TextRenderer::setTextColor( std::shared_ptr< FontConfiguration > config ) {
saveFontConfig( config );
updateSizeAndPosition();
} }
void TextRenderer::changeText( const std::string &text ) { void TextRenderer::changeText( const std::string &text ) {
_text = text; _text = text;
@ -55,13 +73,14 @@ void TextRenderer::updateSizeAndPosition() {
RectangleRender::updateSizeAndPosition(); RectangleRender::updateSizeAndPosition();
int fontSize = 0.6 * getRect().h; int fontSize = 0.6 * getRect().h;
last_font->changeFontSize( fontSize ); _config->getFont()->changeFontSize( fontSize );
int intOutline = last_outline_size; int intOutline = _config->getOutlineSize();
if ( intOutline != -1 ) { if ( intOutline != -1 ) {
intOutline = last_outline_size * fontSize; intOutline = _config->getOutlineSize() * fontSize;
} }
setTexture( *last_font, _text, last_color, last_outline_color, intOutline ); setTexture( *_config->getFont(), _text, _config->getColor(),
last_font->revertSize(); _config->getOutlineColor(), intOutline );
_config->getFont()->revertSize();
updateDstRect(); updateDstRect();
} }
@ -118,9 +137,11 @@ void TextRenderer::saveFontConfig( std::shared_ptr< Font > font,
const std::string &color, const std::string &color,
const std::string &outline_color, const std::string &outline_color,
double outline_size ) { double outline_size ) {
last_font = font; _config = std::make_shared< FontConfiguration >( font, color, outline_color,
last_color = color; outline_size );
last_outline_color = outline_color; }
last_outline_size = outline_size; void TextRenderer::saveFontConfig(
std::shared_ptr< FontConfiguration > config ) {
_config = config;
} }
} // namespace SDLPP } // namespace SDLPP

View File

@ -3,6 +3,7 @@
#include "sdlpp_common.hpp" #include "sdlpp_common.hpp"
#include "sdlpp_rectrenderer.hpp" #include "sdlpp_rectrenderer.hpp"
#include "sdlpp_fontconfiguration.hpp"
#include <memory> #include <memory>
#include <string> #include <string>
@ -18,14 +19,21 @@ public:
const std::string &text, const std::string &color = "FFFFFF", const std::string &text, const std::string &color = "FFFFFF",
const std::string &outline_color = "000000", const std::string &outline_color = "000000",
double outline_size = -1, int flags = SDLPP_TEXT_CENTER ); 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, void setText( std::shared_ptr< Font > font, const std::string &text,
const std::string &color = "FFFFFF", const std::string &color = "FFFFFF",
const std::string &outline_color = "000000", const std::string &outline_color = "000000",
double outline_size = -1 ); double outline_size = -1 );
void setText( const std::string &text,
std::shared_ptr< FontConfiguration > config );
void setTextColor( std::shared_ptr< Font > font, void setTextColor( std::shared_ptr< Font > font,
const std::string &color = "FFFFFF", const std::string &color = "FFFFFF",
const std::string &outline_color = "000000", const std::string &outline_color = "000000",
double outline_size = -1 ); double outline_size = -1 );
void setTextColor( std::shared_ptr< FontConfiguration > config );
void changeText( const std::string &text ); void changeText( const std::string &text );
void setFlags( int flags ); void setFlags( int flags );
virtual void render() override; virtual void render() override;
@ -38,14 +46,11 @@ private:
void saveFontConfig( std::shared_ptr< Font > font, const std::string &color, void saveFontConfig( std::shared_ptr< Font > font, const std::string &color,
const std::string &outline_color, const std::string &outline_color,
double outline_size ); double outline_size );
void saveFontConfig( std::shared_ptr< FontConfiguration > config );
std::string _text{}; std::string _text{};
int position_flags = 0; int position_flags = 0;
SDL_Rect dst_rect{}; SDL_Rect dst_rect{};
std::shared_ptr< Font > last_font; std::shared_ptr< FontConfiguration > _config;
std::string last_color;
std::string last_outline_color;
double last_outline_size;
}; };
} // end of namespace SDLPP } // end of namespace SDLPP
#endif #endif