Win32 message loop and CPU usage
-
I'm developing an animated game on Win32. my message-loop looks like this:
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else //"idle" state. Call graphic rendering functions. Not that they are simple and speedy.
{
CPTWindowManager::GetInstance()->RenderAllGames();
}
}I have noticed that takes 50% of the CPU. I wonder if this 50% are "for real". If so, it is not desirable. I tried to use
GetMessage()
but I don't know how to figure out the "idle" state when usingGetMessage()
. Note that the rendering stuff are relatively simple rendering and thus the time they take is unnoticeable. -
I'm developing an animated game on Win32. my message-loop looks like this:
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else //"idle" state. Call graphic rendering functions. Not that they are simple and speedy.
{
CPTWindowManager::GetInstance()->RenderAllGames();
}
}I have noticed that takes 50% of the CPU. I wonder if this 50% are "for real". If so, it is not desirable. I tried to use
GetMessage()
but I don't know how to figure out the "idle" state when usingGetMessage()
. Note that the rendering stuff are relatively simple rendering and thus the time they take is unnoticeable.No matter how long the "rendering stuff" is taking, you have a busy loop all the time: there's nowhere a 'pause' in the loop (sleep or wait for something...). But don't worry too much, that's usually how games are written: you want to achieve the best possible performance, so it's normal that you will eat up all remaining CPU time. In your case it is probably taking 50% of the CPU because you have a dual-core, am I right ?
Cédric Moonen Software developer
Charting control [v1.4 - Updated] -
No matter how long the "rendering stuff" is taking, you have a busy loop all the time: there's nowhere a 'pause' in the loop (sleep or wait for something...). But don't worry too much, that's usually how games are written: you want to achieve the best possible performance, so it's normal that you will eat up all remaining CPU time. In your case it is probably taking 50% of the CPU because you have a dual-core, am I right ?
Cédric Moonen Software developer
Charting control [v1.4 - Updated]Yes I have a dual-core. Also, I don't 'feel' the 50% slowing down my windows experience. I have, however, saw a similar game that when run only eats several %. Perhaps they did some scheme with the
WM_PAINT
messages - they cause the window to refersh, byInvalideRect()
I guess, and render on theWM_PAINT
s. Sound like something I should try ? Thanks -
Yes I have a dual-core. Also, I don't 'feel' the 50% slowing down my windows experience. I have, however, saw a similar game that when run only eats several %. Perhaps they did some scheme with the
WM_PAINT
messages - they cause the window to refersh, byInvalideRect()
I guess, and render on theWM_PAINT
s. Sound like something I should try ? ThanksIt depends which kind of game it is. For example, if it is something static (like solitaire, mine sweeper, ...) then you don't need to repaint the view dynamically. You can simply repaint the view when something changed. So, I can't really answer your question, it depends of the kind of game you are trying to write.
Cédric Moonen Software developer
Charting control [v1.4 - Updated] -
It depends which kind of game it is. For example, if it is something static (like solitaire, mine sweeper, ...) then you don't need to repaint the view dynamically. You can simply repaint the view when something changed. So, I can't really answer your question, it depends of the kind of game you are trying to write.
Cédric Moonen Software developer
Charting control [v1.4 - Updated]