Jump to content
  • Advertisement
Sign in to follow this  
ghiotto86

[DIRECTX C++]- problems with directinput

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

Share this post


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

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!