#include "messagewidget.hpp" #include "fontfactory.h" using namespace SDLwidgets; MessageWidget::MessageWidget( int width, int height ) { _width = width; _height = height; // default color for a text label is white _color.r = 255; _color.g = 255; _color.b = 255; m_font = FontFactory::getInstance()->createFont( "tahoma.ttf", 12 ); } MessageWidget::~MessageWidget() { } void MessageWidget::setFont( std::string fontName, int fontSize ) { m_font = FontFactory::getInstance()->createFont( fontName, fontSize ); } void MessageWidget::draw( SDL_Surface* surface ) { SDL_Rect srcRect, destRect; Sint16 offset_x, offset_y; getOffset( offset_x, offset_y ); int maxHeight = TTF_FontHeight( m_font ); destRect.x = offset_x; destRect.y = offset_y + _height - maxHeight; srcRect.x = 0; srcRect.y = 0; srcRect.w = _width; srcRect.h = maxHeight; std::vector::reverse_iterator colorIt = m_messageColor.rbegin(); for ( std::vector::reverse_iterator lineIt = m_messageLine.rbegin(); lineIt != m_messageLine.rend() && destRect.y >= offset_y; lineIt++, colorIt++, destRect.y -= maxHeight + 1 ) { SDL_Surface* textSurface = TTF_RenderText_Solid( m_font, lineIt->c_str(), *colorIt ); SDL_BlitSurface( textSurface, &srcRect, surface, &destRect ); SDL_FreeSurface( textSurface ); } }