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

Author Topic: [SOLVED] Text Size And Origin  (Read 1459 times)

0 Members and 1 Guest are viewing this topic.

Riser

  • Newbie
  • *
  • Posts: 33
    • View Profile
[SOLVED] Text Size And Origin
« on: July 11, 2020, 08:57:59 pm »
I'm trying to display a text prompt and a RectangleShape as a behind it that has the same exact size as the aforementioned text,to act as a bouding box,and I came up with a simple formula to do it:
int width = (text.getCharacterSize() * str.size()) + (text.getLetterSpacing() * (str.size() - 1));
With 'str' being the string containing the text displayed (In this case the words "LEVEL SELECT").
And the Rectangle's height simply being the CharacterSize.

And it...sort of works...


As you can see the box isn't quite the size of the text, and I don't know what the right way to do that would be...

My second problem is the text origin, I don't quite understand how that works, and I want to set the origin of the text to always be in the center,so that if I set the texts position to be the middle of the screen, the text will always be centered regardless of it's length.
« Last Edit: July 12, 2020, 07:44:53 pm by Riser »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Text Size And Origin
« Reply #1 on: July 12, 2020, 06:23:52 pm »
auto rect = text.getLocalBounds();

Use this rectangle to build your background (or use getGlobalBounds() if you want absolute coordinates).
Set the origin of the text to the center of it.
Laurent Gomila - SFML developer

Riser

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: Text Size And Origin
« Reply #2 on: July 12, 2020, 06:44:02 pm »
Yup,it's getLocalBounds() I should be working with,took me a whole day to figure that out,but for some reason the width value is set to zero inside the class where text is declared,and only becomes the actual width value if I call it from the main.cpp file...

EDIT: That problem was solved by calling the method that loads the font file locally (As in inside the header file that contains the text class/inside the class itself).
« Last Edit: July 12, 2020, 07:44:30 pm by Riser »

 

anything