本篇是使用DirectInput進(jìn)行交互(3)的續(xù)篇,當(dāng)時(shí)由于身邊沒有游戲操縱桿,所以拖到現(xiàn)在才寫。
使用游戲桿玩游戲
游戲桿是游戲控制的支柱,盡管游戲桿不是游戲惟一可以使用的輸入設(shè)備,但它卻是專門為玩游戲而設(shè)計(jì)的。將游戲桿向左推,游戲人物就會向左走,按下一個(gè)按鍵,游戲中的英雄就會揮舞他的劍,還有什么比這更容易的嗎?
游戲桿的形狀和大小千差萬別,商店貨架上的方向盤控制器就是一個(gè)游戲桿。如果去過街機(jī)游戲廳,就可能玩過允許(或要求)玩家站在很大的踏雪板或騎在小摩托車上來控制屏幕上的角色之類的游戲。別驚奇,這些踏雪板和摩托車甚至都可以看成是游戲桿!
游戲桿是一種軸控制器,按鍵很少。方向盤只有一個(gè)用于向左向右轉(zhuǎn)向的控制軸,它可能還有用于剎車和控制油門的控制軸。連基本的兩鍵游戲桿都有兩個(gè)控制軸:一個(gè)用于向上和向下,另外一個(gè)用于向左和向右。
下圖顯示了一些游戲桿,不論外形如何,方向性、旋轉(zhuǎn)型以及推動型輸入是游戲桿的共同特征。

控制軸只是一個(gè)電位器(變量寄存器),它控制傳送給電路的電壓。傳送的最小電壓表示一個(gè)軸的范圍(游戲桿能夠被移動的最遠(yuǎn)點(diǎn)),而最大電壓表示的是另外一個(gè)范圍,所有電壓就介于這兩個(gè)范圍之間。
電壓會流向系統(tǒng),多虧經(jīng)過windows(或directinput)處理,因此才能使用它。游戲桿按鍵的工作方式幾乎完全相同,就是根據(jù)電壓是否施加到按鍵上來發(fā)出按鍵是否被按下的信號。
讀入游戲桿數(shù)據(jù)的方式采用的是絕對值,這些絕對值都是相對于游戲桿中心的值。向左或向右推動游戲桿,都會接收到負(fù)值,負(fù)值表示遠(yuǎn)離游戲桿中心的距離。向下或向右按,就會得到正值。按鍵都是單個(gè)標(biāo)志,這些標(biāo)志指出了按鍵是否被按下。
各種游戲桿之間惟一較大的差別就是那些帶有數(shù)字控制軸的游戲桿了,這些游戲桿就像是一些按鍵的組合。將游戲桿向左推就像按下一個(gè)表示向左的按鍵一樣,無論程序員何時(shí)查詢游戲桿以得到正在讀取的軸,游戲桿都會返回軸的可能最低值或最高值。
使用DirectInput處理游戲桿
從某種程度上講,游戲桿是最難處理的設(shè)備。最難的地方在于游戲桿的設(shè)置,要找到連接到系統(tǒng)中的游戲桿設(shè)備,必須進(jìn)行枚舉。在枚舉的過程中,必須決定使用哪個(gè)游戲桿,然后再為游戲桿創(chuàng)建COM對象。
如下所示,該函數(shù)枚舉并返回第一個(gè)枚舉到的游戲桿。
IDirectInput8* g_di; // directinput component
IDirectInputDevice8* g_enum_joystick; // enum joystick device
IDirectInputDevice8* g_joystick; // joystick device
//--------------------------------------------------------------------------------
// Initialize joystick interface, return a joystick interface pointer.
//--------------------------------------------------------------------------------
IDirectInputDevice8* init_joystick(HWND hwnd, IDirectInput8* di)
{
g_di->EnumDevices(DI8DEVTYPE_JOYSTICK, enum_joysticks, NULL, DIEDFL_ATTACHEDONLY);
// everything was a success, return the pointer.
return g_enum_joystick;
}
接著來看看枚舉函數(shù)的實(shí)現(xiàn):
//--------------------------------------------------------------------------------
// Enumerate all attached joysticks, return first enumerated joystick.
//--------------------------------------------------------------------------------
BOOL CALLBACK enum_joysticks(LPCDIDEVICEINSTANCE device_inst, LPVOID ref)
{
DIPROPRANGE prop_range;
DIPROPDWORD prop_dword;
g_enum_joystick = NULL;
// create the device object using global directinput object
if(FAILED(g_di->CreateDevice(device_inst->guidInstance, &g_enum_joystick, NULL)))
return DIENUM_CONTINUE;
// set the data format
if(FAILED(g_enum_joystick->SetDataFormat(&c_dfDIJoystick)))
goto fail;
// set the cooperative mode
if(FAILED(g_enum_joystick->SetCooperativeLevel(g_hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
goto fail;
// clear out the structure first
ZeroMemory(&prop_range, sizeof(DIPROPRANGE));
prop_range.diph.dwSize = sizeof(DIPROPRANGE);
prop_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
prop_range.diph.dwHow = DIJOFS_X;
prop_range.diph.dwHow = DIPH_BYOFFSET; // offset into data format
prop_range.lMin = -1024;
prop_range.lMax = 1024;
// Sets properties that define the device behavior.
// These properties include input buffer size and axis mode.
HRESULT rv = g_enum_joystick->SetProperty(DIPROP_RANGE, &prop_range.diph);
if(FAILED(rv))
{
if(rv == DIERR_INVALIDPARAM)
MessageBox(NULL, "invalid param", NULL, MB_OK);
else if(rv == DIERR_NOTINITIALIZED)
MessageBox(NULL, "not initialize", NULL, MB_OK);
else if(rv == DIERR_OBJECTNOTFOUND)
MessageBox(NULL, "object not found", NULL, MB_OK);
else if(rv == DIERR_UNSUPPORTED)
MessageBox(NULL, "unsopported", NULL, MB_OK);
goto fail;
}
prop_range.diph.dwObj = DIJOFS_Y;
if(FAILED(g_enum_joystick->SetProperty(DIPROP_RANGE, &prop_range.diph)))
goto fail;
// set x deadzone to 15%
prop_dword.diph.dwSize = sizeof(DIPROPDWORD);
prop_dword.diph.dwHeaderSize = sizeof(DIPROPHEADER);
prop_dword.diph.dwHow = DIPH_BYOFFSET;
prop_dword.diph.dwObj = DIJOFS_X;
prop_dword.dwData = 1500;
if(FAILED(g_enum_joystick->SetProperty(DIPROP_DEADZONE, &prop_dword.diph)))
goto fail;
// set Y deadzone
prop_dword.diph.dwObj = DIJOFS_Y;
if(FAILED(g_enum_joystick->SetProperty(DIPROP_DEADZONE, &prop_dword.diph)))
goto fail;
// acquire the device for use
if(FAILED(g_enum_joystick->Acquire()))
goto fail;
// stop enumeration
return DIENUM_STOP;
fail:
g_enum_joystick->Release();
g_enum_joystick = NULL;
return DIENUM_CONTINUE;
}
其中涉及到的結(jié)構(gòu)體DIPROPRANGE定義如下:
Contains information about the range of an object within a device. This
structure is used with the DIPROP_RANGE flag set in the
IDirectInputDevice8::GetProperty and IDirectInputDevice8::SetProperty methods.
typedef struct DIPROPRANGE {
DIPROPHEADER diph;
LONG lMin;
LONG lMax;
} DIPROPRANGE, *LPDIPROPRANGE;
Members
- diph
- DIPROPHEADER structure.
- lMin
- Lower limit of the range. If the range of the device is unrestricted,
this value is DIPROPRANGE_NOMIN when the
IDirectInputDevice8::GetProperty method returns.
- lMax
- Upper limit of the range. If the range of the device is unrestricted,
this value is DIPROPRANGE_NOMAX when the
IDirectInputDevice8::GetProperty method returns.
Remarks
The diph member must be initialized as follows:
Member |
Value |
dwSize
|
sizeof(DIPROPRANGE) |
dwHeaderSize |
sizeof(DIPROPHEADER) |
dwObj
|
If the
dwHow member is DIPH_DEVICE, this member must be 0.
If
the dwHow member is DIPH_BYID, this member must be the identifier for
the object whose property setting is to be set or retrieved.
If
the dwHow member is DIPH_BYOFFSET, this member must be a data format
offset for the object whose property setting is to be set or retrieved.
For example, if the c_dfDIMouse data format is selected, it must be one
of the DIMOFS_* values.Identifier of the object whose property is being
retrieved or set.
If
the dwHow member is DIPH_BYUSAGE, the device must be a Human Interface
Device (human interface device). The device object will be identified by
the HID usage page and usage values in packed form.
|
dwHow
|
Specifies how the dwObj member should be interpreted. See the preceding
description of the dwObj member for details. |
The range values for devices whose ranges are unrestricted wraparound.
DIPROPDWORD結(jié)構(gòu)體的定義如下:
Used to access DWORD properties.
typedef struct DIPROPDWORD {
DIPROPHEADER diph;
DWORD dwData;
} DIPROPDWORD, *LPDIPROPDWORD;
Members
- diph
- DIPROPHEADER structure.
- dwData
- Property-specific value being set or retrieved.
-
DIPROPHEADER的定義如下:
Serves as a header for all property structures.
typedef struct DIPROPHEADER {
DWORD dwSize;
DWORD dwHeaderSize;
DWORD dwObj;
DWORD dwHow;
} DIPROPHEADER, *LPDIPROPHEADER;
Members
- dwSize
- Size of the enclosing structure. This member must be initialized before
the structure is used.
- dwHeaderSize
- Size of the DIPROPHEADER structure.
- dwObj
- Object for which the property is to be accessed. The value set for this
member depends on the value specified in the dwHow member.
- dwHow
- Value that specifies how the dwObj member should be interpreted. This
value can be one of the following:
- DIPH_DEVICE
- The dwObj member must be 0.
- DIPH_BYOFFSET
- The dwObj member is the offset into the current data format of the
object whose property is being accessed.
- DIPH_BYUSAGE
- The dwObj member is the human interface device usage page and usage
values in packed form.
- DIPH_BYID
- The dwObj member is the object type/instance identifier. This
identifier is returned in the dwType member of the
DIDEVICEOBJECTINSTANCE structure returned from a previous call to the
IDirectInputDevice8::EnumObjects member.
如果有游戲桿被初始化,g_enum_joystick就成了指向新對象的指針,反之如果沒有游戲桿被初始化,它就等于NULL。一旦設(shè)備對象被初始化,就能像前面讀取鍵盤和鼠標(biāo)的信息一樣讀取游戲桿的信息,但是read_joystick函數(shù)要使用DIJOYSTATE結(jié)構(gòu)體。
Describes the state of a joystick device. This structure is used with the
IDirectInputDevice8::GetDeviceState method.
typedef struct DIJOYSTATE {
LONG lX;
LONG lY;
LONG lZ;
LONG lRx;
LONG lRy;
LONG lRz;
LONG rglSlider[2];
DWORD rgdwPOV[4];
BYTE rgbButtons[32];
} DIJOYSTATE, *LPDIJOYSTATE;
Members
- lX
- X-axis, usually the left-right movement of a stick.
- lY
- Y-axis, usually the forward-backward movement of a stick.
- lZ
- Z-axis, often the throttle control. If the joystick does not have this
axis, the value is 0.
- lRx
- X-axis rotation. If the joystick does not have this axis, the value is
0.
- lRy
- Y-axis rotation. If the joystick does not have this axis, the value is
0.
- lRz
- Z-axis rotation (often called the rudder). If the joystick does not have
this axis, the value is 0.
- rglSlider
- Two additional axes, formerly called the u-axis and v-axis, whose
semantics depend on the joystick. Use the IDirectInputDevice8::GetObjectInfo
method to obtain semantic information about these values.
- rgdwPOV
- Direction controllers, such as point-of-view hats. The position is
indicated in hundredths of a degree clockwise from north (away from the
user). The center position is normally reported as - 1; but see Remarks. For
indicators that have only five positions, the value for a controller is - 1,
0, 9,000, 18,000, or 27,000.
- rgbButtons
- Array of buttons. The high-order bit of the byte is set if the
corresponding button is down, and clear if the button is up or does not
exist.
Remarks
You must prepare the device for joystick-style access by calling the
IDirectInputDevice8::SetDataFormat method, passing the c_dfDIJoystick global
data format variable.
If an axis is in relative mode, the appropriate member contains the change in
position. If it is in absolute mode, the member contains the absolute axis
position.
Some drivers report the centered position of the POV indicator as 65,535.
Determine whether the indicator is centered as follows:
BOOL POVCentered = (LOWORD(dwPOV) == 0xFFFF);
Note Under DirectX 7.0, sliders on
some joysticks could be assigned to the Z axis, with subsequent code retrieving
data from that member. Using DirectX 8.0 and later, those same sliders will be
assigned to the rglSlider array. This should be taken into account when porting
applications to later versions of DirectX. Make any necessary alterations to
ensure that slider data is retrieved from the rglSlider array.
來看看read_joystick的實(shí)現(xiàn):
//--------------------------------------------------------------------------------
// Read joystick buffer.
//--------------------------------------------------------------------------------
BOOL read_joystick(void* buffer, long buffer_size)
{
HRESULT rv;
while(1)
{
// poll device
g_joystick->Poll();
// read in state
if(SUCCEEDED(rv = g_joystick->GetDeviceState(buffer_size, buffer)))
break;
// return when an unknown error
if(rv != DIERR_INPUTLOST || rv != DIERR_NOTACQUIRED)
return FALSE;
// re-acquire and try again
if(FAILED(g_joystick->Acquire()))
return FALSE;
}
return TRUE;
}
用于讀取按鍵狀態(tài)的宏在這里仍然起作用:
#define JOYSTICK_BUTTON_STATE(x) ((joy_state.rgbButtons[x] &0x80) ? TRUE :
FALSE)
完整代碼示例如下:
點(diǎn)擊下載源碼和工程
/***************************************************************************************
PURPOSE:
Joystick device Demo
***************************************************************************************/
#define DIRECTINPUT_VERSION 0x0800
#include <windows.h>
#include <stdio.h>
#include <dinput.h>
#include "resource.h"
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dinput8.lib")
#pragma warning(disable : 4996)
#define Safe_Release(p) if((p)) (p)->Release();
// window handles, class and caption text.
HWND g_hwnd;
char g_class_name[] = "JoystickClass";
IDirectInput8* g_di; // directinput component
IDirectInputDevice8* g_enum_joystick; // enum joystick device
IDirectInputDevice8* g_joystick; // joystick device
BOOL CALLBACK enum_joysticks(LPCDIDEVICEINSTANCE device_inst, LPVOID ref);
//--------------------------------------------------------------------------------
// Window procedure.
//--------------------------------------------------------------------------------
long WINAPI window_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return (long) DefWindowProc(hwnd, msg, wParam, lParam);
}
//--------------------------------------------------------------------------------
// Initialize joystick interface, return a joystick interface pointer.
//--------------------------------------------------------------------------------
IDirectInputDevice8* init_joystick(HWND hwnd, IDirectInput8* di)
{
g_di->EnumDevices(DI8DEVTYPE_JOYSTICK, enum_joysticks, NULL, DIEDFL_ATTACHEDONLY);
// everything was a success, return the pointer.
return g_enum_joystick;
}
//--------------------------------------------------------------------------------
// Read joystick buffer.
//--------------------------------------------------------------------------------
BOOL read_joystick(void* buffer, long buffer_size)
{
HRESULT rv;
while(1)
{
// poll device
g_joystick->Poll();
// read in state
if(SUCCEEDED(rv = g_joystick->GetDeviceState(buffer_size, buffer)))
break;
// return when an unknown error
if(rv != DIERR_INPUTLOST || rv != DIERR_NOTACQUIRED)
return FALSE;
// re-acquire and try again
if(FAILED(g_joystick->Acquire()))
return FALSE;
}
return TRUE;
}
//--------------------------------------------------------------------------------
// Enumerate all attached joysticks, return first enumerated joystick.
//--------------------------------------------------------------------------------
BOOL CALLBACK enum_joysticks(LPCDIDEVICEINSTANCE device_inst, LPVOID ref)
{
DIPROPRANGE prop_range;
DIPROPDWORD prop_dword;
g_enum_joystick = NULL;
// create the device object using global directinput object
if(FAILED(g_di->CreateDevice(device_inst->guidInstance, &g_enum_joystick, NULL)))
return DIENUM_CONTINUE;
// set the data format
if(FAILED(g_enum_joystick->SetDataFormat(&c_dfDIJoystick)))
goto fail;
// set the cooperative mode
if(FAILED(g_enum_joystick->SetCooperativeLevel(g_hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
goto fail;
// clear out the structure first
ZeroMemory(&prop_range, sizeof(DIPROPRANGE));
prop_range.diph.dwSize = sizeof(DIPROPRANGE);
prop_range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
prop_range.diph.dwHow = DIJOFS_X;
prop_range.diph.dwHow = DIPH_BYOFFSET; // offset into data format
prop_range.lMin = -1024;
prop_range.lMax = 1024;
// Sets properties that define the device behavior.
// These properties include input buffer size and axis mode.
HRESULT rv = g_enum_joystick->SetProperty(DIPROP_RANGE, &prop_range.diph);
if(FAILED(rv))
{
if(rv == DIERR_INVALIDPARAM)
MessageBox(NULL, "invalid param", NULL, MB_OK);
else if(rv == DIERR_NOTINITIALIZED)
MessageBox(NULL, "not initialize", NULL, MB_OK);
else if(rv == DIERR_OBJECTNOTFOUND)
MessageBox(NULL, "object not found", NULL, MB_OK);
else if(rv == DIERR_UNSUPPORTED)
MessageBox(NULL, "unsopported", NULL, MB_OK);
goto fail;
}
prop_range.diph.dwObj = DIJOFS_Y;
if(FAILED(g_enum_joystick->SetProperty(DIPROP_RANGE, &prop_range.diph)))
goto fail;
// set x deadzone to 15%
prop_dword.diph.dwSize = sizeof(DIPROPDWORD);
prop_dword.diph.dwHeaderSize = sizeof(DIPROPHEADER);
prop_dword.diph.dwHow = DIPH_BYOFFSET;
prop_dword.diph.dwObj = DIJOFS_X;
prop_dword.dwData = 1500;
if(FAILED(g_enum_joystick->SetProperty(DIPROP_DEADZONE, &prop_dword.diph)))
goto fail;
// set Y deadzone
prop_dword.diph.dwObj = DIJOFS_Y;
if(FAILED(g_enum_joystick->SetProperty(DIPROP_DEADZONE, &prop_dword.diph)))
goto fail;
// acquire the device for use
if(FAILED(g_enum_joystick->Acquire()))
goto fail;
// stop enumeration
return DIENUM_STOP;
fail:
g_enum_joystick->Release();
g_enum_joystick = NULL;
return DIENUM_CONTINUE;
}
//--------------------------------------------------------------------------------
// Main function, routine entry.
//--------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
WNDCLASS win_class;
MSG msg;
DIJOYSTATE joy_state;
char text[256];
LONG joystick_x = 0, joystick_y = 0;
BOOL is_first_render = TRUE;
// create window class and register it
win_class.style = CS_HREDRAW | CS_VREDRAW;
win_class.lpfnWndProc = window_proc;
win_class.cbClsExtra = 0;
win_class.cbWndExtra = DLGWINDOWEXTRA;
win_class.hInstance = inst;
win_class.hIcon = LoadIcon(inst, IDI_APPLICATION);
win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
win_class.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
win_class.lpszMenuName = NULL;
win_class.lpszClassName = g_class_name;
if(! RegisterClass(&win_class))
return FALSE;
// create the main window
g_hwnd = CreateDialog(inst, MAKEINTRESOURCE(IDD_Joystick), 0, NULL);
ShowWindow(g_hwnd, cmd_show);
UpdateWindow(g_hwnd);
// initialize directinput and get keyboard device
DirectInput8Create(inst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **) &g_di, NULL);
// initialize mouse
g_joystick = init_joystick(g_hwnd, g_di);
if(g_joystick != NULL)
{
// start message pump, waiting for signal to quit.
ZeroMemory(&msg, sizeof(MSG));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// read in mouse and display coordinates
read_joystick(&joy_state, sizeof(DIJOYSTATE));
if(is_first_render || (joystick_x != joy_state.lX || joystick_y != joy_state.lY))
{
is_first_render = FALSE;
sprintf(text, "%ld, %ld", joy_state.lX, joy_state.lY);
SetWindowText(GetDlgItem(g_hwnd, IDC_COORDINATES), text);
}
joystick_x = joy_state.lX;
joystick_y = joy_state.lY;
}
}
else
{
MessageBox(g_hwnd, "No Joysticks!", "Error", MB_OK);
return 0;
}
// release directinput objects
g_joystick->Unacquire();
g_joystick->Release();
g_di->Release();
UnregisterClass(g_class_name, inst);
return (int) msg.wParam;
}
程序截圖:
