Skip to main content
gamedev.net
🔒 Locked

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

Started by Tertsi 2 replies 700+ views
Original Post
Tertsi
Tertsi
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) delete MGEgfxref->font;
				MGEsoundref->stop(0);
				dolostdevice = false;
			}
			return;
		}
//ETC ETC........................
}

moeron
moeron
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..
moe.ron
Daishim
Daishim
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}

I know only that which I know, but I do not know what I know.

Topic Locked

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

Sign in to reply to this topic.