game/sdlpp/sdlpp_textrenderer.cpp

110 lines
4.0 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, 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 );
}
void TextRenderer::setText( 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( Font &font, const std::string &color,
const std::string &outline_color,
double outline_size ) {
int fontSize = 0.6 * getRect().h;
font.changeFontSize(fontSize);
int intOutline = outline_size;
if(intOutline != -1) {
intOutline = outline_size * fontSize;
}
setTexture( font, _text, color, outline_color, intOutline );
font.revertSize();
updateDstRect();
}
void TextRenderer::changeText( const std::string &text ) {
_text = text;
}
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();
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;
}
}
} // namespace SDLPP