I am trying to use DirectInput but I can't see why my code crashes. I checked directx sdk and some online sources for reference. Turns out I do the same thing as tutorials but it still doesn't work.
LPDIRECTINPUTDEVICE8 DIKeyboard;
LPDIRECTINPUTDEVICE8 DIMouse;
DIMOUSESTATE mouseLastState;
LPDIRECTINPUT8 DirectInput;
HRESULT hr;
HINSTANCE hInst;
HWND hwnd;
bool InitInput()
{
hr = DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8,
(void**)&DirectInput,
NULL);
DirectInput->CreateDevice(GUID_SysKeyboard, &DIKeyboard, NULL);
DirectInput->CreateDevice(GUID_SysMouse, &DIMouse, NULL);
DIKeyboard->SetDataFormat(&c_dfDIKeyboard);
DIKeyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
DIMouse->SetDataFormat(&c_dfDIMouse);
DIMouse->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND);
return true;
}
void CheckInput()
{
DIMOUSESTATE mouseCurrState;
BYTE keyboardState[256];
DIKeyboard->Acquire();
DIMouse->Acquire();
DIKeyboard->GetDeviceState(sizeof(keyboardState),(LPVOID)&keyboardState);
DIMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseCurrState);
//if(keyboardState[DIK_ESCAPE] & 0x80)
// PostMessage(hwnd, WM_DESTROY, 0, 0);
//float speed = 10.0f * time;
if(keyboardState[DIK_UP] & 0x80)
RotationAngle += 1.0f;
if(keyboardState[DIK_DOWN] & 0x80)
RotationAngle -= 1.0f;
if((mouseCurrState.lX != mouseLastState.lX) || (mouseCurrState.lY != mouseLastState.lY))
{
//camYaw += mouseLastState.lX * 0.001f;
//camPitch += mouseCurrState.lY * 0.001f;
mouseLastState = mouseCurrState;
}
}
void ReleaseInput()
{
DIKeyboard->Unacquire();
DIMouse->Unacquire();
DirectInput->Release();
}
I use InitInput() after I create a window with SDL. I don't want to use SDL for input.
When I debug the program I am getting the crash at
DirectInput->CreateDevice(GUID_SysKeyboard, &DIKeyboard, NULL);
with E_INVALIDARG.
Why Is this this doesn't appear to be the problem in other appliactions that are using DirectInput?