Home

Code DX

Code GL

FMOD

Files

Links

About

Code on this page.

How to create and manage input devices in you own game app’s.

Using DirectX 7 Interfaces.

All classes intuitive structured.

 

Input Manager

Keyboard class

Mouse class

/*

Global variables definition

global variables this is structure I wrote for simle acces

to important data types&structures

defined lyke

struct sdi_GV

{

HWND m_hwnd;//handler to app window

HINSTANCE m_hinst;//instance of app

And so on…

And so on…

};

*/

#ifndef SDIINPUTC_H

#define SDIINPUTC_H

//header class to manage input devices

#include "sdikeyboard.h"//see bottom

#include "sdimous.h"//see bottom

class sdiinputc //this is main input manager

{

public:

sdiinputc();//constructor

~sdiinputc();//destructor

bool Create(bool reqK,bool reqM);//requesting keyboard and mouse devices

bool GetStatus();

sdikeyboard*GetKIP();

sdimous*GetMIP();

private:

bool activated;//manager active ?

bool mousactivated;//mouse device active ?

bool keyboardactivated; //keyboard device active ?

sdikeyboard*keyboard;//pointer to keyboard object

sdimous*mouse; //pointer to mouse object

LPDIRECTINPUT7 m_DI;

void Destroy();

void DIterm();

};

#endif

//implementation of input manager

#include "sdiinputc.h"

#include "D3DUtil.h"

#include "sdiglobvars.h"

extern sdi_GV g_sys_var;

//return status of manager

bool sdiinputc::

GetStatus()

{

return activated;

}

//return pointer to keyboard object

sdikeyboard*sdiinputc::

GetKIP()

{

if(keyboardactivated)return keyboard;

return NULL;

}

//return pointer to mouse object

sdimous*sdiinputc::

GetMIP()

{

if(mousactivated)return mouse;

return NULL;

}

sdiinputc::sdiinputc()

{

keyboard=NULL;

mouse=NULL;

activated=false;

mousactivated=false;

keyboardactivated=false;

m_DI=NULL;

}

sdiinputc::~sdiinputc()

{

Destroy();

}

bool sdiinputc::

Create(bool reqK,bool reqM)

{

HRESULT hr;

if(activated==true)return activated;

hr=DirectInputCreateEx(g_sys_var.m_hinst,

DIRECTINPUT_VERSION,

IID_IDirectInput7,

(void**)&m_DI,

NULL);

if FAILED(hr)

{

// DirectInput not available; take appropriate action

DIterm();return activated;

}

if(reqK)

{

keyboard=new sdikeyboard;

if(keyboard==NULL)

{

DIterm();

return activated;

}

keyboardactivated=keyboard->Create(m_DI);

if(keyboardactivated==false)

{

DIterm();

return activated;

}

}

if(reqM)

{

mouse=new sdimous;

if(mouse==NULL)

{

DIterm();

return activated;

}

mousactivated=mouse->Create(m_DI);

if(mousactivated==false)

{

DIterm();

return activated;

}

}

if(mousactivated||keyboardactivated)activated=true;

return activated;

}

void sdiinputc::

Destroy()

{

DIterm();

}

void sdiinputc::

DIterm()

{

if(m_DI)

{

if(keyboard)

{

SAFE_DELETE(keyboard);

keyboardactivated=false;

}

if(mouse)

{

SAFE_DELETE(mouse);

mousactivated=false;

}

SAFE_RELEASE(m_DI);

}

activated=false;

}

//keyboard class definition

#ifndef SDIKEYBOARD_H

#define SDIKEYBOARD_H

#include <dinput.h>

class sdikeyboard

{

public:

sdikeyboard();

~sdikeyboard();

bool Create(LPDIRECTINPUT7 lpDI);

void Destroy();

bool KeyPressed(int key);

bool activated;

private:

void DIterm();

LPDIRECTINPUTDEVICE7 m_Keyboard;

char m_keyState[256];

};

#endif

//implementation of keyboard object

#include "sdikeyboard.h"

#include "D3DUtil.h"

#include "sdiglobvars.h"

extern sdi_GV g_sys_var;

sdikeyboard::sdikeyboard()

{

activated=false;

m_Keyboard=NULL;

}

sdikeyboard::~sdikeyboard()

{

Destroy();

}

void sdikeyboard::

DIterm()

{

if (m_Keyboard)

{

/*Always unacquire device before

calling Release().*/

m_Keyboard->Unacquire();

SAFE_RELEASE(m_Keyboard);

}

activated=false;

}

bool sdikeyboard::

Create(LPDIRECTINPUT7 lpDI)

{

HRESULT hr;

try

{

if(activated)return activated;

hr=lpDI->

CreateDeviceEx(GUID_SysKeyboard,

IID_IDirectInputDevice7,

(void**)&m_Keyboard,

NULL);

if FAILED(hr)throw(hr);

hr=m_Keyboard->

SetDataFormat(&c_dfDIKeyboard);

if FAILED(hr)throw(hr);

hr=m_Keyboard->

SetCooperativeLevel(g_sys_var.m_hwnd,

/*DISCL_EXCLUSIVE*/

DISCL_NONEXCLUSIVE|

//DISCL_NOWINKEY |

DISCL_BACKGROUND);

if FAILED(hr)throw(hr);

activated=true;

}

catch(HRESULT thr)

{

if(thr!=DI_OK)DIterm();

return activated;

}

return activated;

}

void sdikeyboard::

Destroy()

{

DIterm();

}

bool sdikeyboard::

KeyPressed(int key)

{

if(!activated)return activated;

HRESULT hr;

hr=m_Keyboard->

GetDeviceState(sizeof(m_keyState),

(LPVOID)&m_keyState);

if FAILED(hr)

{

// If it failed, the device has probably been lost.

// We should check for (hr == DIERR_INPUTLOST)

// and attempt to reacquire it here.

hr=m_Keyboard->Acquire();

if FAILED(hr)return false;

hr=m_Keyboard->

GetDeviceState(sizeof(m_keyState),

(LPVOID)&m_keyState);

if FAILED(hr)return false;

}

if(m_keyState[key]&0x80)return true;

return false;

}

//definition of mouse class

#ifndef SDIMOUS_H

#define SDIMOUS_H

#include <dinput.h>

#include "D3DTextr.h"

class sdimous

{

public:

sdimous();

~sdimous();

bool Create(LPDIRECTINPUT7 lpDI);

void Render(LPDIRECT3DDEVICE7 pd3dDevice);

bool ProccedMouse();

bool GetPos(POINT*pt);

bool LBP(RECT*pt);

bool RBP(RECT*pt);

bool LBP();

bool RBP();

bool activated;

private:

void Destroy();

void DIterm();

LPDIRECTINPUTDEVICE7 m_Mouse;

DIMOUSESTATE curstate;

POINT pos;

char*m_texture;

D3DTLVERTEX m_Rect[4];

};

#endif

//implementation of mouse object

#define D3D_OVERLOADS

#include "sdimous.h"

#include "D3DUtil.h"

#include "sdiglobvars.h"

extern sdi_GV g_sys_var;

sdimous::sdimous()

{

activated=false;

m_Mouse=NULL;

m_texture=NULL;

}

sdimous::~sdimous()

{

Destroy();

}

bool sdimous::

Create(LPDIRECTINPUT7 lpDI)

{

HRESULT hr;

try

{

hr=lpDI->

CreateDeviceEx(GUID_SysMouse,

IID_IDirectInputDevice7,

(void**)&m_Mouse,

NULL);

if FAILED(hr)throw(hr);

hr=m_Mouse->SetDataFormat(&c_dfDIMouse);

if FAILED(hr)throw(hr);

hr=m_Mouse->SetCooperativeLevel(g_sys_var.m_hwnd,

/*DISCL_EXCLUSIVE |*/

DISCL_NONEXCLUSIVE|

/*DISCL_NOWINKEY |*/

DISCL_BACKGROUND);

if FAILED(hr)throw(hr);

hr=m_Mouse->Acquire();

if FAILED(hr)throw(hr);

hr=m_Mouse->

GetDeviceState(sizeof(DIMOUSESTATE),&curstate);

if FAILED(hr)throw(hr);

if((m_texture=new char[strlen("mar1.bmp")+1])!=NULL)

strcpy(m_texture,"mar1.bmp");

}

catch(HRESULT thr)

{

if(thr!=DI_OK)DIterm();

return activated;

}

pos.x=0;

pos.y=0;

activated=true;

return activated;

}

//this function drawing cursor at the point screen

void sdimous::Render(LPDIRECT3DDEVICE7 pd3dDevice)

{

ProccedMouse();

GetPos(&pos);

if(m_texture)

{

m_Rect[0]=D3DTLVERTEX(D3DVECTOR((float)pos.x,(float)(pos.y+32),0.99f),0.5f,-1,0,0,1);

m_Rect[1]=D3DTLVERTEX(D3DVECTOR((float)pos.x,(float)pos.y,0.99f),0.5f,-1,0,0,0); m_Rect[2]=D3DTLVERTEX(D3DVECTOR((float)(pos.x+32),(float)(pos.y+32),0.99f),0.5f,-1,0,1,1);

m_Rect[3]=D3DTLVERTEX(D3DVECTOR((float)(pos.x+32),(float)pos.y,0.99f),0.5f,-1,0,1,0);

pd3dDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE,TRUE);

pd3dDevice->SetRenderState( D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA );

pd3dDevice->SetRenderState( D3DRENDERSTATE_DESTBLEND, D3DBLEND_ONE );

pd3dDevice->SetTexture(0,D3DTextr_GetSurface(m_texture));

pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,D3DFVF_TLVERTEX,m_Rect,4,0);

pd3dDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE,FALSE);

}

}

void sdimous::

Destroy()

{

DIterm();

}

bool sdimous::

ProccedMouse()

{

HRESULT hr;

if(!activated)return activated;

hr=m_Mouse->GetDeviceState(sizeof(DIMOUSESTATE),&curstate);

if FAILED(hr)

{

hr=m_Mouse->Acquire();

if FAILED(hr)return fa;

hr=m_Mouse->GetDeviceState(sizeof(DIMOUSESTATE),&curstate);

if FAILED(hr)return false;

}

return true;

}

bool sdimous::

GetPos(POINT*pt)

{

if(!activated)return activated;

intdx=curstate.lX,dy=curstate.lY;

if(dx||dy)

{

pos.x=pos.x+dx;

pos.y=pos.y+dy;

}

if(pt)

{

pt->x=pos.x;

pt->y=pos.y;

}

return true;

}

bool sdimous::

LBP(RECT*pt)

{

if(!activated)return activated;

if(pt&&curstate.rgbButtons[0]&0x80)

{

if(pos.x<=pt->right&&pos.x>=pt->left&&

pos.y<=pt->bottom&&pos.y>=pt->top)

return true;

}

return false;

}

bool sdimous::

RBP(RECT*pt)

{

if(!activated)return activated;

if(pt&&curstate.rgbButtons[1]&0x80)

{

if(pos.x<=pt->right&&pos.x>=pt->left&&

pos.y<=pt->bottom&&pos.y>=pt->top)

return true;

}

return false;

}

bool sdimous::LBP()

{

if(!activated)return activated;

if(curstate.rgbButtons[0]&0x80)return true;

return false;

}

bool sdimous::RBP()

{

if(!activated)return activated;

if(curstate.rgbButtons[1]&0x80)return true;

return false;

}

void sdimous::

DIterm()

{

if (m_Mouse)

{

/*

Always unacquire device before calling Release().

*/

m_Mouse->Unacquire();

SAFE_RELEASE(m_Mouse);

}

SAFE_DELETE(m_texture);

activated=false;

}