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

Author Topic: Centralising a sf::Text inside a sf::Rectangle  (Read 2778 times)

0 Members and 1 Guest are viewing this topic.

svento92

  • Newbie
  • *
  • Posts: 6
    • View Profile
Centralising a sf::Text inside a sf::Rectangle
« on: December 18, 2019, 04:33:31 pm »
So i have my function that puts an object into the center of the screen.

Quote
    object.setPosition(width,height);
    sf::FloatRect shapeRectangle = object.getLocalBounds();
    object.setOrigin(shapeRectangle.left + shapeRectangle.width/2.0f,
                    shapeRectangle.top  + shapeRectangle.height/2.0f);

The function is called twice once for an sf::RectangleShape and another for a sf::Text that should appear inside the sf::RectangleShape.  This works great if I call the function above after i have called sf::Texts function setString().

However if i put the object in the center of the screen and then call setString() or if setString(" "). Then the text that i put in using setString() is slightly offset. It goes up and to the right instead of the center of the sf::RectangleShape.

Not really sure how to solve this. :<

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Centralising a sf::Text inside a sf::Rectangle
« Reply #1 on: December 18, 2019, 05:36:55 pm »
You can't center texts of different lengths with the same origin.
Recompute the origin every time you change the string of your Text.

 

anything