Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)  (Read 3614 times)

0 Members and 3 Guests are viewing this topic.

manabreak

  • Newbie
  • *
  • Posts: 4
    • View Profile
SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)
« 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?
« Last Edit: October 05, 2013, 06:21:05 pm by manabreak »

CalledMaster

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML 2.1 - High CPU usage
« Reply #1 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/

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

manabreak

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML 2.1 - High CPU usage
« Reply #2 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/

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.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)
« Reply #3 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?

manabreak

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)
« Reply #4 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.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)
« Reply #5 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.

manabreak

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML 2.1 - High CPU usage (SOLVED) (Need explanation still)
« Reply #6 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.