Original Post
Hi guys,
I got a question for ya.
For example, I want to implement an interface for input devices.
So, I go:
class IDeviceState
{
public:
IDeviceState() {};
~IDeviceState() {};
private:
long m_dwFlags;
};
class IInputDevice
{
public:
// Basically a state for a keyboard is the info about pressed keys
// and for mouse it''s the position/button
IDeviceState *GetState()
{ return m_pDeviceState; };
virtual void ResetState() = 0;
IInputDevice()
{
m_pDeviceState = NULL;
m_eDeviceType = INPUTDEV_NONE;
};
virtual ~IInputDevice() {};
public:
eInputDeviceType m_eDeviceType;
IDeviceState *m_pDeviceState;
};
OK, this is a generic interface for all other devices that are going to inherit it, ok?
So, I want to add a mouse input device:
#include "InputDevice.h"
struct sMouseDeviceState : public IDeviceState
{
bool *m_pButtonDown;
int m_iNumButtons;
sMouseDeviceState()
{
m_pButtonDown = NULL;
m_iNumButtons = 0;
};
~sMouseDeviceState()
{
if(m_pButtonDown)
delete m_pButtonDown;
};
};
class CMouseInputDevice : public IInputDevice
{
public:
void ResetState();
CMouseInputDevice();
virtual ~CMouseInputDevice();
};
Also I want to have a keyboard input device:
#include "InputDevice.h"
struct sKeyboardDeviceState : public IDeviceState
{
char *m_cKeys;
int m_iNumKeys;
// Add NumLock, CapsLock or whatever here too
};
class CKeyboardInputDevice : public IInputDevice
{
public:
void ResetState();
CKeyboardInputDevice();
virtual ~CKeyboardInputDevice();
};
Ok, we got so far.
Now I want to implement an Input Engine that holds all input devices and acts on them:
#include "KeyboardInputDevice.h"
#include "MouseInputDevice.h"
class CInputEngine
{
public:
bool Create();
void Destroy();
IInputDevice *GetDevice(eInputDeviceType type);
CInputEngine();
virtual ~CInputEngine();
public:
vector< IInputDevice *> m_DeviceList;
public:
IInputDevice *QueryDevice(eInputDeviceType type);
};
So, the question is: is there any better way to do that kind of inheritance so it will still be pretty generic and easy to use?
This example works only with lots of type castings which is not really good.
Please help.
Thanks.
" Do we need us? "