My SFML programs stutter about 80 ms every half second. I've created a simple program of a sprite moving to demonstrate. Removing the event loop stops the stuttering.
The length of time of each game loop iteration, dt, is printed. dt is usually 0.3 ms, except for every half second or so, when it is 80 ms. And the sprite's stuttering is clearly visible. If I remove the event loop, dt goes up to only 2 ms every half second or so, and the sprite moves smoothly.
#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(1920, 1080), "GAME TEST 1");
sf::Texture texture;
texture.loadFromFile("media/cat.png");
sf::Sprite sprite;
sprite.setTexture(texture);
//physics
sf::Vector2f position(50, 100);
sf::Vector2f velocity(20, 0);
sf::Clock clock;
while (window.isOpen())
{
float dt = clock.restart().asSeconds();
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
default:
break;
}
}
// update physics
position += velocity*dt;
sprite.setPosition(position);
//render
window.clear(sf::Color::Red);
window.draw(sprite);
window.display();
//report
std::cout << dt << std::endl;
}
return 0;
}
Even if the event loop is empty the stuttering occurs. Replacing the loop with sf::Joystick::update() also causes stuttering. VSync makes dt more like 16 ms, but it still stutters at 80 ms at the same rate.
I am using:
Visual Studio 2015
Windows 10
SFML 2.4.1 C++14
All static SFML libraries
I've also tried SFML 2.4.0 and 2.3.2 with no success. Any help would be greatly appreciated!