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

Author Topic: 50% CPU usage... thoughts on improvement  (Read 4210 times)

0 Members and 1 Guest are viewing this topic.

Sivak

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
50% CPU usage... thoughts on improvement
« on: April 01, 2010, 07:44:19 pm »
Hey all.  I'm using VCpp 2008 express and Windows XP Pro.

Well, I've added plenty of stuff to the game such as sounds and bullets.  I've had upwards of 300 bullets on the screen without slowdown, but when monitoring this in the task manager, it's always at 50% no matter where I am and no matter how little is actually going on...  I've looked at other games, and they never seem to go beyond 15% except when loading between things.  In debug mode, slowdown does happen.  Release mode is fine (for now).

Here's a breakdown of how I'm doing things:

I have singleton classes for the title screen and game.  These classes have their own sf::Image instances.  Most objects spawned from them have their own sf::Sprites.  I also have a singleton class for the sounds.  I only have 4 small sound effects thus far.

I could understand the game part maybe having some slowdown, but the title?  Is it normal for an SFML app?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
50% CPU usage... thoughts on improvement
« Reply #1 on: April 01, 2010, 08:12:14 pm »
Do you limit your framerate, with SetFramerateLimit or UseVerticalSync?
Laurent Gomila - SFML developer

Sivak

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
50% CPU usage... thoughts on improvement
« Reply #2 on: April 01, 2010, 08:15:14 pm »
Quote from: "Laurent"
Do you limit your framerate, with SetFramerateLimit or UseVerticalSync?


No, neither.  I did try setting VerticalSync to true as well as FrameLimit to 60, but same result.  50-55%.  I turned them back off.

UPDATE:  Well, I did take out this line just to see what would happen and I got a very strange result.

Code: [Select]

App.Display();
//Wait to get a proper time in-between frames...
/* bool bGo = true;
while (bGo)
{ float Time = frameClock.GetElapsedTime();
if (Time >= 0.0166)
bGo = false;
}*/


Removing that got the CPU to be okay, but the frame rate went to like 34.  But the WEIRD thing is I let the game go for a while and as soon as like 200 bullets were on the screen it was fine!!!  I killed off the enemies and then the game went back to a crawl...

So it seems like the game having to update more makes the game go faster, which makes no sense!

The title screen also doesn't go very fast, and that updates very little...

Sivak

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
50% CPU usage... thoughts on improvement
« Reply #3 on: April 02, 2010, 05:37:41 am »
Hm...  I have another updated.  I added:  Sleep(1); to the while loop and that seemed to fix the CPU problems.  I had to make the time it compared to be something like 0.011 instead of 0.0166...  

I'm only wondering if this solution is not the best.

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
50% CPU usage... thoughts on improvement
« Reply #4 on: April 02, 2010, 09:19:08 am »
Can you show us your main loop ?
Mindiell
----

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
50% CPU usage... thoughts on improvement
« Reply #5 on: April 02, 2010, 10:58:25 am »
SFML has built-in frame rate limiting. Look at:

sf::RenderWindow::EnableVerticalSync
sf::RenderWindow::SetFrameRateLimit

Edit: Just saw that you did that. Test it with a minimal game loop from the tutorials, though.
Listen to my band: pencilcase.bandcamp.com

Sivak

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
50% CPU usage... thoughts on improvement
« Reply #6 on: April 02, 2010, 04:54:35 pm »
Quote from: "Mindiell"
Can you show us your main loop ?


Sure...

Code: [Select]

while (bGameGoing && App.IsOpened())
{ App.Clear(); //Wipe screen
playerInput.GetGameplayKeys();
//Escape goes to exit
//-------------------------------------------------------------------------------------------
if (playerInput.bEscKey)
{ bGameGoing = false;
gameState = 1;
}


//-------------------------------------------------------------------------------------------
//Update anything...
//-------------------------------------------------------------------------------------------
it1 = GameObjs.begin();
while(it1 != GameObjs.end())
{ //(*it1)->Update();
if((*it1)->Update())
{ delete *it1;
it1 = GameObjs.erase(it1);
}
else
++it1;
}

//-------------------------------------------------------------------------------------------
//Display!
//-------------------------------------------------------------------------------------------
/-------------------------------------------------------------------------------------------
Draw all objects...
//-------------------------------------------------------------------------------------------
for (int drawPass = 1; drawPass < 7; drawPass++)
{ for(it1 = GameObjs.begin(); it1 != GameObjs.end(); ++it1) //Go through the list and update ALL!
{ if ((*it1)->drawPriority == drawPass)
(*it1)->Draw();
}
}

//A bunch of App.Draw statements go here.  I left them out as they aren't relevant.

App.Display();
//Wait to get a proper time in-between frames...
//-------------------------------------------------------------------------------------------
bool bGo = true;
while (bGo)
{ float Time = frameClock.GetElapsedTime();
if (Time >= 1.f/60.f)
bGo = false;
}

frameRate = 1.f / frameClock.GetElapsedTime();
frameClock.Reset();
}


Basically, the while Loop at the end is what makes the CPU go to 50%.  If this while loop is NOT present and I use the setFramerate to 60, the game lags big time until it's updating a lot of stuff.

The while loop at the end, if I add:  else, Sleep(1); and change it to 1.f/80.f seems to work, but that just feels sloppy.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
50% CPU usage... thoughts on improvement
« Reply #7 on: April 11, 2010, 05:58:29 am »
loops eat CPU

It's better to use function calls with a threaded timer to delay.

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
50% CPU usage... thoughts on improvement
« Reply #8 on: April 11, 2010, 11:36:46 am »
Here:
Quote from: "Sivak"
Code: [Select]
//Wait to get a proper time in-between frames...
//-------------------------------------------------------------------------------------------
bool bGo = true;
while (bGo)
{ float Time = frameClock.GetElapsedTime();
if (Time >= 1.f/60.f)
bGo = false;
}

You're actively checking the time every iteration of the while, you should make the program sleep instead.
Something like this:
Code: [Select]
float Time = frameClock.GetElapsedTime();
sf::Sleep(1.f/60.f - time);
Pluma - Plug-in Management Framework