100% cpu cycle issue

Started by
16 comments, last by kordova 20 years, 11 months ago
also, because of the minimum sleep time on windows ... you will want to pad your "b. sleep for time between current and needed elapsed" so that if this number is below a certain threshold, don''t sleep anyway.
Advertisement
Xao wrote:
also, because of the minimum sleep time on windows


Just use Sleep(0), which will only yield the cpu time to threads of equal or higher priority....
Game production:Good, quick, cheap: Choose two.
If you''re on a laptop with SpeedStep CPU, the Windows kernel will "helpfully" reduce the speed of the CPU if you call Sleep(). So calling that between each frame will cause jerkiness.

All games I know of just sit in a tight PeekMessage() loop, and step/render when PeekMessage returns 0 (or, if they want a fixed frame rate, just re-call PeekMessage()). As the poster said, you can switch to GetMessage() when you''re in the background, and post a WM_TIMER to keep the game moving forward a bit if necessary.

Don''t trust WM_TIMER to be accurate enough for rendering frames, though.
quote:Original post by kordova
WinMain1. 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  



Try these:
WinMain1. Initialize Windows etc (if any problems exit)2. While (TRUE)    if peekmsg      - Translate & dispatch  msg    else if game not sleeping (paused, minimized etc)      - enter per frame game loop    else        - ::WaitMessage();  

"after many years of singularity, i'm still searching on the event horizon"
What is WaitMessage()?
quote:Original post by kordova
What is WaitMessage()?


quote:From MSDN
The WaitMessage function yields control to other threads when a thread has no other messages in its message queue. The WaitMessage function suspends the thread and does not return until a new message is placed in the thread''s message queue.



Qui fut tout, et qui ne fut rien
Invader''s Realm
WaitMessage is the correct way of reducing your CPU cycle down to 0% (if your app is not in focus). Respond to WM_ACTIVATEAPP, when your app lost its focus, call WaitMessage().

If your game is running and in focus, 100% CPU usage is normal.

Current project: A puzzle game.
% completed: ~10%
Status: Active.
Ah, ok, yeah WaitMessage made a huge difference. Many thanks from us both!

This topic is closed to new replies.

Advertisement