Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Adding optional background color to sf::Text  (Read 6277 times)

0 Members and 1 Guest are viewing this topic.

escher

  • Newbie
  • *
  • Posts: 8
    • View Profile
Adding optional background color to sf::Text
« on: April 20, 2012, 12:26:16 pm »
I posted it as a question here, 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Adding optional background color to sf::Text
« Reply #1 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.
« Last Edit: April 20, 2012, 12:56:43 pm by Laurent »
Laurent Gomila - SFML developer

Cornstalks

  • Full Member
  • ***
  • Posts: 180
    • View Profile
    • My Website
Re: Adding optional background color to sf::Text
« Reply #2 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Adding optional background color to sf::Text
« Reply #3 on: April 20, 2012, 08:58:14 pm »
Yep, this code looks good.
Laurent Gomila - SFML developer

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: Adding optional background color to sf::Text
« Reply #4 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.
Want to play movies in your SFML application? Check out sfeMovie!

minirop

  • Sr. Member
  • ****
  • Posts: 254
    • View Profile
    • http://dev.peyj.com
Re: Adding optional background color to sf::Text
« Reply #5 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").