Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kordova

100% cpu cycle issue

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm looking through a friend's source (I believe he originally based it from someone's book?) as he's using up 100% of his cpu cycles when running a very simple app. I'm certain it comes from this chunk as when I added the "else { Sleep(iTickTrigger ... " lines it dropped significantly. Anyway, is this the best way to handle it? It seems so to me but I don't want to misinform. Thanks ahead.
    
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  PSTR szCmdLine, int iCmdShow)
{
  MSG         msg;
  static int  iTickTrigger = 0;
  int         iTickCount;

  if (GameInitialize(hInstance))
  {
    // Initialize the game engine

    if (!GameEngine::GetEngine()->Initialize(iCmdShow))
      return FALSE;

    // Enter the main message loop

    while (TRUE)
    {
      if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      {
        // Process the message

        if (msg.message == WM_QUIT)
          break;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      else
      {
        // Make sure the game engine isn't sleeping

        if (!GameEngine::GetEngine()->GetSleep())
        {
          // Check the tick count to see if a game cycle has elapsed

          iTickCount = GetTickCount();
          if (iTickCount > iTickTrigger)
          {
            iTickTrigger = iTickCount +
              GameEngine::GetEngine()->GetFrameDelay();
            GameCycle();
          }
          else {
            Sleep((iTickTrigger-iTickCount));
          }
        }
      }
    }
    return (int)msg.wParam;
  }

  // End the game

  GameEnd();

  return TRUE;
}
    
[edited by - kordova on May 3, 2003 2:34:42 PM]

Share this post


Link to post
Share on other sites
Advertisement
A game usually uses all the CPU power available so a usage of 100% is absolutetly normal (look at commong game and judge yourself). It draws as many FPS as possible so it should be using maximum CPU.

hope that helped,
cya,
Phil

Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this question a few times over in the states )

Share this post


Link to post
Share on other sites
Yeah, but I received 100% cpu when i set his global fps to 1. I''m pretty sure that it sits in an infinite loop that only calls the "gamecycle" when the determined time has elapsed, though it continues to loop even when theres many hundred milliseconds when there should be no activity. My only concern with my approach is that peekmsg won''t be hit very frequently though that may not matter (?)

Share this post


Link to post
Share on other sites
edit: upon reading all the code, I have no idea what you're attempting to do, but I'm fairly certain there's an easier way.

You can call Sleep() each frame to relinquish some CPU time to other processes. However, even if you only call Sleep(0) (the parameter is the number of milliseconds to give up), your maximum framerate will be 100, since Sleep is only accurate to about 10ms. So, you can either live with 100% usage, limit your FPS to 100, or come up with a creative way to call Sleep(10) every X number of frames. You get the idea.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on May 3, 2003 2:50:01 PM]

Share this post


Link to post
Share on other sites
Is 100% an ok thing to do regardless of size of the game? I''ve always avoided it as best as possible personally but then who am I?

Share this post


Link to post
Share on other sites
As long as your game has focus, you can use most of the processor (not ALL...this isnt dos). If your game is out of focus you should try to use as close to 0% as possible...because it doesnt matter.

Brian J

Share this post


Link to post
Share on other sites
Well a process should use as much CPU time as needed when it is in the foreground as long as it is doing meaningful work (i.e. not just spinning endlessly in a tight loop.)

Share this post


Link to post
Share on other sites
Ok thats what I thought. Is this an ok way to deal with the current setup:

WinMain
1. Initialize Windows etc (if any problems exit)
2. While (TRUE)
a. peekmsg
else
b. if game not sleeping (deactivated, minimized etc)
1. if enough time has elapsed
a. call engine work function
else
b. sleep for time between current and needed elapsed

Not sure if you can follow that, but that's the best an understanding I can get from this. The only thing I've added is the sleep portion and again im pretty sure if i change something in here it should make the difference. Thanks again all.

[edited by - kordova on May 3, 2003 3:41:24 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by ZealousElixir
edit : upon reading all the code, I have no idea what you''re attempting to do, but I''m fairly certain there''s an easier way.



Switch between GetMessage and PeekMessage when you receive WM_ACTIVATEAPP.

MSN

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!