Reputation: 185
I'm trying to get my cursor sprite to try and appear on top of my mouse cursor but for some reason it is not appearing on top of the mouse cursor.
Code:
cursorSprite.setPosition(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y);
As you can see, I'm simply using setPosition() with my sprite, and setting it at the x and y positions of the mouse. However, this is not working and the cursor sprite is appearing at a different location to my actual mouse cursor location.
Why? Could it be something to do with how I'm setting up my window?
window(sf::VideoMode(800, 600, 32), "SFML Test", sf::Style::Default)
Upvotes: 3
Views: 1560
Reputation: 2623
There are two main coordinate systems that should be considered when running SFML app (as well as pretty much every other application) in windowed mode: screen coordinates and windowed coordinates.
You are getting wrong results because sf::Mouse::getPosition()
return click position in screen coordinates and you want click in window coordinates.
You can manually transform screen coordinates to window coordinates, but it is much better to use this interface provided by SFML:
sf::Vector2i pos = sf::Mouse::getPosition(window);
The pos
will be in window coordinates.
Upvotes: 4