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

Author Topic: Cannot center text on screen in sfml 2.5.0  (Read 2925 times)

0 Members and 1 Guest are viewing this topic.

Dschoonmaker

  • Newbie
  • *
  • Posts: 10
    • View Profile
Cannot center text on screen in sfml 2.5.0
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Cannot center text on screen in sfml 2.5.0
« Reply #1 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.
Laurent Gomila - SFML developer

lori3

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Cannot center text on screen in sfml 2.5.0
« Reply #2 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);
Warcraft II HD. Check it out! - https://github.com/lori2001/Warcraft-II---clone

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Cannot center text on screen in sfml 2.5.0
« Reply #3 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.