I'm new to everything DX, so I just have some stupid questions... :D
LPDIRECTINPUT8 diObject = NULL; // The DirectInput object
LPDIRECTINPUTDEVICE8 diKeyboard = NULL; // The keyboard device
LPDIRECTINPUTDEVICE8 diMouse = NULL; // The mouse device
What are the LPDIRECTINPUT objects for? I use it to get to my keyboard object, and then it seems I never have to call it again, other than to release it when I'm done. Am I supposed to create my mouse 'under' this thing too, or do I make a new one for it?
Also, what does this do? "&0 x80"?
if( keys[DIK_A] &0 x80)
{
// The 'A' key has been pressed on the keyboard
// We don't know if it's keydown or keyup
}
This is what I put together after looking at some samples, and the docs. am I on the right path?
//input.cpp
//all input related functions belong in this file
#include "main.h"
#define DIRECTINPUT_VERSION 0x0800
#include "dinput.h"
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
extern bool bAcquired;
LPDIRECTINPUT8 diObject = NULL; // The DirectInput object
LPDIRECTINPUTDEVICE8 diKeyboard = NULL; // The keyboard device
LPDIRECTINPUTDEVICE8 diMouse = NULL; // The mouse device
// *** Init functions
void InputAcquire(void)
{
diKeyboard->Acquire();
//diMouse->Acquire();
bAcquired = true;
return;
}
bool InputInit(HWND hWnd)
{
DWORD dwKeyboardFlags = DISCL_FOREGROUND|DISCL_EXCLUSIVE;
DWORD dwMouseFlags;
HRESULT hResult = NULL;
// Create a DirectInput object
if (DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&diObject, NULL) != DI_OK)
{
MessageBox(NULL,"ERROR: Failed to initialize Direct Input.",
APP_TITLE,MB_OK|MB_ICONERROR);
return false;
}
//create an interface to the keyboard
if (diObject->CreateDevice(GUID_SysKeyboard, &diKeyboard, NULL)!= DI_OK)
{
MessageBox(NULL,"ERROR: Failed to acquire the keyboard.",
APP_TITLE,MB_OK|MB_ICONERROR);
return false;
}
//set the dataformat
if (diKeyboard->SetDataFormat(&c_dfDIKeyboard) != DI_OK)
{
MessageBox(NULL,"ERROR: Failed to set keyboard data format.",
APP_TITLE,MB_OK|MB_ICONERROR);
return false;
}
//set the cooperation level
if (diKeyboard->SetCooperativeLevel(hWnd,dwKeyboardFlags) != DI_OK)
{
MessageBox(NULL,"ERROR: Failed to set keyboard co-op level.",
APP_TITLE,MB_OK|MB_ICONERROR);
return false;
}
InputAcquire();
return true;
}
void InputShutdown(void)
{
if (diKeyboard) diKeyboard->Unacquire();
// Release any DirectInput objects.
SAFE_RELEASE(diKeyboard);
SAFE_RELEASE(diObject);
return;
}
// *** Standard Functions
//check the mouse and keyboard
void InputPoll(void)
{
HRESULT hRes;
BYTE Keys[256];
ZeroMemory(Keys,sizeof(Keys));
hRes = diKeyboard->GetDeviceState(sizeof(Keys),Keys);
if (hRes != DI_OK)
{
if (hr == DIERR_INPUTLOST) diKeyboard->Acquire();
return;
}
//nothing here yet
return;
}