SFML community forums
General => Feature requests => Topic started by: escher on April 20, 2012, 12:26:16 pm
-
I posted it as a question here (http://en.sfml-dev.org/forums/index.php?topic=7651.0), but from the response I got it seems like I should post it here.
Many other libraries give you the option of rendering text with a background color. Any chance we could get that in SFML?
-
A text with background is a composed entity: a text and a colored rectangle. SFML provides stuff for both, so I don't know why there should be an additional feature just to put them together. I think it's easy enough to handle on user side, isn't it?
Plus, you usually don't want the background to have the exact same size as the text, in many situations you'll have the background at a fixed size, and then you'll align the text inside it.
-
Just out of curiosity, Laurent, what's the best way to do this? I saw this question last night and thought of something like:
sf::FloatRect backgroundRect = text.getLocalBounds();
sf::RectangleShape background(sf::Vector2f(backgroundRect.width, backgroundRect.height));
background.setFillColor(sf::Color::Red);
// And draw...
renderWindow.draw(background, text.getTransform());
renderWindow.draw(text);
Is this the best (though of course simplified example) way to do this?
-
Yep, this code looks good.
-
The only useful point I can think of would be to follow the lines end. But I don't even remember whether SFML handles line breaks.
(http://lucas.soltic.perso.luminy.univmed.fr/downloads/surlignage.png)
-
Ceylo, SFML does handle line-breaks.
Your example is more useful if you want to "highlight" some words (like a selection), so it shouldn't be "global background color" but "setBackgroundColor(sf::Color, int start, int length)" (start being the position of the first character to "highlight").