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

Author Topic: Problems with positioning sf::Text objects  (Read 1352 times)

0 Members and 1 Guest are viewing this topic.

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Problems with positioning sf::Text objects
« on: October 14, 2012, 03:17:32 pm »
So this is similar to my previous post, but the problem has become more pervasive.  I am not able to sensibly come up with a solution for properly arranging a series of sf::Text objects along the center of the screen.  The loop looks like this:

short CharSize = 16;
for (int ii = 0; ii < m_WeaponData.size(); ++ii)
{
    m_WeaponData[ii].setOrigin(...);
    m_WeaponData[ii].setCharacterSize(CharSize);
    m_WeaponData[ii].setColor(sf::Color(255, 255, 255));

    //Then, position it, making sure that each element is below the last
    m_WeaponData[ii].setPosition(m_App->getSize().x / 2, 30 + ii * (CharSize + 8));
}

My initial attempt was to put the origin of the sf::Text objects at their visual center, as such:

m_WeaponData[ii].setOrigin(m_WeaponData[ii].getLocalBounds().width / 2, 0);

But I discovered that this doesn't actually position the origin to the proper place; instead, the end of each string, more or less, is centered at the middle of the screen (though there is some slight variation).  Instead, I had to do this:

m_WeaponData[ii].setOrigin(m_WeaponData[ii].getLocalBounds().width / 4, 0);

To get the texts aligned according to their visual centers, which makes no sense since width/4 should not be the horizontal center.

Now, however, I've decided that it would be more convenient for the user if the strings were all arranged such that they are centered around the colon ':' character that appears in each of them, which would neatly put labels on one side of the screen and data on the other side.  So to do that, I tried this:

m_WeaponData[ii].setOrigin(m_WeaponData[ii].findCharacterPos(m_WeaponData[ii].getString().find(sf::String(":"))));

What I get instead is a bunch of texts that are shoved off to the left at seemingly random distances, rather than centered on the only ':' character in each string.  However, when I read the values provided by .findCharacterPos(), they seem to be more or less correct (i.e. the farther the ':' into the string, the higher the value of x).

Note that I know that findCharacterPos() returns global coordinates; the sf::Text objects all start positioned at (0,0), however, so that shouldn't matter.

I find that the only way to almost properly position the objects is to ignore setOrigin() altogether and do this instead:

m_WeaponData[ii].setPosition(m_App->getSize().x / 2 - Origin.x / 2, 30 + ii * (CharSize + 8));

But the result is only approximately lined up, not perfectly lined up, and I don't even understand why that solution is better than finding the position of the ':' character.

So something is going wrong here, and I don't know what it is.  Could somebody please offer some advice on this?

GarrickW

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: Problems with positioning sf::Text objects
« Reply #1 on: October 14, 2012, 03:27:52 pm »
Ugh, how embarrassing. 

The problem was that I was calling setCharacterSize() AFTER calculating the origin.  Doing so beforehand solves all my problems.  Of course.

Nevermind then.  Hopefully this helps someone in the future with similar inattentiveness.

 

anything