-
Hi everyone, i'm having an issue.
in the constructor of the class Starship i put this:
sprite.setPosition((float)sf::VideoMode::getDesktopMode().width/2, (float)sf::VideoMode::getDesktopMode().height/2);
to have the starship sprite in the center of the screen whatever resolution the window is using.
The problem is that the sprite is always constructed in 0,0 even if i put random integers in the setPosition call.
What am i doing wrong?
-
The issue is probably somewhere else in your code.
Can you provide a complete and minimal example that reproduces the issue?
-
why are you using desktop mode, instead of your sf::RenderWindow variable?
I would do it like sprite.setPosition((float)render_window.getSize().x/2-sprite.getSize().x/2, (float)render_window.getSize().y/2-sprite.getSize().y/2)
-
I would say that it's more reliable to use the view rather than the size of the window since the view is the actual co-ordinates used for the positioning.
And, since you want the centre and the view knows what its centre is, you can just get that:
sprite.setPosition(view.getCenter());
Or, if you don't have a separate view, you can get the view from the window:
sprite.setPosition(window.getView().getCenter());
Remember, though, that the top-left corner of the sprite will be placed in the centre of the window unless you alter the sprite's origin. You probably want it to be in its centre:
sprite.setOrigin(sf::Vector2f(sprite.getTexture().getSize() / 2u));