SFML community forums

Help => General => Topic started by: manabreak on October 05, 2013, 01:45:52 pm

Title: SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)
Post by: manabreak on October 05, 2013, 01:45:52 pm
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?
Title: Re: SFML 2.1 - High CPU usage
Post by: CalledMaster on October 05, 2013, 05:00:18 pm
You can limit you framerate like this guy does it:
http://obviam.net/index.php/the-android-game-loop/ (http://obviam.net/index.php/the-android-game-loop/)

Yes, it's for android but you can just port the logic. :)
Title: Re: SFML 2.1 - High CPU usage
Post by: manabreak on October 05, 2013, 06:08:28 pm
You can limit you framerate like this guy does it:
http://obviam.net/index.php/the-android-game-loop/ (http://obviam.net/index.php/the-android-game-loop/)

Yes, it's for android but you can just port the logic. :)

Thanks, but I tried to explain that I tried similar solution already (with SFML threads), and it didn't work. Profiling says that sfml-graphics-d-2.dll is the one hogging the resources. Sleep time looks correct, though, the main thread sleeps for 15 ms or so each frame, but it's still taking a full core of CPU usage.
Title: Re: SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)
Post by: G. on October 05, 2013, 07:45:53 pm
vSync and setFramerateLimit are not supposed to be used together. Have you tried using only vSync and no setFramerateLimit?
Title: Re: SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)
Post by: manabreak on October 05, 2013, 08:52:26 pm
vSync and setFramerateLimit are not supposed to be used together. Have you tried using only vSync and no setFramerateLimit?
I have, but it causes the same problem. Only by disabling vsync I get the intended result.
Title: Re: SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)
Post by: Ixrec on October 05, 2013, 09:24:16 pm
What's your graphics card?

Any issues with vsync probably have more to do with the card and/or drivers than SFML.
Title: Re: SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)
Post by: manabreak on October 06, 2013, 06:46:46 am
What's your graphics card?

Any issues with vsync probably have more to do with the card and/or drivers than SFML.

I'm using GeForce Ti 560.