SFML community forums

Help => Graphics => Topic started by: svento92 on December 18, 2019, 04:33:31 pm

Title: Centralising a sf::Text inside a sf::Rectangle
Post by: svento92 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. :<
Title: Re: Centralising a sf::Text inside a sf::Rectangle
Post by: G. 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.