-
My DirectInput solution
CDInput.h
Code:
#ifndef _CDINPUT_
#define _CDINPUT_
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
class CDInput
{
protected:
IDirectInput8 *_pDI8;
public:
CDInput() {};
~CDInput() {if (_pDI8) _pDI8->Release();};
HRESULT Init(HINSTANCE hinst);
};
#endif
CDInput.cpp
Code:
#include "CDinput.h"
HRESULT CDInput::Init(HINSTANCE hinst)
{
if (FAILED(DirectInput8Create(hinst,
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**)&_pDI8,
NULL)))
{
::MessageBox(0,"Failed to create DirectInput8 interface",0,0);
}
return true;
}
CKeyboard.h
Code:
#include "CDInput.h"
//Inherits from virtual base class CDInput
//Helpers
#define DIKEYDOWN(data,n) (data[n] & 0x80)
class CKeyboard:virtual public CDInput
{
protected:
IDirectInputDevice8 *_pDID8;
public:
//So anyone can access our data - otherwise function overhead per use
//could inline....but
UCHAR keystate[256];
CKeyboard() {};
~CKeyboard() {if (_pDID8) _pDID8->Unacquire();
if (_pDID8) _pDID8->Release();};
HRESULT Create(HWND hwnd);
HRESULT Update(void);
};
CKeyboard.cpp
Code:
#include "CKeyboard.h"
HRESULT CKeyboard::Create(HWND window)
{
//Create device
if (FAILED(_pDI8->CreateDevice(GUID_SysKeyboard,
&_pDID8,
NULL)))
{
::MessageBox(0,"Could not create keyboard device",0,0);
}
//Set cooperative level
if (FAILED(_pDID8->SetCooperativeLevel(window,
DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
{
::MessageBox(0,"Could not set keyboard cooperative level",0,0);
}
//Set data format
if (FAILED(_pDID8->SetDataFormat(&c_dfDIKeyboard)))
{
::MessageBox(0,"Could not set keyboard data format",0,0);
}
//Acquire the keyboard
if (FAILED(_pDID8->Acquire()))
{
::MessageBox(0,"Could not acquire keyboard device",0,0);
}
return true;
}
HRESULT CKeyboard::Update(void)
{
HRESULT hr;
hr=(_pDID8->GetDeviceState(sizeof(keystate),(LPVOID)&keystate));
if (hr==DIERR_INPUTLOST)
{
::MessageBox(0,"Input lost",0,0);
}
return true;
}
CMouse.h
Code:
#include "CDInput.h"
#define LMB 0
#define RMB 1
#define MMB 2
class CMouse:virtual public CDInput
{
protected:
IDirectInputDevice8 *_pDID8;
public:
CMouse() {};
~CMouse() {if (_pDID8) _pDID8->Unacquire();
if (_pDID8) _pDID8->Release();};
HRESULT Create(HWND window);
//Share our data with the world
DIMOUSESTATE _mousestate;
HRESULT Update(void);
};
CMouse.cpp
Code:
#include "CMouse.h"
HRESULT CMouse::Create(HWND window)
{
if (FAILED(_pDI8->CreateDevice(GUID_SysMouse,&_pDID8,NULL)))
{
::MessageBox(0,"Failed to create mouse device",0,0);
}
if (FAILED(_pDID8->SetCooperativeLevel(window,
DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
{
::MessageBox(0,"Failed to set mouse cooperative level",0,0);
}
if (FAILED(_pDID8->SetDataFormat(&c_dfDIMouse)))
{
::MessageBox(0,"Failed to set mouse data format",0,0);
}
if (FAILED(_pDID8->Acquire()))
{
::MessageBox(0,"Failed to acquire mouse",0,0);
}
::ShowCursor(false);
return true;
}
HRESULT CMouse::Update(void)
{
if (FAILED(_pDID8->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&_mousestate)))
{
return FALSE;
} else return true;
}
I'm not sure if this is creating 2 Direct Input interface pointers or not. Since CDInput is a virtual base...isn't there only 1 copy of it??
-
_pDI8 is uninitialized until you call Init(). Creating and destroying an object without calling Init() will result in undefined behaviour.
-
I know that.
But if you call init for each input device...does that create another instance of CDInput and thus create another _pDI8 interface?
-
It shouldn't matter unless you are using multiple inheritance. I wouldn't do it this way, however.I think CDInput should be a singleton or you could just use a global IDirectInput device.