简介: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;
}
=================================================
- The DI8DEVCLASS_GAMECTRL constant specifies the class of device to be enumerated.
- EnumJoysticksCallback is the address of a callback function to be called each time a joystick is found.
- The third parameter can be any 32-bit value that you want to make available to the callback function.
- The last parameter, DIEDFL_ATTACHEDONLY, is a flag that restricts enumeration to devices that are attached to the computer.
=================================================
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;
- DIJOYSTATE2. This structure holds data for up to six axes, 128 buttons, and four point-of-view hats.
- Joystick buttons work just like keys or mouse buttons. If the high bit of the returned byte is 1, the button is pressed.
- 返回字节最高位为1,则按键处于按下状态
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:
- A pointer to the device instance, supplied by the DirectInput system when the device is enumerated.
- A pointer to a 32-bit value that you supplied as a parameter to IDirectInput8::EnumDevices
(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;
=================================================
DirectInput Joystick 创建指南
本文详细介绍了如何使用DirectInput创建并管理游戏控制杆(Joystick)。首先创建DirectInput对象,然后枚举Joysticks,设置Joystick数据格式,定义设备行为,获取设备访问权限,最后从Joystick检索数据。在枚举过程中,使用了回调函数EnumJoysticksCallback来创建设备,并通过SetDataFormat和SetCooperativeLevel设定设备属性。
638

被折叠的 条评论
为什么被折叠?



