/* Drawing::drawText * Draws [text] at [x,y]. If [bounds] is not null, the bounding * coordinates of the rendered text string are written to it. *******************************************************************/ void Drawing::drawText(string text, int x, int y, rgba_t colour, int font, int alignment, frect_t* bounds) { // Setup SFML string sf::Text sf_str; sf_str.setString(UTF8(text)); sf_str.setPosition(x, y); sf_str.setColor(sf::Color(colour.r, colour.g, colour.b, colour.a)); // Set font sf::Font* f = theFontManager->getFont(font); sf_str.setFont(*f); if (font == FONT_SMALL) sf_str.setCharacterSize((gl_font_size * 0.6) + 1); else sf_str.setCharacterSize(gl_font_size); // Setup alignment if (alignment != ALIGN_LEFT) { float width = sf_str.getLocalBounds().width; if (alignment == ALIGN_CENTER) sf_str.move(-MathStuff::round(width*0.5), 0.0f); else sf_str.move(-width, 0.0f); } // Set bounds rect if (bounds) { sf::FloatRect rect = sf_str.getGlobalBounds(); bounds->set(rect.left, rect.top, rect.left+rect.width, rect.top+rect.height); } // Draw the string if (render_target) { if (text_state_reset) setTextState(true); // Draw render_target->draw(sf_str); if (text_state_reset) setTextState(false); } }
/* Drawing::drawText * Draws [text] at [x,y]. If [bounds] is not null, the bounding * coordinates of the rendered text string are written to it. *******************************************************************/ void Drawing::drawText(string text, int x, int y, rgba_t colour, int font, int alignment, frect_t* bounds) { // Setup SFML string sf::Text sf_str; sf_str.setString(UTF8(text)); sf_str.setPosition(x, y); sf_str.setColor(sf::Color(colour.r, colour.g, colour.b, colour.a)); // Set font sf::Font* f = theFontManager->getFont(font); sf_str.setFont(*f); if (font == FONT_SMALL) sf_str.setCharacterSize((gl_font_size * 0.6) + 1); else sf_str.setCharacterSize(gl_font_size); // Setup alignment if (alignment != ALIGN_LEFT) { float width = sf_str.getLocalBounds().width; if (alignment == ALIGN_CENTER) sf_str.move(-MathStuff::round(width*0.5), 0.0f); else sf_str.move(-width, 0.0f); } // Set bounds rect if (bounds) { sf::FloatRect rect = sf_str.getGlobalBounds(); bounds->set(rect.left, rect.top, rect.left+rect.width, rect.top+rect.height); } // Draw the string if (render_target) { if (text_state_reset) setTextState(true); if (text_outline_width > 0) { #if (SFML_VERSION_MAJOR == 2 && SFML_VERSION_MINOR >= 4) || SFML_VERSION_MAJOR > 2 // Set text outline if SFML version is 2.4 or later sf_str.setOutlineThickness(text_outline_width); sf_str.setOutlineColor( sf::Color( text_outline_colour.r, text_outline_colour.g, text_outline_colour.b, text_outline_colour.a ) ); #else // On SFML < 2.4, use old hacky outline method sf_str.setColor( sf::Color( text_outline_colour.r, text_outline_colour.g, text_outline_colour.b, text_outline_colour.a ) ); sf_str.setPosition(x - text_outline_width, y - text_outline_width); render_target->draw(sf_str); sf_str.setPosition(x - text_outline_width, y + text_outline_width); render_target->draw(sf_str); sf_str.setPosition(x + text_outline_width, y + text_outline_width); render_target->draw(sf_str); sf_str.setPosition(x + text_outline_width, y - text_outline_width); render_target->draw(sf_str); sf_str.setPosition(x, y); #endif } // Draw render_target->draw(sf_str); if (text_state_reset) setTextState(false); } }