I'm currently working on mouse support for my tile engine and I've run into problems. The DirectInput object is one interface - created one time. The Mouse interface is as well but I'm deriving it from DirectInput. Problem is that when I create the mouse, keyboard, etc - I do not want to create another instance of DirectInput.
If I declare DXInput as static, will this work?
It seems that MSVC does not like that, but I thought this was possible with classes?
Here is my setup:
DXInput.h
Code:
#ifndef _DXINPUT_
#define _DXINPUT_
#define DIRECTINPUT_VERSION 0x0700
#include "dinput.h"
class DXInput
{
protected:
LPDIRECTINPUT lpdi;
public:
DXInput(HINSTANCE instance);
~DXInput(void) {if (lpdi) lpdi->Release();};
};
#endif
DXInput.cpp
Code:
#include "DXInput.h"
DXInput::DXInput(HINSTANCE instance)
{
if (FAILED(DirectInputCreate(instance,DIRECTINPUT_VERSION,&lpdi,NULL)))
{
//error
}
}
DXMouse.h
Code:
#ifndef _DXMOUSE_
#define _DXMOUSE_
#include "DXInput.h"
#define RMB 0
#define LMB 1
#define MMB 2
class DXMouse:public DXInput
{
LPDIRECTINPUTDEVICE lpdiMouse;
DIMOUSESTATE MouseState;
public:
DXMouse(void) {};
~DXMouse(void)
{
if (lpdiMouse) lpdiMouse->Unacquire();
if (lpdiMouse) lpdiMouse->Release();
}
void Query(void) {if (FAILED(lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)MouseState))) };
int CheckButton(int ButtonToCheck);
void Show(void);
void Hide(void);
DIMOUSESTATE GetMouseState(void) {return MouseState;};
};
#endif
DXMouse.cpp
Code:
#include "DXInput.h"
#include "DXMouse.h"
DXMouse::DXMouse(void)
{
if (FAILED(lpdi->CreateDevice(GUID_SysMouse,&lpdiMouse,NULL))
{
//error
}
if (FAILED(lpdiMouse->SetCooperativeLevel(MainWindow,DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
{
//error
}
if (FAILED(lpdiMouse->SetDataFormat(&c_dfDIMOUSE)))
{
//error
}
if (FAILED(lpdiMouse->Acquire()))
{
//error
}
}
int DXMouse::CheckButton(int ButtonToCheck)
{
Query();
if (MouseState.rgbButtons[ButtonToCheck] & 0x80)
{
return TRUE;
} else return FALSE;
}
Any help would be appreciated. I really want the mouse to be accessible by everyone in the engine. Having to go through a bunch of inheritance crapola when the mouse is a pretty global object seems kinda dumb.
Right now the compiler is yelling at me about a default constructor for DXInput when I attempt to create the mouse - I understand why it's doing this, but I'm not sure about the best route to go in getting around it.
And hey, look, all that and no assembly at all.