SFML community forums

Help => Graphics => Topic started by: Dschoonmaker on June 16, 2018, 12:01:40 am

Title: Cannot center text on screen in sfml 2.5.0
Post by: Dschoonmaker on June 16, 2018, 12:01:40 am
How do I center text on the middle of the screen in sfml 2.5.0?

I have tried things like:
        text.setOrigin(text.getLocalBounds().width / 2.0f, text.getLocalBounds().height / 2.0f);
        text.setPosition(screenWidth / 2, screenHeight / 2);
 
But it is just slightly off.
Title: Re: Cannot center text on screen in sfml 2.5.0
Post by: Laurent on June 16, 2018, 02:28:29 pm
The bounding rect not only has a width and height, it also has a left and top.
Title: Re: Cannot center text on screen in sfml 2.5.0
Post by: lori3 on June 18, 2018, 10:44:32 pm
Laurent is right.. In other words:
Quote
text.setOrigin(text.getLocalBounds().left + text.getLocalBounds().width / 2.0f, text.getLocalBounds().top + text.getLocalBounds().height / 2.0f);
Title: Re: Cannot center text on screen in sfml 2.5.0
Post by: NGM88 on June 19, 2018, 07:45:22 am
Laurent is right.. In other words:
Quote
text.setOrigin(text.getLocalBounds().left + text.getLocalBounds().width / 2.0f, text.getLocalBounds().top + text.getLocalBounds().height / 2.0f);

Should also be rounded.