• Advertisement
Sign in to follow this  

Fewer resources for games while not active

This topic is 4648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi friends Is there anyway to use fewer resource when running d3d apps? when I press ctrl+alt+del and look at the cpu usage, I see a number almost always higher than 90% (!!!) while a good game shouldn't be so while now active. I know it has something to do with the windows message handler and the render loop, can anyone give me a good sample or tutorial? thanks a lot

Share this post


Link to post
Share on other sites
Advertisement
What you can do is track whether your application is active or inactive. Keep an enum in your application class somewhere, and change its value when you receive different windows messages (something like this):


// Inside your WndProc method:
case WM_ACTIVATEAPP:
{
if( wParam )
m_WinState = Active;
else
m_WinState = Inactive;
break;
}





Then, in your main windows loop, add some code like this:


while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
RenderScene();

if( m_WinState == Inactive )
Sleep( 30 );
}
}





The higher your sleep value, the more CPU time you'll free up to other apps while your application is inactive. If you have an FPS counter running, you'll see your game's FPS drop dramatically while you switch to other windows but then pick up instantly as soon as you switch back. With this technique, my application can take up 95%+ CPU time while active, and 0-5% while inactive.

Keep in mind that this is a simplified version of the solution. Ideally you would only want to sleep the rendering for 30ms, not frame computation and everything else.

Hope this helps~

Share this post


Link to post
Share on other sites
thanks for your solution
I'm goint to test it, and I hope I can fix this problem whit your plan.

Share this post


Link to post
Share on other sites
Quote:
Original post by kosmon_x
Then, in your main windows loop, add some code like this:

You might want to consider using wait instead of sleep, kosmon:
if( m_WinState == Inactive )
WaitMessage();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement