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

Author Topic: sf::Text position  (Read 5313 times)

0 Members and 1 Guest are viewing this topic.

fer_t

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
sf::Text position
« on: May 06, 2016, 03:06:13 am »
I am a bit confused about sf::Text and its positioning.

If origin is set to upper left corner 0,0 and I place it (setPosition) at 0,0 I expect it to start from 0,0 and extend to getGlobalBounds.width, getGlobalBounds.height.

But actually it does not start at 0,0 even if the origin is set to 0,0 and position is set to 0,0.
That is quite confusing, see first attachment: "Hallo" is placed as described and the RectangleShape is created:
sf::RectangleShape b({20, 5});
b.setPosition(0, s.getGlobalBounds().height);

So normally the bottom of the H should be end at the beginning of the shape and not overlap, or am I wrong?

Another example is attachment 2 (b.png) where all my menu entries are misplaced (see markers are where I expect the text to start).

PS:
Yes I know globalBounds.top gives me the offset (found that few minutes ago), but why is this offset always added?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text position
« Reply #1 on: May 06, 2016, 09:54:12 am »
Text is aligned on the baseline, not on the top. So that having characters of different heights on the first line doesn't change its global alignment. So there's this little offset on top that compensates.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::Text position
« Reply #2 on: May 09, 2016, 01:22:01 am »
If you need to position the text by the top-left of the actual content, set the sf::Text's origin to the local bounds' top-left. It will need updating (to the bounds' top-left) whenever the content (string) changes.

To simplify these sorts of things, you can use something like Plinth's SFML Object Anchor.
(Anchor.hpp) (Anchor.inl)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything