why is the game shutting down?

Started by
7 comments, last by Svenorvar 14 years, 2 months ago
Okay, so in my very simple game using DirectX 10 the August 2009 SDK, I just added the feature of input from the keyboard. I have seen this bug before with previous SDKs so I don't believe its a problem with just this SDK but what happens is that I will change focus from the game to another application and the entire game will shutdown. Why?
Cal State Fullerton,Computer Science Student-----Just want to learn.
Advertisement
There could be a thousand reasons. All we can do is guess unless you give us more information...
Well here is some code.

This is from my main.cpp where the keyboard is initialized.
// init the keyboardif (!InitKeyboard(hInst, mainhWnd)){   return 0;}


These are the functions for my keyboard.
bool InitKeyboard(HINSTANCE h, HWND whand){	// create the directinput object	HRESULT hr = DirectInput8Create( h, DIRECTINPUT_VERSION, IID_IDirectInput8,										(void**)&directInputObject, NULL);	// check the return code for directinput8create	if FAILED( hr )	{		return false;	}	// retrieve a pointer to an idirectinputdevice8 interface	hr = directInputObject->CreateDevice( GUID_SysKeyboard, &directInputDevice, NULL );	// check the return code from createdevice	if FAILED(hr)	{		return false;	}	// set the data format for the device by calling the setdataformat function	hr = directInputDevice->SetDataFormat(&c_dfDIKeyboard);	// check the setdataformat return code	if FAILED(hr)	{		return false;	}	// set the cooperative level 	hr = directInputDevice->SetCooperativeLevel( whand, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );	// check the return code for setcooperativelevel	if FAILED(hr)	{		return false;	}	// get access to the input device	hr = directInputDevice->Acquire();	if FAILED(hr)	{		return false;	}	return true;}void ShutdownDirectInput(){	if (directInputObject)	{		if (directInputDevice)		{			// always unacquire device before calling release()			directInputDevice->Unacquire();			directInputDevice->Release();			directInputDevice = NULL;		}		directInputObject->Release();		directInputObject = NULL;	}}


And here is where the inputdevice is actually being used.
HRESULT hr = directInputDevice->GetDeviceState(sizeof(buffer), (LPVOID)&buffer);if (FAILED(hr)){  return false;}


Also here is my message process function.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){	PAINTSTRUCT ps;    HDC hdc;	switch (message)	{		// allow the user to press the escape key to end the application		case WM_KEYDOWN:			switch(wParam)			{				// check if the user hit the escape key				case VK_ESCAPE:					PostQuitMessage(0);				break;			}		break;				// hopefully..., thought it might help redraw things after minimization		case WM_PAINT:            hdc = BeginPaint( hWnd, &ps );            EndPaint( hWnd, &ps );            break;		// the user hit the close button, close the application		case WM_DESTROY:			PostQuitMessage(0);			break;	}	return DefWindowProc(hWnd, message, wParam, lParam);}


So when I click on another application the game shuts down completely. And this began ever since I built the code with keyboard initialized for use.
Cal State Fullerton,Computer Science Student-----Just want to learn.
By any chance are you calling InitKeyboard in your main game loop (every frame)? If another app has focus you won't be able to get foreground access (you're specifying DISCL_FOREGROUND), so InitKeyboard will return false and your app will exit.

edit: By the way all this is unnecessary, Microsoft themselves say in the documentation "The use of DirectInput for keyboard and mouse input is not recommended, Windows messages should be used instead". http://msdn.microsoft.com/en-gb/library/ee416842%28VS.85%29.aspx Generally nowadays DirectInput is only used for joystick/joypad input.
HRESULT hr = directInputDevice->GetDeviceState(sizeof(buffer), (LPVOID)&buffer);
if (FAILED(hr))
{
return false;
}


How is that called and where?
Oh yeah well spotted Sven, another potential (and more likely) suspect for something that goes wrong when focus is lost. Clowkun, you can just stick a breakpoint on each of your "return false"s and see which one it is that fails and causes your app to exit when you alt+tab.
It's good that you're checking errors at all of the points, but bad that:
1. You're not cleaning up if the function fails halfway through (Although that could just be your coding style)
2. You're using DirectInput for keyboard input in the first place
3. When the function fails, you don't log any sort of error, so you don't know what part of the function is failing.
Quote:Original post by Hinch
By any chance are you calling InitKeyboard in your main game loop (every frame)? If another app has focus you won't be able to get foreground access (you're specifying DISCL_FOREGROUND), so InitKeyboard will return false and your app will exit.

edit: By the way all this is unnecessary, Microsoft themselves say in the documentation "The use of DirectInput for keyboard and mouse input is not recommended, Windows messages should be used instead". http://msdn.microsoft.com/en-gb/library/ee416842%28VS.85%29.aspx Generally nowadays DirectInput is only used for joystick/joypad input.


I was not aware of that so I will have to go and check that out.

@Svenorvar, this is where it is being called
bool UpdateGame(){	// update user input		HRESULT hr = directInputDevice->GetDeviceState(sizeof(buffer),                 (LPVOID)&buffer);	if (FAILED(hr))	{		return false;	}...


The function UpdateGame() is being called every time in the game loop.

@Evil Steve, I thought I was cleaning up with the returning of false if hr fails.
Cal State Fullerton,Computer Science Student-----Just want to learn.
Do you perhaps shutdown your game if UpdateGame() return false?

Since
HRESULT hr = directInputDevice->GetDeviceState(sizeof(buffer), (LPVOID)&buffer);

Might fail if your app lost focus depending on your settings here:
hr = directInputDevice->SetCooperativeLevel( whand, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );

I don't recall which settings should be used if you want to catch stuff when your app don't have focus but I personally had a problem with that one regardless of the CooperativeLevel.

This topic is closed to new replies.

Advertisement