SFML community forums

General => Feature requests => Topic started by: escher on April 20, 2012, 12:26:16 pm

Title: Adding optional background color to sf::Text
Post 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?
Title: Re: Adding optional background color to sf::Text
Post by: Laurent on April 20, 2012, 12:53:56 pm
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.
Title: Re: Adding optional background color to sf::Text
Post by: Cornstalks on April 20, 2012, 07:34:30 pm
Just out of curiosity, Laurent, what's the best way to do this? I saw this question last night and thought of something like:

Code: [Select]
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?
Title: Re: Adding optional background color to sf::Text
Post by: Laurent on April 20, 2012, 08:58:14 pm
Yep, this code looks good.
Title: Re: Adding optional background color to sf::Text
Post by: Ceylo on April 22, 2012, 10:30:03 am
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)
Title: Re: Adding optional background color to sf::Text
Post by: minirop on April 23, 2012, 08:23:59 am
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").