Primitive::Ptr Renderer::CreateText( const sf::Text& text, sf::Color background_color_hint ) { Primitive::Ptr primitive( new Primitive ); const sf::Font& font = text.getFont(); unsigned int character_size = text.getCharacterSize(); sf::Color color = text.getColor(); if( m_preblend ) { color = sf::Color::White; } sf::Vector2f atlas_offset = LoadFont( font, character_size, background_color_hint, text.getColor() ); const sf::String& str = text.getString(); std::size_t length = str.getSize(); float horizontal_spacing = static_cast<float>( font.getGlyph( L' ', character_size, false ).advance ); float vertical_spacing = static_cast<float>( font.getLineSpacing( character_size ) ); sf::Vector2f position( std::floor( text.getPosition().x + .5f ), std::floor( text.getPosition().y + static_cast<float>( character_size ) + .5f ) ); const static float tab_spaces = 2.f; sf::Uint32 previous_character = 0; for( std::size_t index = 0; index < length; ++index ) { sf::Uint32 current_character = str[index]; position.x += static_cast<float>( font.getKerning( previous_character, current_character, character_size ) ); switch( current_character ) { case L' ': position.x += horizontal_spacing; continue; case L'\t': position.x += horizontal_spacing * tab_spaces; continue; case L'\n': position.y += vertical_spacing; position.x = 0.f; continue; case L'\v': position.y += vertical_spacing * tab_spaces; continue; default: break; } const sf::Glyph& glyph = font.getGlyph( current_character, character_size, false ); Primitive::Vertex vertex0; Primitive::Vertex vertex1; Primitive::Vertex vertex2; Primitive::Vertex vertex3; vertex0.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left ), static_cast<float>( glyph.bounds.top ) ); vertex1.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left ), static_cast<float>( glyph.bounds.top + glyph.bounds.height ) ); vertex2.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left + glyph.bounds.width ), static_cast<float>( glyph.bounds.top ) ); vertex3.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left + glyph.bounds.width ), static_cast<float>( glyph.bounds.top + glyph.bounds.height ) ); vertex0.color = color; vertex1.color = color; vertex2.color = color; vertex3.color = color; // Let SFML cast the Rect for us. sf::FloatRect texture_rect( glyph.textureRect ); vertex0.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left, texture_rect.top ); vertex1.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left, texture_rect.top + texture_rect.height ); vertex2.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left + texture_rect.width, texture_rect.top ); vertex3.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left + texture_rect.width, texture_rect.top + texture_rect.height ); primitive->AddVertex( vertex0 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex3 ); position.x += static_cast<float>( glyph.advance ); previous_character = current_character; } AddPrimitive( primitive ); return primitive; }
// Render text with gaussian blur drop shadow void ShadowTextRenderer::DrawDropShadowText(sf::RenderTarget& target, sf::Text& text, bool highQuality = true) { sf::View oldView = Utilities::SetDefaultView(target); sf::RenderStates states = sf::RenderStates(); states.blendMode = sf::BlendMode::BlendAlpha; if (gaussianShader && highQuality) { sf::FloatRect bounds = text.getGlobalBounds(); sf::Vector2u size = sf::Vector2u(bounds.width * 2.0f, bounds.height * 4.0f); auto ptr = findFromTextureCache(size); sf::RenderTexture* base; if (ptr != 0) base = ptr; else { base = new sf::RenderTexture(); base->create(size.x, size.y); base->setSmooth(true); } sf::View view = base->getView(); view.setCenter(text.getPosition().x + bounds.width / 2.0f, text.getPosition().y + bounds.height / 2.0f); base->setView(view); base->clear(sf::Color::Transparent); sf::Color oldColor = text.getColor(); text.setColor(sf::Color::Black); base->draw(text, states); text.setColor(oldColor); base->display(); ptr = findFromTextureCache(size); sf::RenderTexture* shadow; if (ptr != 0) shadow = ptr; else { shadow = new sf::RenderTexture(); shadow->create(size.x, size.y); shadow->setSmooth(true); } view = shadow->getView(); view.setCenter(shadow->getSize().x / 2.0f, shadow->getSize().y / 2.0f); shadow->setView(view); shadow->clear(sf::Color::Transparent); gaussianShader->setParameter("texture", base->getTexture()); gaussianShader->setParameter("resolution", bounds.width); gaussianShader->setParameter("blurScale", 0.4f); gaussianShader->setParameter("blurStrength", 0.5f); gaussianShader->setParameter("blurAmount", 6.0f); gaussianShader->setParameter("dir", sf::Vector2f(1.0f, 0.0f)); states.shader = gaussianShader; shadow->draw(sf::Sprite(base->getTexture()), states); shadow->display(); addToTextureCache(base); sf::Texture shadowTex = shadow->getTexture(); shadow->clear(sf::Color::Transparent); gaussianShader->setParameter("texture", shadowTex); gaussianShader->setParameter("resolution", bounds.height); gaussianShader->setParameter("dir", sf::Vector2f(0.0f, 1.0f)); states.shader = gaussianShader; shadow->draw(sf::Sprite(shadowTex), states); shadow->display(); states.shader = 0; sf::Sprite shadowSpr = sf::Sprite(shadow->getTexture()); shadowSpr.setPosition(text.getPosition() - sf::Vector2f(shadow->getSize().x / 2.0f , shadow->getSize().y / 2.0f) + sf::Vector2f(bounds.width / 2.0f, bounds.height / 2.0f)); target.draw(shadowSpr, states); addToTextureCache(shadow); //sf::Sprite baseSpr = sf::Sprite(base.getTexture()); //baseSpr.setPosition(text.getPosition() + (text.getPosition() - view.getCenter())); } else { sf::Vector2f oldPos = text.getPosition(); text.setPosition(oldPos - sf::Vector2f(2.0f, 2.0f)); sf::Color oldColor = text.getColor(); text.setColor(sf::Color::Black); target.draw(text, states); text.setPosition(oldPos); text.setColor(oldColor); } target.draw(text, states); target.setView(oldView); }