Hello, I am trying to dynamically position a sf::Text up and down based on keyboard input. The problem is, when I set the position by incrementing its location, I get different values of movement when I move it up or down even if I increment/decrement by the same amount. Here is a simple version:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "Move Text Test");
sf::Font fontMovable;
fontMovable.loadFromFile("arial.ttf");
sf::Text txtMovable;
txtMovable.setFont(fontMovable);
txtMovable.setString("Testing");
txtMovable.setColor(sf::Color::White);
txtMovable.setCharacterSize(20);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed)
if(event.key.code==sf::Keyboard::Up){
txtMovable.setPosition(txtMovable.getGlobalBounds().left, txtMovable.getGlobalBounds().top - 10);
}else if(event.key.code==sf::Keyboard::Down){
txtMovable.setPosition(txtMovable.getGlobalBounds().left, txtMovable.getGlobalBounds().top + 10);
}
}
window.clear();
window.draw(txtMovable);
window.display();
}
return 0;
}
As you can see, I increment by 10, but when I move the sf::Text up, it actually goes up by 5 and going down actually goes down by 10. These numbers change based on the size of the font. I know there is probably something I am not understanding about the getGlobalBounds().
I am using SFML 2.1 and Visual C++ Express 2010.
Any help would be appreciated. Thanks.