|
-
February 2nd, 2005, 10:12 PM
#1
Creating a reliable timer
This question concerns how to create a timer; This timer is used in the topic of a game program, but no gaming knowledge is necessary to answer.
In the case of a small direct draw game, 30 FPS doesn't require 100% CPU on a really powerful system.
The problem I'm having is that my 'game loop' (which is simply my message loop + my game functions) consumes 100% CPU because I use PeekMessage and while() to wait for 33ms to go by so that I can begin the next iteration. This ensures that I loop 30 times per second (at most)
The thing is, this while loop consumes 100% CPU while it is waiting for 33ms to go by. I don't like this. Here is my loop:
Code:
while(true)
{
Timer = GetTickCount();
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(AllowGame)
{
GameInput();
GameAI();
GameRender();
}
while(GetTickCount() - Timer < (unsigned)1000 / FPS);
}
Now, if I used Sleep(), the CPU would barely use 20%, because Sleep doesn't hog the CPU like while() does. The thing is, Sleep() isn't as accurate by the millisecond as GetTickCount is.
I need a way to be able to limit my iterations as efficiently as possible by milliseconds, but WITHOUT consuming more CPU that needed.
Any suggestions?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|