I'm experiencing high CPU usage with SFML 2.1.
sf::RenderWindow window(sf::VideoMode(1280, 720), "Hello SFML", sf::Style::Default, settings);
window.setFramerateLimit(60);
while(window.isOpen())
{
window.clear(sf::Color::Black);
window.display();
}
This does not seem to limit the FPS correctly. I've also tried using sf::Clock and sleeping for the remainder (1.f / 60.f - clock.getElapsedTime().asSeconds()), but it doesn't seem to help. I clocked the sleep time, and it seems to be correct as well, but something hogs the CPU. I use only a single thread (was using multiple threads, but stripped the code down and it still does that). Any ideas?
EDIT:
I did some testing. My sleeping code is like this:
// Inside the main loop
clock.restart();
// Do the drawing etc.
sf::Time elapsed = clock.getElapsedTime();
float sleepTime = 1.f / 60.f - elapsed.asSeconds();
if(sleepTime > 0.f)
{
sf::sleep(sf::seconds(sleepTime));
}
Now, with 1.f / 60.f (60 FPS limiter), it hogs the CPU, but drops to zero when using 1 FPS limiter. If I set it to 1.f / 30.f (30 FPS), it stays around 4-5 % usage. Profiler says that window.clear(sf::Color::Black) takes the most of the processing time.
EDIT 2:
Okay, I solved it by turning off VSync. Why is that? Why does enabling VSync cause CPU usage to skyrocket?