|
键盘鼠标对象,出现的问题。初始化没有问题
/**
** DirectInput initialization
**/
bool DirectInput_Init(HWND hwnd)
{
//initialize DirectInput object
HRESULT result = DirectInput8Create(
GetModuleHandle(NULL),
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&dinput,
NULL);
//initialize the keyboard
dinput->CreateDevice(GUID_SysKeyboard, &dikeyboard, NULL);
dikeyboard->SetDataFormat(&c_dfDIKeyboard);
dikeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
dikeyboard->Acquire();
//initialize the mouse
dinput->CreateDevice(GUID_SysMouse, &dimouse, NULL);
dimouse->SetDataFormat(&c_dfDIMouse);
dimouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
dimouse->Acquire();
d3ddev->ShowCursor(false);
return true;
}
/**
** DirectInput update
**/
void DirectInput_Update()
{
//update mouse
if(FAILED( dimouse->GetDeviceState(sizeof(mouse_state), (LPVOID)&mouse_state))) //这个地方一直失败!上有个碰到同样问题的
//解决办法:就是Acquire() ..但是我仍然失败。
{
if(FAILED(dimouse ->Acquire())) return;
if(FAILED(dimouse ->GetDeviceState(sizeof(mouse_state), (LPVOID)&mouse_state)))return;
}
//update keyboard
dikeyboard->GetDeviceState(sizeof(keys), (LPVOID)&keys);
for (int i=0; i< 4; i++ )
{
ZeroMemory( &controllers, sizeof(XINPUT_STATE) );
//get the state of the controller
XINPUT_STATE state;
DWORD result = XInputGetState( i, &state );
//store state in global controllers array
if (result == 0) controllers = state.Gamepad;
}
}
原因可能大致是这样,Keyboard独占会创建失败,就算独占也很有可能会使设备丢失,需要重新调用Acquire()函数,当然也有可能失败,这个由操作系统说了算。所以在这个循环里面不要出现诸如MessageBox之类的使线程停止的操作。
相当悲剧。
abcabcabcabc |
|