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

Author Topic: Setting Text origin driving me insane  (Read 3096 times)

0 Members and 1 Guest are viewing this topic.

Notion

  • Newbie
  • *
  • Posts: 17
    • View Profile
Setting Text origin driving me insane
« on: August 04, 2018, 07:36:21 pm »
Hey folks,

trying to set the origin of a text to its center in order to place it in the middle of a circle. Text tends to be quite off to the bottom right.

Read the thread https://en.sfml-dev.org/forums/index.php?topic=19447.0 , but that didn't work for me either.

Here's the code:

Code: [Select]
                //Some Circle Shape
lifeBackground = new sf::CircleShape();

lifeBackground->setFillColor(sf::Color::Red);
lifeBackground->setOutlineThickness(4);
lifeBackground->setOutlineColor(sf::Color::Black);
lifeBackground->setRadius(20);

lifeBackground->setOrigin(lifeBackground->getLocalBounds().width/2, lifeBackground->getLocalBounds().height / 2);

                //Text should go into center of circle shape
lifelabel = new sf::Text();
lifelabel->setFont(font);
lifelabel->setCharacterSize(25);
lifelabel->setOutlineThickness(3);
lifelabel->setString(std::to_string(life));
lifelabel->setFillColor(sf::Color::Yellow);
lifelabel->setOutlineColor(sf::Color::Black);

sf::FloatRect numRect = lifelabel->getLocalBounds();
sf::Vector2f numRectCenter(numRect.width / 2.0 - numRect.left, numRect.height / 2.0 + numRect.top);
lifelabel->setOrigin(numRectCenter);


See the RED circle in the attachment. The Text within the BLUE one is centered by hand and therefore not resolution-independent.

Would be glad for any help!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Setting Text origin driving me insane
« Reply #1 on: August 04, 2018, 10:11:24 pm »
Did you realise that you're adding numRect.top but subtracting numRect.left ?

Also, with circles it makes more sense to use the radius for the each origin member.

Note that a circle's local boundary is another case where left and top is not guaranteed to be zero. The width will take into account the outline so you'll need left and top of the local boundary too.
Try it without circle outlines to confirm that that is what is happening.
« Last Edit: August 04, 2018, 10:15:56 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Notion

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Setting Text origin driving me insane
« Reply #2 on: August 04, 2018, 10:40:19 pm »
Did you realise that you're adding numRect.top but subtracting numRect.left ?
Yea, that were at least the signs, that pushed the text into the write direction :D

Quote
Note that a circle's local boundary is another case where left and top is not guaranteed to be zero. The width will take into account the outline so you'll need left and top of the local boundary too.
Try it without circle outlines to confirm that that is what is happening.

Thanks. I love you. That solved it :D