简介:DirectInput和其他DirectX组成部分一样,是通过硬件抽象层(HAL)和硬件仿真层(HEL)来实现,处理的输入设备一般包括鼠标、键盘、游戏控制杆、操作杆等。而且现在的DirectInput还能够支持力反馈设备。
Step 1: Creating the DirectInput Object
create a single DirectInput object as overall manager.
// HINSTANCE g_hinst; // initialized earlier
HRESULT hr;
LPDIRECTINPUT8 g_lpDI;
hr = DirectInput8Create(g_hinst, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&g_lpDI, NULL);
if FAILED(hr)
{
// DirectInput not available; take appropriate action
}
=================================================
Step 2: Enumerating the Joysticks
//g_pDI is an initialized pointer to IDirectInput8
g_pDI->EnumDevices(DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback,
NULL, DIEDFL_ATTACHEDONLY)
=================================================
Step 4: Setting the Joystick Data Format
call the IDirectInputDevice8::SetDataFormat to manipulate that device
if (FAILED(hr = g_pJoystick->SetDataFormat(&c_dfDIJoystick2)))
return hr;
typedef struct { LONG lX; LONG lY; LONG lZ; LONG lRx; LONG lRy; LONG lRz; LONG rglSlider[2]; DWORD rgdwPOV[4]; BYTE rgbButtons[128]; LONG lVX; LONG lVY; LONG lVZ; LONG lVRx; LONG lVRy; LONG lVRz; LONG rglVSlider[2]; LONG lAX; LONG lAY; LONG lAZ; LONG lARx; LONG lARy; LONG lARz; LONG rglASlider[2]; LONG lFX; LONG lFY; LONG lFZ; LONG lFRx; LONG lFRy; LONG lFRz; LONG rglFSlider[2]; } DIJOYSTATE2, *LPDIJOYSTATE2;
=================================================
Step 5: Setting the Joystick Behavior
The joystick device has been created, and its data format has been set. The next step is to set its cooperative level.
if (FAILED(hr = g_pJoystick->SetCooperativeLevel(hDlg,
DISCL_EXCLUSIVE | DISCL_FOREGROUND)))
return hr;
The next step is to gather information about the capabilities of the joystick.
g_diDevCaps.dwSize = sizeof(DIDEVCAPS);//must be first initailized
if (FAILED(hr = g_pJoystick->GetCapabilities(&g_diDevCaps)))
return hr;
if (FAILED(hr = g_pJoystick->EnumObjects(EnumAxesCallback,
(VOID*)hDlg, DIDFT_AXIS)))
return hr;
=================================================
Step6: Gaining Access to the Joystick
hr = g_pJoystick->Poll();
if (FAILED(hr))
{
hr = g_pJoystick->Acquire();
while(hr == DIERR_INPUTLOST)
hr = g_pJoystick->Acquire();
return S_OK;
}
=================================================
=================================================
Step 7: Retrieving Data from the Joystick
HRESULT UpdateInputState(HWND hDlg)
{
HRESULT hr;
CHAR strText[128]; // Device state text
DIJOYSTATE2 js; // Direct Input joystick state
CHAR* str;
if (NULL == g_pJoystick)
return S_OK;
// Poll the device to read the current state
hr = g_pJoystick->Poll();
if (FAILED(hr))
{
// Attempt to reacquire joystick
}
hr = g_pJoystick->GetDeviceState(sizeof(DIJOYSTATE2), &js);
if (FAILED(hr))
return hr;
Step 3: Creating the DirectInput Joystick Device
IDirectInput8::EnumDevices枚举到设备后调用EnumJoysticksCallback回调函数,在回调函数中调用IDirectInput8::CreateDevice建立设备。
IDirectInput8::CreateDevice有三个参数:
1.枚举设备GUID(由DIDEVICEINSTANCE结构体提供,枚举到设备后DIDEVICEINSTANCE结构体通过指针传递给回调函数)
2.
The EnumJoysticksCallback callback function parameters are as follows:
(NULL = VOID* pContext)
BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE*
pdidInstance, VOID* pContext)
{
HRESULT hr;
// Obtain an interface to the enumerated joystick.
hr = g_pDI->CreateDevice(pdidInstance->guidInstance,
&g_pJoystick, NULL);
if(FAILED(hr))
return DIENUM_CONTINUE;
return DIENUM_STOP;
=================================================