Sign in to follow this  
clowkun

why is the game shutting down?

Recommended Posts

clowkun    122
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?

Share this post


Link to post
Share on other sites
clowkun    122
Well here is some code.

This is from my main.cpp where the keyboard is initialized.

// init the keyboard
if (!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.

Share this post


Link to post
Share on other sites
Hinch    244
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.

Share this post


Link to post
Share on other sites
Hinch    244
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.

Share this post


Link to post
Share on other sites
Evil Steve    2017
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.

Share this post


Link to post
Share on other sites
clowkun    122
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.

Share this post


Link to post
Share on other sites
Svenorvar    136
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.

Share this post


Link to post
Share on other sites

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