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

Author Topic: Positioning relative to sf::RenderWindow isn't quite working as expected...  (Read 1286 times)

0 Members and 1 Guest are viewing this topic.

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
So I have an std::vector of sf::Text items I'd like to distribute evenly along a vertical axis in the center of the screen.  Here is what I do:

    short CharSize = 16;
    for (int ii = 0; ii < m_WeaponData.size(); ++ii)
    {
        //First, set the text object's origin
        //Use zero as the origin's Y, because if you use half the height, hanging characters such as (g,j,p,q,y) will make things uneven
        //Use 4 instead of 2 because... I have no idea.
        m_WeaponData[ii].setOrigin(m_WeaponData[ii].getLocalBounds().width / 4, 0);
        //Then, position it, making sure that each element is below the last
        m_WeaponData[ii].setPosition(m_App->getSize().x / 2,
                                     30 + ii * (CharSize + 2));
    }

Where m_App is of type sf::RenderWindow*.

My question is this: why am I dividing the local bounds by 4?  If I use 2, as would seem to make sense (that should put the origin at the halfway point of the sf::Text's X side, since the width divided by 2 is half the width...), the end of the text lines up with the center of the screen, rather than the center.  Using 4, though, does line up the X center of the sf::Text with the X center of m_App (which I assume I am obtaining correctly, right?).

I can only assume I am doing something wrong, but what?  Why does getLocalBounds().width / 2 not give me the proper results?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
I'm not sure, but it could be related to the issue #216...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/