So there I was trying to display a snippet of text XD
Unfortunately, there seems to be some "invisible" padding that does not display the text at the requested position (0,0).
The text seems to be shifted downwards like maybe 10px? (Even though it says its position is what I made it)
The font is mono spaced.
Here is some code that reproduces the "weird" behavior
sf::Text text;
text.setFont(Resources::getGameFont());
text.setCharacterSize(50);
text.setString("Hand");
text.setPosition(0,0);
text.setFillColor(sf::Color::Black);
while (window.isOpen())
{
while (window.pollEvent(event))
if (event.type == sf::Event::Closed)
window.close();
window.clear(sf::Color::Yellow);
window.draw(text);
window.display();
}
From what I understand, the default origin of text objects is their top left corner?
I've also tried 3 other fonts and they all seem to have the same problem
I've linked an image that shows this strange behavior on one of the fonts
I edited the code a little (I inserted the following just before the while loop):
std::cout << "gb_box.left = " << text.getGlobalBounds().left << std::endl;
std::cout << "gb_box.top = " << text.getGlobalBounds().left << std::endl;
std::cout << "lb_box.left = " << text.getLocalBounds().left << std::endl;
std::cout << "lb_box.top = " << text.getLocalBounds().top << std::endl;
And I got:
gb_box.left = 1
gb_box.top = 1
lb_box.left = 1
lb_box.top = 19
Is this normal? I mean, shouldn't the 'left' and 'top' members be equivalent to the 'x' and 'y' positions of the text?
Okay, after playing around a little, I found a somewhat funny "solution"
The following snippet was also inserted right before the main while loop (or in other words, after the last insertion).
while (text.getGlobalBounds().left != 0)
text.move(-1,0);
std::cout << "text_pos.x = " << text.getPosition().x << std::endl;
while (text.getGlobalBounds().top != 0)
text.move(0,-1);
std::cout << "text_pos.y = " << text.getPosition().y << std::endl;
The interesting thing is the output though
gb_box.left = 1
gb_box.top = 1
lb_box.left = 1
lb_box.top = 19
text_pos.x = -1
text_pos.y = -19
I'm just really curious whether this is supposed to happen XD or if I'm doing something wrong.
So I learned that the position of text is not always equivalent to the position of its bounding box, which was a tiny bit of a shock to my eyes XD.
So I guess, It's good to know that if I want to set the position of text to (x,y), what I'm really trying to do is set the position of the bounding box of that text to (x,y).