game/sdlpp/sdlpp_textrenderer.cpp
zv0n a818c567fc SDLPP: TextRenderer remembers last font/color/outline
This is used for changeText() and updateSizeAndPosition() so programmer
doesn't have to pass the text configuration with every change of text/size.
2021-01-30 16:43:43 +01:00

127 lines
4.7 KiB
C++

#include "sdlpp_textrenderer.hpp"
namespace SDLPP {
TextRenderer::TextRenderer( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r )
: RectangleRender( x, y, w, h, r ) {}
TextRenderer::TextRenderer( double x, double y, double w, double h,
std::shared_ptr< Renderer > &r,
std::shared_ptr< Font > font,
const std::string &text, const std::string &color,
const std::string &outline_color,
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 );
}
void TextRenderer::setText( std::shared_ptr< Font > font,
const std::string &text, const std::string &color,
const std::string &outline_color,
double outline_size ) {
_text = text;
setTextColor( font, color, outline_color, outline_size );
}
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::changeText( const std::string &text ) {
_text = text;
updateSizeAndPosition();
}
void TextRenderer::setFlags( int flags ) {
position_flags = flags;
updateDstRect();
}
void TextRenderer::render() {
if ( !getHidden() ) {
if ( polygon )
polygon->render( *renderer );
if ( texture != NULL )
SDL_RenderCopy( renderer->getRendererPtr(),
texture->getTexturePtr(), NULL, &dst_rect );
}
if ( hasCollisions() && renderer->getRenderColiders() && !getHidden() ) {
for ( const auto &col : getCollisions() )
col->render( *renderer, colider_color );
}
}
void TextRenderer::updateSizeAndPosition() {
RectangleRender::updateSizeAndPosition();
int fontSize = 0.6 * getRect().h;
last_font->changeFontSize( fontSize );
int intOutline = last_outline_size;
if ( intOutline != -1 ) {
intOutline = last_outline_size * fontSize;
}
setTexture( *last_font, _text, last_color, last_outline_color, intOutline );
last_font->revertSize();
updateDstRect();
}
std::shared_ptr< RenderObject > TextRenderer::copySelf() {
auto ret = std::make_shared< TextRenderer >( *this );
copyTo( ret );
return ret;
}
void TextRenderer::copyTo( std::shared_ptr< RenderObject > other ) {
RectangleRender::copyTo( other );
}
void TextRenderer::updateDstRect() {
if ( !texture )
return;
int text_width{}, text_height{};
SDL_QueryTexture( texture->getTexturePtr(), NULL, NULL, &text_width,
&text_height );
if ( text_width < rect.w && text_height < rect.h ) {
dst_rect.w = text_width;
dst_rect.h = text_height;
} else {
double x_div = static_cast< double >( text_width ) /
static_cast< double >( rect.w );
double y_div = static_cast< double >( text_height ) /
static_cast< double >( rect.h );
if ( x_div > y_div ) {
dst_rect.w = text_width / x_div;
dst_rect.h = text_height / x_div;
} else {
dst_rect.w = text_width / y_div;
dst_rect.h = text_height / y_div;
}
}
if ( !( position_flags & SDLPP_TEXT_LEFT ||
position_flags & SDLPP_TEXT_RIGHT ) ) {
dst_rect.x = rect.x + ( rect.w - dst_rect.w ) / 2;
} else if ( position_flags & SDLPP_TEXT_LEFT ) {
dst_rect.x = rect.x;
} else if ( position_flags & SDLPP_TEXT_RIGHT ) {
dst_rect.x = rect.x + rect.w - dst_rect.w;
}
if ( !( position_flags & SDLPP_TEXT_TOP ||
position_flags & SDLPP_TEXT_BOTTOM ) ) {
dst_rect.y = rect.y + ( rect.h - dst_rect.h ) / 2;
} else if ( position_flags & SDLPP_TEXT_TOP ) {
dst_rect.y = rect.y;
} else if ( position_flags & SDLPP_TEXT_BOTTOM ) {
dst_rect.y = rect.y + rect.h - dst_rect.h;
}
}
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;
}
} // namespace SDLPP