Actually its just because of framerate limit you set. You should really read the section "
Controlling the framerate" of this documentation :
https://www.sfml-dev.org/tutorials/2.6/window-window.phpHowever, I've made some improvements to the code. Maybe you should try it :
#include <SFML/Graphics.hpp>
int main()
{
sf::VideoMode desktopMode = sf::VideoMode::getDesktopMode(); //Getting desktop mode of the screen (full screen size)
sf::RenderWindow window(desktopMode, "Screensaver", sf::Style::Fullscreen);
//setting some constants
const float WIN_WIDTH = desktopMode.width;
const float WIN_HEIGHT = desktopMode.height;
sf::CircleShape shape(50.f);
shape.setOrigin(50, 50);
shape.setFillColor(sf::Color::Green);
shape.setPosition(shape.getRadius(), WIN_HEIGHT / 2.f); //Ball starts from middle left of the screen
/* To make movement independent of frame rate*/
sf::Clock clock;
float dt = 0;
float multiplier = 60;
float xVelocity = 5; //Ball will go forward in the start
float yVelocity = -5; //Ball will go upward in the start
sf::Event event; //Moved it out of game loop, because it was useless to move it in loop and creating it in each iteration
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close();
}
/*Point to remember is that, shapes origin is at its center, so we have to keep this in mind*/
if (shape.getPosition().x < shape.getOrigin().x || shape.getPosition().x >= (WIN_WIDTH - shape.getOrigin().x)) {
xVelocity = -xVelocity; //Some basic logic
}
if (shape.getPosition().y < shape.getOrigin().x || shape.getPosition().y >= (WIN_HEIGHT - shape.getOrigin().y)) {
yVelocity = -yVelocity;
}
dt = clock.restart().asSeconds(); //restarting clock on each frame recording time elapsed in the dt variable
//Using move rather than setPosition
shape.move(xVelocity * dt * multiplier, yVelocity * dt * multiplier);
//Drawing stuff
window.clear();
window.draw(shape);
window.display();
}
return 0;
}