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

Author Topic: sf::Text::GetGlobalBounds() returns misaligned rectangle?  (Read 2555 times)

0 Members and 1 Guest are viewing this topic.

ChristResurrection

  • Newbie
  • *
  • Posts: 2
    • View Profile
sf::Text::GetGlobalBounds() returns misaligned rectangle?
« on: April 10, 2014, 01:19:39 pm »
Good morning (afternoon, evening, depending on where you are). I think I have encountered a small problem with GetGlobalBounds().
I was not able to position my text properly so I tried to draw a rectangle using Text::GetGlobalBounds() for its position:
test.setPosition(txt.getPosition());
test.setSize(sf::Vector2f(txt.getGlobalBounds().width, txt.getGlobalBounds().height));

As you can see, the rectangle is not properly matching the bounding box of the text.
My text loads font from verdana.ttf, which is a default Windows font, and has its character size set to 64. Text style is not changed ("regular").
Is this a bug, or am I just missing something? Thanks in advance.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text::GetGlobalBounds() returns misaligned rectangle?
« Reply #1 on: April 10, 2014, 01:34:05 pm »
You must use the bound's left and top coordinates, which are not zero in case of sf::Text, instead of the text position (which is totally unrelated to the text bounds, especially if you start playing with the origin for example).
Laurent Gomila - SFML developer

ChristResurrection

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: sf::Text::GetGlobalBounds() returns misaligned rectangle?
« Reply #2 on: April 10, 2014, 03:01:31 pm »
Oh, it was a silly mistake in this test case, which works properly now, thank you.
However, I am still having troubles with text positioning. I am trying to put my text at the bottom (or center) of the shape by vertical axis. For some reason the text appears slightly lower than it should be (origin is always at (0, 0)).
text.setPosition(text->getPosition().left,
                          -text.getGlobalBounds().height +
                          shape.getGlobalBounds().top + shape.getGlobalBounds().height
);
 

I tried aligning the text by setting its origin to bottom, but it results in the same picture. I would like to know what am I doing wrong.

 

anything