Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Programmer16

DirectInput wrapper question/opinion request

This topic is 5304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m making a DirectInput wrapper and I was wondering if it would be smarter to just wrap the DirectInput pointer, and the device for the keyboard and mouse, etc. all together into 1 class or into 3. Thanks in advance! /* I use DirectX 8.1 and C++ (Microsoft Visual C++ 6.0 Professional edition) */

Share this post


Link to post
Share on other sites
Advertisement
I made one class for the device and the keyboard together


class CDirectInput
{

protected:

LPDIRECTINPUT8 m_pDI; // DirectInput object

LPDIRECTINPUTDEVICE8 m_pKeyboard; // DirectInput keyboard device


BYTE m_diks[256];

public:

CDirectInput();
~CDirectInput() { Cleanup(); }

bool KeyDown(int dikCode);

void Update();

bool Init(HWND hWnd);
void Cleanup();

};



#include <windows.h>
#include <dinput.h>
#include "input.h"

CDirectInput::CDirectInput()
{
m_pDI = NULL;
m_pKeyboard = NULL;

for( int i=0; i<256; i++ )
m_diks[i] = 0;
}

bool CDirectInput::KeyDown(int dikCode)
{
return ((m_diks[dikCode] & 0x80) == 0x80);
}

void CDirectInput::Update()
{
HRESULT hr = DI_OK;

// Get the input''s device state, and put the state in m_diks

ZeroMemory(&m_diks, sizeof(m_diks));
hr = m_pKeyboard->GetDeviceState(sizeof(m_diks), &m_diks);
if( FAILED(hr) )
{
m_pKeyboard->Acquire();
return;
}
}

bool CDirectInput::Init(HWND hWnd)
{

// Create a IDirectInput8*

if( FAILED( DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&m_pDI, NULL) ) )
return false;

// Create a IDirectInputDevice8* for the keyboard

if( FAILED( m_pDI->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, NULL) ) )
return false;

// Set the keyboard data format

if( FAILED( m_pKeyboard->SetDataFormat(&c_dfDIKeyboard) ) )
return false;

// Set the cooperative level on the keyboard

if( FAILED( m_pKeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND | DISCL_NOWINKEY) ) )
return false;

// Acquire the keyboard

m_pKeyboard->Acquire();

return true;
}

void CDirectInput::Cleanup()
{
if( m_pKeyboard )
{
m_pKeyboard->Release();
m_pKeyboard = NULL;
}

if( m_pDI )
{
m_pDI->Release();
m_pDI = NULL;
}
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!