Using 80+ of my CPU

Started by
11 comments, last by Programmer16 20 years, 1 month ago
quote:Original post by Xai
...but you KNOW you do not need it when you have no messages in the message queue...
Take a look at his loop structure. GameLoop() gets called when there are no messages in the message queue. Which, I presume, would not be an ideal time to sleep.

The truth is that we have terrible notions of the main game loop, and particularly of handling the Windows message pump. Introductory texts set up a basic loop/pump based on the notion that the game updates single step (as opposed to interpolating multiple step), resulting in these debates.

Advertisement
ah, well I don''t have a game loop that does anything not using the message queue ... i just put a message in the queue whenever I need to continue the operation after letting other messages get handled ...

for instance my 2d graphics library does a DrawScreen by posting a DrawScreenObject message (a custom message of my own) with the object to draw next ... inside the handler, it invokes draw, and posts another message to draw the next item (if there is one) ... so it stays busy USING the message loop ... until the process is complete ...

and of course, a game in which the time is passing even when the user is doing nothing has some sort of timer type message generation ..

truth be told ... my most advanced windows program does hog cpu, because I needed more acurate timers than you can get with the built in system ... but what I would do now I think is something like have a bool ... then my code would be something like:

while(1)
{
while(PeekMessage(&msg,...,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(!GenerateTimerMessages()) // return false when no timers are triggered
Sleep(0);
}

now ... i must admit I have never written really intense game ... like an FPS or anything .. the most active game I''ve written in windows is an 8 line video slot machine ... and so maybe these techniques are not efficient enough for many uses ... I don''t know.
and actually the above post is wrong anyway .. because I don''t let my timers not get checked if the queue never goes empty ... I actually have something in the inner while that counts to like 10 or so, and force checks the timers for every 10 messages that go by ... that way it can''t be completely starved ... just gets bogged down.

and as some extra sophistication, some of my messages are "update" or "trigger" type messages, such as "DrawFame" so these things don''t every post multiple entries into a backed up queue ... they are set to NotPostDups, so I have a flag I set when I post one, and clear when I handle it ... to keep things sane ... but other messages, MUST be posted no matter what, if their effect is cummunulative ...

This topic is closed to new replies.

Advertisement