game/sdlpp/sdlpp_textrenderer.cpp

165 lines
6.1 KiB
C++
Raw Normal View History

2020-11-21 19:57:40 +00:00
#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,
2020-11-21 19:57:40 +00:00
const std::string &text, const std::string &color,
const std::string &outline_color,
double outline_size, int flags )
2020-11-21 19:57:40 +00:00
: RectangleRender( x, y, w, h, r ) {
position_flags = flags;
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 );
2020-11-21 19:57:40 +00:00
}
void TextRenderer::setText( std::shared_ptr< Font > font,
const std::string &text, const std::string &color,
2020-11-21 19:57:40 +00:00
const std::string &outline_color,
double outline_size ) {
changeText(text);
2020-11-21 19:57:40 +00:00
setTextColor( font, color, outline_color, outline_size );
}
void TextRenderer::setText( const std::string &text,
std::shared_ptr< FontConfiguration > config ) {
changeText(text);
setTextColor( config );
}
void TextRenderer::setTextColor( std::shared_ptr< Font > font,
const std::string &color,
2020-11-21 19:57:40 +00:00
const std::string &outline_color,
double outline_size ) {
saveFontConfig( font, color, outline_color, outline_size );
}
void TextRenderer::setTextColor( std::shared_ptr< FontConfiguration > config ) {
saveFontConfig( config );
2020-11-21 19:57:40 +00:00
}
void TextRenderer::changeText( const std::string &text ) {
_text = text;
text_change = true;
2020-11-21 19:57:40 +00:00
}
void TextRenderer::setFlags( int flags ) {
position_flags = flags;
updateDstRect();
}
void TextRenderer::render() {
if(text_change) {
updateTexture();
text_change = false;
}
2020-11-21 19:57:40 +00:00
if ( !getHidden() ) {
if ( polygon )
polygon->render( *renderer );
2021-04-29 11:06:37 +00:00
if ( texture != NULL ) {
SDL_Point *rotation_point = NULL;
if ( rotation_center.getX() != -1 ) {
rotation_point = &rotation_center_point;
}
SDL_RenderCopyEx( renderer->getRendererPtr(),
texture->getTexturePtr(), NULL, &dst_rect,
rotation_angle, rotation_point, flip );
}
2020-11-21 19:57:40 +00:00
}
if ( hasCollisions() && renderer->getRenderColiders() && !getHidden() ) {
for ( const auto &col : getCollisions() )
col->render( *renderer, colider_color );
}
}
2021-03-07 11:20:00 +00:00
void TextRenderer::updateTexture() {
if(_text.empty()) {
texture = NULL;
return;
}
int fontSize = 0.6 * getRect().h;
_config->getFont()->changeFontSize( fontSize );
int intOutline = _config->getOutlineSize();
if ( intOutline != -1 ) {
intOutline = _config->getOutlineSize() * fontSize;
}
setTexture( *_config->getFont(), _text, _config->getColor(),
_config->getOutlineColor(), intOutline );
_config->getFont()->revertSize();
2021-03-07 11:20:00 +00:00
}
void TextRenderer::updateSizeAndPosition() {
RectangleRender::updateSizeAndPosition();
// TODO only if size actually changed
// updateTexture();
text_change = true;
2020-11-21 19:57:40 +00:00
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 );
2020-11-21 19:57:40 +00:00
}
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 ) {
2022-06-22 19:42:57 +00:00
auto config = std::make_shared< FontConfiguration >( font, color, outline_color,
outline_size );
2022-06-22 19:42:57 +00:00
saveFontConfig(config);
}
void TextRenderer::saveFontConfig(
std::shared_ptr< FontConfiguration > config ) {
2022-06-22 19:42:57 +00:00
_config = std::move(config);
text_change = true;
}
2020-11-21 19:57:40 +00:00
} // namespace SDLPP