Skip to main content
gamedev.net
🔒 Locked

Processor Usage - WinMain

Started by NoLoGiC Mar 31, 2004 at 4:29 PM 6 replies 600+ views
Original Post
NoLoGiC
NoLoGiC
Hey all! With the following code to process Windows Messages in my WinMain, I''ve noticed processor usage always seems to be 100%: while(true) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } } return(msg.wParam); I thought I remember reading somewhere that there was a way to avoid using a WHILE statement, which in turn only processes Windows Messages when they occur, reducing the processor load on the application. Am I off base? Was I dreaming? Does it really matter in a hybrid Win-GUI DirectX game? Thanks for reading, NoLoGiC
Oluseyi
Oluseyi
To process messages only when they occur, use GetMessage, which blocks while waiting for a message, instead of PeekMessage:
while(true)
{
ret = GetMessage(&msg, NULL, 0, 0); // ret can be -1, 0, 1
if( ret == -1 )
{
// error occured
}
else if( ret )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
break;
}
Oluseyi
Oluseyi
Note that while the thread is blocked waiting for a message, nothing else can happen. If you''re using GetMessage, you might want to create a separate thread for application processing and call it at sufficiently regular intervals. Alternatively, you could use PeekMessage from within your game loop, but limit the number of times per second it gets called.
McZ
McZ
If I want to use the PeekMessage(..); in a game loop could I restrict it to just check less.. will messages be queued until I call PeekMessage?


e.g.

// instead of this:

while(true)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
render();
}
}

/**
I could do this instead
*/


int g_iCounter = 0; // a global counter


int CheckMess(void)
{
if( g_iCounter > 10 )
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return -1; // so it knows to quit


TranslateMessage(&msg);
DispatchMessage(&msg);
}
g_iCounter = 0;
}
else
{
// becouse counter was less or equal then 10

// we just update the counter for the next time.

g_iCounter++;
}

return 0; // return 0 = no quit message recived

}

while(true)
{
if (CheckMessage == -1)
{
break;
}
else
{
Render();
}
}

Oluseyi
Oluseyi
quote:
Original post by McZ
will messages be queued until I call PeekMessage?
It''s called a message queue; what do you think?
makeshiftwings
makeshiftwings
In general, you WANT your game to use 100% of the CPU if it''s the only thing running. CPU usage is a good thing, it means you''re using the user''s hardware to its fullest. The CPU usage isn''t a sign that Windows is wasting time handling messages, making an app that is nothing but:

while(true) { //do nothing }

will also take up around 100% of the CPU.

Windows takes care of giving processor time to other apps running at the same time; if you minimize your app and start up Word or something, you will see that in Task Manager, CPU time gets shifted to Word when it needs it, and back to your app when Word is sitting there doing nothing.

The thing that you read was probably the thing about using while(true) and PeekMessage() (which you did) instead of using while(GetMessage()) as in some Windows samples, which is a bad idea because it will stall on the GetMessage waiting for something to happen.

PeekMessage is pretty fast, trying to call it less by using a counter as in the other post isn''t going to increase your FPS much.

Using Sleep() or setting your game''s process to Low Priority will drop your processor usage and give more time to background apps, but it will make your game really slow; I can''t see why you would want to purposefully drop your usage of system resources for anything other than an app that''s supposed to run unseen in the background.
alnite
alnite
quote:
Original post by makeshiftwings
Windows takes care of giving processor time to other apps running at the same time; if you minimize your app and start up Word or something, you will see that in Task Manager, CPU time gets shifted to Word when it needs it, and back to your app when Word is sitting there doing nothing.

Nope. Use that while(true) { } example of yours, and minimize it and see if its CPU usage drops down to 0. A running process is a running process. The main loop will still run regardless whether it's minimized or not.

quote:

Using Sleep() ... will drop your processor usage and give more time to background apps, but it will make your game really slow ; I can't see why you would want to purposefully drop your usage of system resources for anything other than an app that's supposed to run unseen in the background.
Woa.


It's okay to have 99% CPU usage when your game is active. But if we use your version of loop, the game will still use 99% CPU even when it's inactive. WaitMessage() will magically reduce your CPU usage to 0, but only use it when your game is inactive. When you minimize your game, your game won't receive any message, so call WaitMessage(). Since your game is inactive, the first message sent to you would be the activate message (the message that makes your game active again). Then your game is active again, then the loop shall continue as usual.

Oh, regarding that Sleep(), if you are afraid losing 1 milisecond processing time, you better not use it.

[edited by - alnite on April 1, 2004 5:23:56 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.