#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; 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, const std::string &outline_color, double outline_size ) { changeText(text); 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, 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 ); } void TextRenderer::changeText( const std::string &text ) { _text = text; text_change = true; } void TextRenderer::setFlags( int flags ) { position_flags = flags; updateDstRect(); } void TextRenderer::render() { if(text_change) { updateTexture(); text_change = false; } if ( !getHidden() ) { if ( polygon ) polygon->render( *renderer ); 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 ); } } if ( hasCollisions() && renderer->getRenderColiders() && !getHidden() ) { for ( const auto &col : getCollisions() ) col->render( *renderer, colider_color ); } } 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(); } void TextRenderer::updateSizeAndPosition() { RectangleRender::updateSizeAndPosition(); // TODO only if size actually changed // updateTexture(); text_change = true; 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 ) { auto config = std::make_shared< FontConfiguration >( font, color, outline_color, outline_size ); saveFontConfig(config); } void TextRenderer::saveFontConfig( std::shared_ptr< FontConfiguration > config ) { _config = std::move(config); text_change = true; } } // namespace SDLPP