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

Author Topic: A few Questions concerning sf::text(sfml 2.0)  (Read 6095 times)

0 Members and 1 Guest are viewing this topic.

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
A few Questions concerning sf::text(sfml 2.0)
« on: July 02, 2012, 02:27:03 pm »
Hello,

Im trying to write a simple Label class which, for now, is supposed to draw a sf::text object on the screen with an rectangle surrounding it.
Im doing that by first drawing the text at the destined position and then using the text's globalposition to get the rectangle at the proper position. This works and always creates a fitting rectangle around the text, but what i actually want is to do this the other way around:
i create the background(with a fixed size) and then make the text fit it. Setting the character size of the sf::text does not give one proper control over the actual size of the text(or so it seems) and i can not like directly set the localBounds so the only solution i see right now is to solve this by iterating over possible character size's and sf::text positions.

In short:
What i wanna do in the end is to have a label with enough space for three digits of a certain size.
But i want it in this direction:
I define the (fix)size of the background rectangle(or later an image, but thats not the problem right now) and then compute the size and positon of the sf::text to properly fit my rectangle(so that neither a 1digit number nor a 3 digit number is out of the rectangle).

Thank you very much :)

 

texus

  • Sr. Member
  • ****
  • Posts: 499
    • View Profile
    • TGUI
    • Email
Re: A few Questions concerning sf::text(sfml 2.0)
« Reply #1 on: July 02, 2012, 02:39:22 pm »
I am not saying that this is the perfect solution, but I am using the following to do it.

// Set the new text height
text.setCharacterSize(static_cast<unsigned int>(height));

// Get the size of the text
sf::FloatRect bounds = text.getLocalBounds();

// The text still has too fit inside the area
if (bounds.width > width)
{
    // Adjust the text size
    text.setCharacterSize(static_cast<unsigned int>((height * width) / bounds.width));
}

The height and width are of course the size of the rectangle.
TGUI: C++ SFML GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: A few Questions concerning sf::text(sfml 2.0)
« Reply #2 on: July 02, 2012, 02:42:22 pm »
- declare your sf::Text with its string
- compute its total size (getLocalBounds)
- compute the ratio desired_size / actual_size
- multiply the character size by this ratio
- since the character size is an integer, if you want an exact match you'll need to adjust the scale factor too:
-- compute the new size of the text (getLocalBounds)
-- compute the ratio desired_size / actual_size
-- set it as the scale factor of the text (setScale)
Laurent Gomila - SFML developer

kingcools

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Re: A few Questions concerning sf::text(sfml 2.0)
« Reply #3 on: July 03, 2012, 05:22:51 pm »
thank you for the replies.

OT:
is the scale factor responsible for the size change on window resize events? E.g. if i set the scale factor to 2 and double the height of the window, does my object become 4 times as high?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: A few Questions concerning sf::text(sfml 2.0)
« Reply #4 on: July 03, 2012, 07:58:19 pm »
Quote
is the scale factor responsible for the size change on window resize events?
No, it's something else (-> the view) which is combined with the scale of objects.

Quote
E.g. if i set the scale factor to 2 and double the height of the window, does my object become 4 times as high?
Yes: 2 (object scale) * 2 (window scale) = 4 ;)
Laurent Gomila - SFML developer

 

anything