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

Author Topic: [SOLVED] SFML using 100% CPU (with limits on)  (Read 2027 times)

0 Members and 1 Guest are viewing this topic.

MTMBStudios

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] SFML using 100% CPU (with limits on)
« on: September 13, 2011, 09:29:11 pm »
I have been trying to solve this problem since yesterday. I have chopped down my code down to nothing but the beginning exercise and the limit (I am using SFML 2)

Code: [Select]
       _render.SetFramerateLimit(60);
while(_render.IsOpened())
{
sf::Event currentEvent;
if(_render.PollEvent(currentEvent))
{
if(currentEvent.Type == currentEvent.Closed)
_render.Close();

_render.Clear();

_render.Display();
}
}


but it still maxes out my CPU. I used FRAPS to check the framerate and it is not even hitting 60 frames per second. I have tried SFML 1.6, SFML 2 and the static and dynamic libraries and nothing has changed. Nothing changes between release or debug either. I have an ATI 4800 series card. This library seems great (especially in trying to port an XNA game) but I just cannot seem to get it working on my end.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
[SOLVED] SFML using 100% CPU (with limits on)
« Reply #1 on: September 13, 2011, 09:34:53 pm »
The way you have it coded, it will only do frames when events are in the queue, which is why you're getting such a low framerate.

Also when you're not running frames, you're spinning which is why you're eating up 100% CPU time.

The proper way to have a game loop would be like this:

Code: [Select]

while(_render.IsOpened())
{
  // empty the event queue
  sf::Event currentEvent;
  while(_render.PollEvent(currentEvent))  // <- while, not if
  {
    // notice in here we only do event stuff.  Do not render the scene here
    if(currentEvent.Type == currentEvent.Closed)
      _render.Close();
  }

  // NOW render the scene
  _render.Clear();

  _render.Display();
}


The idea is you want to render scenes unconditionally in a continuous loop.  The PollEvent thing just checks to see if any events have occurred that need your attention.

MTMBStudios

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] SFML using 100% CPU (with limits on)
« Reply #2 on: September 13, 2011, 09:48:28 pm »
Wow I am an idiot. Instead of doing all that debugging I should have just reread the tutorial again. Alright this appears to be solved.