Jump to content
  • Advertisement
Sign in to follow this  
ghiotto86

[C++]-problems with directinput

This topic is 4875 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 boys. i am a new user into this forum and i start with the programmation with directx in c++. i have a problem. i watch the msdn tutorial on directinput and i create these function The function that create the device:
HRESULT CreateKeyboard( HWND hWnd )
{   
	HRESULT hr;
	// Create a DInput object
    if (FAILED(hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, 
		IID_IDirectInput8, (VOID**)&g_lpDI, NULL ))){
		MessageBox (hWnd,"Non è possibile inizializzare DirectInput","ERROR",MB_ICONEXCLAMATION | MB_OK);
		exit(-1);}
    
	if(FAILED(hr = g_lpDI->CreateDevice(GUID_SysKeyboard , &g_pKeyboard, NULL)))
		return hr;
      
    if(FAILED(hr = g_pKeyboard->SetDataFormat( &c_dfDIKeyboard )))
		return hr;
      
    hr = g_pKeyboard->SetCooperativeLevel( hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );
	
	if( FAILED(hr) )
        return hr;
	
	g_pKeyboard->Acquire();

	return S_OK;
}
the function that release the device
VOID FreeDirectInput()
{
    // Unacquire the device one last time just in case 
    // the app tried to exit while the device is still acquired.
    if( g_pKeyboard ) 
        g_pKeyboard->Unacquire();
    
    // Release any DirectInput objects.
    SAFE_RELEASE( g_pKeyboard );
    SAFE_RELEASE( g_lpDI );
}
and the function that process the keypress
HRESULT ProcessKBInput(HWND hWnd) 
{ 
	#define KEYDOWN(name, key) (name[key] & 0x80) 

    BYTE     buffer[256]; 
	HRESULT  hr; 

	if( NULL == g_pKeyboard ) 
        return S_OK;

	ZeroMemory( buffer, sizeof(buffer) );
	g_pKeyboard->Acquire();
    hr = g_pKeyboard->GetDeviceState(sizeof(buffer),(LPVOID)buffer);

	if( FAILED(hr) ) 
    {
        // DirectInput may be telling us that the input stream has been
        // interrupted.  We aren't tracking any state between polls, so
        // we don't have any special reset that needs to be done.
        // We just re-acquire and try again.
        
        // If input is lost then acquire and keep trying 
        hr = g_pKeyboard->Acquire();
        while( hr == DIERR_INPUTLOST ) 
            hr = g_pKeyboard->Acquire();

        return S_OK; 
    } 

 	// Turn the spaceship right or left 
    if (KEYDOWN(buffer, DIK_RIGHT))
		exit(-1); 
		// Turn right. 

	return S_OK;
	
} 
and here there are a lot of problems. when i debug at the line of code
hr = g_pKeyboard->GetDeviceState(sizeof(buffer),(LPVOID)buffer);
there is an error: hr 0x8007000c Code of access not valid. HRESULT what means??? help me i driving crazy.

Share this post


Link to post
Share on other sites
Advertisement
Probably the device has not been acquired. You need to do something like this:


HRESULT hr=m_diKeyboardDevice->GetDeviceState( sizeof(keys), keys );

if (FAILED(hr))
{
// If input is lost then acquire and keep trying until we get it back
hr=m_diKeyboardDevice->Acquire();
while( hr == DIERR_INPUTLOST )
{
hr = m_diKeyboardDevice->Acquire();
}
// Now get them again
m_diKeyboardDevice->GetDeviceState( sizeof(keys), keys );
}




Note also that as soon as you enter the debugger the device will be lost - makes it tricky to debug :)

Share this post


Link to post
Share on other sites
hi trip.
thanks for the answer.

it works :D

but i have another problem.

I have made as you have said and it works when i press the escape key.
but when i click on the X for close the window appear a messagebox of error
watch here

http://img237.echo.cx/img237/9274/immagine0nb.jpg

what means???

thanks still

Share this post


Link to post
Share on other sites
Hi,

you trying to get the device state of your keyboard, which may have been released. I think you release your keyboard device when the message WM_DESTROY appears. But your mainloop makes a last call to ProcessKBInput().
The best way to quit your program is to have loop that looks like this:

while(!done){
...
}

Then, in WM_DESTROY all you have to do is:

- Release all your devices (call a function which handles it, so you don't have to put it all into the WM_DESTROY selection)

- and after releasing just do this: done=true;


Try it, if it doesn't work, set a breakpoint, and use your debugger!


greetz

Share this post


Link to post
Share on other sites
Quote:
Original post by codex01
Hi,

you trying to get the device state of your keyboard, which may have been released. I think you release your keyboard device when the message WM_DESTROY appears. But your mainloop makes a last call to ProcessKBInput().
The best way to quit your program is to have loop that looks like this:

while(!done){
...
}

Then, in WM_DESTROY all you have to do is:

- Release all your devices (call a function which handles it, so you don't have to put it all into the WM_DESTROY selection)

- and after releasing just do this: done=true;


Try it, if it doesn't work, set a breakpoint, and use your debugger!


greetz


hi i resolved.
it was a parenthesis belt fastener :D
bye

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!