Sign in to follow this  

Controlling Fullscreen Win C++ DX program's processor usage %

This topic is 4717 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

When I'm alt tabbing out of my program it will still use 99-100% of the computer's processing power, therefore not really launching any programs before I quit the game and only capable of really doing something on a window which was opened prior to launching my program. I'd like it to use a lot less processing power when it's inactive. Here's the code which gets processed when the game is not the active window.
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	if(msg == WM_SETCURSOR || msg == WM_MOVE) return 0;
	else if(msg == WM_DESTROY || (msg == WM_SYSCOMMAND && wParam == SC_CLOSE))
	{
		PostQuitMessage(0);
		isgamerunning = false;
		return 0;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

	do
	{
		if(PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		game->update_game();
	}
	while(isgamerunning);

void BoNgame::update_game()
{
	HRESULT hr;
	hr = MGEgfxref->lpdevice->TestCooperativeLevel();
if(hr != D3D_OK)
	{
		if(hr == D3DERR_DEVICELOST)
		{
			if(dolostdevice)
			{
				MGEinputref->lpkeydevice->Unacquire();
				MGEinputref->lpmousedevice->Unacquire();
				MGEgfxref->d3dxsprite->OnLostDevice();
				MGEgfxref->d3dxline->OnLostDevice();
				for(int i = 0; i < 12; ++i)    if(MGEgfxref->font[i]) delete MGEgfxref->font[i];
				MGEsoundref->stop(0);
				dolostdevice = false;
			}
			return;
		}
//ETC ETC........................
}

Share this post


Link to post
Share on other sites
maybe try adding this check in your game loop

if( hWnd == GetFocus() )
game->update_game();


Then you only do your update when the game's window is in focus

Hope that helps..

Share this post


Link to post
Share on other sites
You will also need to have some kind of pause, such as sleep(), otherwise your program will still use a majority of the clock cycles as it's running through the loop as fast as it can waiting for the window to get focus again.

So something like the above, but with a sleep() function, for example:


if(hWnd == GetFocus())
{
game->update_game();
}
else
{
Sleep(100); // Relinquishes priority of this program for 100ms
}

Share this post


Link to post
Share on other sites
Okay thanks I did it a bit differently though (using Sleep() anyway) and now it's all good and doesn't seem to use just about any processing time when it's inactive.

Share this post


Link to post
Share on other sites

This topic is 4717 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this