diff options
Diffstat (limited to 'src/graphics/text.cpp')
-rw-r--r-- | src/graphics/text.cpp | 79 |
1 files changed, 52 insertions, 27 deletions
diff --git a/src/graphics/text.cpp b/src/graphics/text.cpp index 82b8811..43e8be1 100644 --- a/src/graphics/text.cpp +++ b/src/graphics/text.cpp @@ -2,36 +2,61 @@ using namespace Graphics; -Text::Text(TextureFont &font, const std::string &text) - : font_(font) - , text_(text) - , vertices_(sf::Quads, text.size()*4) -{ - unsigned int length = vertices_.getVertexCount(); - sf::Vector2f glyphSize = font_.getGlyphSize(); - // TODO: this is the simplest rendering way: everything in one line - // we should improve this to make it handle newlines or max witdths - for(unsigned int i=0; i < length; i=i+4){ - vertices_[i+0].position = sf::Vector2f(glyphSize.x * i/4, 0); - vertices_[i+1].position = sf::Vector2f(glyphSize.x * (i/4 + 1), 0); - vertices_[i+2].position = sf::Vector2f(glyphSize.x * (i/4 + 1), glyphSize.y); - vertices_[i+3].position = sf::Vector2f(glyphSize.x * i/4, glyphSize.y); +template <> +void Text<TextureFont>::arrange(TextureFont &font){ + // TODO: this is really awful rendering but works a little bit + unsigned int filledX = 0; + unsigned int row = 0; + unsigned int col = 0; - std::array<sf::Vector2f, 4> glyph = font_.getGlyphMapping(text_[i/4]); + for(unsigned int i=0; i < text_.length(); i++){ + sf::Glyph glyph = font.getGlyph( text_[i] ); + filledX += glyph.advance; + if (maxWidth_ != 0 && ( text_[i] == '\n' || filledX > maxWidth_)){ + row++; + filledX = 0; + col = 0; + if(text_[i] == '\n') continue; + } - vertices_[i+0].texCoords = glyph[0]; - vertices_[i+1].texCoords = glyph[1]; - vertices_[i+2].texCoords = glyph[2]; - vertices_[i+3].texCoords = glyph[3]; + vertices_[4*i+0].position = sf::Vector2f( + col*glyph.advance + glyph.bounds.left, + (glyph.bounds.top) + row * glyph.bounds.height + ); + vertices_[4*i+1].position = sf::Vector2f( + col*glyph.advance + glyph.bounds.left+glyph.bounds.width, + (glyph.bounds.top) + row * glyph.bounds.height + ); + vertices_[4*i+2].position = sf::Vector2f( + col*glyph.advance + glyph.bounds.left+glyph.bounds.width, + (glyph.bounds.top+glyph.bounds.height) + row * glyph.bounds.height + ); + vertices_[4*i+3].position = sf::Vector2f( + col*glyph.advance + glyph.bounds.left, + (glyph.bounds.top+glyph.bounds.height) + row * glyph.bounds.height + ); + col++; + + vertices_[4*i+0].texCoords = sf::Vector2f( + glyph.textureRect.left, + glyph.textureRect.top + ); + vertices_[4*i+1].texCoords = sf::Vector2f( + glyph.textureRect.left+glyph.textureRect.width, + glyph.textureRect.top + ); + vertices_[4*i+2].texCoords = sf::Vector2f( + glyph.textureRect.left+glyph.textureRect.width, + glyph.textureRect.top+glyph.textureRect.height + ); + vertices_[4*i+3].texCoords = sf::Vector2f( + glyph.textureRect.left, + glyph.textureRect.top+glyph.textureRect.height + ); } } -void Text::draw(sf::RenderTarget& target, sf::RenderStates states) const -{ - // TODO: only repaint if needed: when repainting is needed - // vertices_.clear() and fun - states.transform *= getTransform(); - states.texture = font_.getTexture(); - // you may also override states.shader or states.blendMode if you want - target.draw(vertices_, states); +template <> +void Text<sf::Font>::arrange(sf::Font &font){ + // TODO: Implement this function for a generic sf::Font } |