I'm simply trying to have it so my sprite moves smoothly across the screen.
It stutters. I tried reading the FAQ about this but I'm still trying to process why it's stuttering.
I also tried limiting the windows to 60FPS and it still stutters, while also disabling/enabling vertical sync.
They all seem to produce the same stuttering result.
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML works!");
window.setFramerateLimit(60);
sf::CircleShape shape(10.f);
shape.setFillColor(sf::Color::Green);
float x = 0.0f;
float y = 0.0f;
float hspeed = 50.0f;
float vspeed = 50.0f;
sf::Clock clock;
sf::Texture texture;
if (!texture.loadFromFile("test_sprite.png"))
{
return EXIT_FAILURE;
}
sf::Sprite sprite;
sprite.setTexture(texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
float delta = clock.restart().asSeconds();
//cout << "delta: " << delta << endl;
x+= hspeed * delta;
y+= vspeed * delta;
sprite.setPosition(x, y);
window.clear();
window.draw(sprite);
window.display();
}
return 0;
}
Also on a side note, why is there a value greater than 0 being outputted by this line:
delta = clock.restart().asSeconds();
If you restart the clock, shouldn't it be 0? When I cout the value it's usually 0.001-something. But didn't we restart the clock making it 0?
Also on a side note, why is there a value greater than 0 being outputted by this line:
delta = clock.restart().asSeconds();
If you restart the clock, shouldn't it be 0? When I cout the value it's usually 0.001-something. But didn't we restart the clock making it 0?
This function puts the time counter back to zero. It also returns the time elapsed since the clock was started.
This is straight from the documentation.