Direct Input 8 Wrapper

Environment: VC6
Requires: DirectX 8 SDK

Use this class to setup and handle input from keyboard, mouse, and one joystick.

Instructions(DI only):

  • Derive a class from CDirectInput8
  • Override the input processing functions for each device used: HRESULT ProcessMouse(); HRESULT ProcessKB(); HRESULT ProcessJoy();(Don't forget to call base function at the beginning of each function.)
  • Call "InitDI(HWND hWnd, HINSTANCE hInstance, DWORD ToUse)"
  • The "ToUse" can take any combination of "USEMOUSE", "USEKEYBOARD", "USEJOYSTICK" flags depending on what devices you want to use

Instructions(Used in conjunction with D3D8 Framework):

  • Perform all steps above
  • Call "HRESULT InitDeviceObjects(LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* pCursorFile = NULL/*Cursor image file*/)" in "InitDeviceObjects" of derived framework class
  • Call "HRESULT DrawMouse(LPDIRECT3DDEVICE8 pd3dDevice)" in "Render" of derived framework class
  • Call "HRESULT DeleteDeviceObjects() in "DeleteDeviceObjects" of derived framework class
  • Call "HRESULT InvalidateDeviceObjects() in "InvalidateDeviceObjects" of derived framework class
  • Call "HRESULT RestoreDeviceObjects(LPDIRECT3DDEVICE8 pd3dDevice) in "RestoreDeviceObjects" of derived framework class

Call ProcessMouse, ProcessKB. and/or ProcessJoy in each render loop to poll each device
Use m_js,  m_MouseState and buffer members to access joy, mouse, and keyboard data respectively

 

EDIT THE CODE AS YOU NEED. NO RESTRICTIONS

Definitions (Not including implementation):

#define KEYDOWN(name, key) (name[key] & 0x80)
#define KEYUP(name, key) (!(name[key] & 0x80))

#define USEKEYBOARD  0x00000001L
#define USEMOUSE     0x00000002L
#define USEJOYSTICK  0x00000004L

////////////////////////////////////////////////////////////
//Cursor vertex structure for Direct3D
struct CV2D_T1D
{
  D3DXVECTOR4 p;
  DWORD diffuse;
  float tu;
  float tv;
};
#define D3DFVF_CV2D_T1D (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX0)

inline CV2D_T1D InitCV2D_T1D( D3DXVECTOR4 p, 
                              D3DCOLOR diffuse, 
                              float tu, float tv)
{
  CV2D_T1D v;
  v.p = p;
  v.diffuse = diffuse;
  v.tu = tu;
  v.tv = tv;

  return v;
}
////////////////////////////////////////////////////////////

class CDirectInput8 
{
public:
  //Load cursor tex from resource

  //Set cursor hot spot
  void SetHotSpot(POINT hspot);

  //8 Element Array, TRUE if Button [n] is up
  BOOL* m_bMouseUp;

  //Mouse Position
  POINT* m_pMousePos;

  //Cursor HotSpot
  POINT* m_pHotSpot;

  //DI Device Objects
  LPDIRECTINPUTDEVICE8 m_pDIJoy;
  LPDIRECTINPUTDEVICE8 m_pDIKeybrd;
  LPDIRECTINPUTDEVICE8 m_pDIMouse;

  //Device State Buffers
  DIJOYSTATE2 m_js;
  DIMOUSESTATE2 m_MouseState;
  char buffer[256];
  DWORD m_dwElements;

  //DI Object
  LPDIRECTINPUT8 m_pDI;

  //Cursor Vertex Buffer
  LPDIRECT3DVERTEXBUFFER8 m_pVB;

  //Cursor Texture
  LPDIRECT3DTEXTURE8 m_pCursorTex;
 
  //DI Device Capabilities
  DIDEVCAPS m_DIDevCaps;

  //Initialize Direct Input
  HRESULT InitDI(HWND hWnd, HINSTANCE hInstance, DWORD ToUse);
 
  //Overridable Input Processing Methods
  virtual HRESULT ProcessMouse();
  virtual HRESULT ProcessKB();
  virtual HRESULT ProcessJoy();

  //Check if Mouse Buttons Up
  BOOL MButtonUp(BYTE button);
  BOOL AllMouseUp();

  //Release and Delete DI Objects
  void DIShutdown();

  //Required for D3D Framework
  HRESULT InitDeviceObjects( LPDIRECT3DDEVICE8 pd3dDevice, 
                             TCHAR* pCursorFile = NULL);
  HRESULT DrawMouse(LPDIRECT3DDEVICE8 pd3dDevice);
  HRESULT DeleteDeviceObjects();
  HRESULT InvalidateDeviceObjects();
  HRESULT RestoreDeviceObjects(LPDIRECT3DDEVICE8 pd3dDevice);

  //Construction/Destruction
  CDirectInput8();
  virtual ~CDirectInput8();

protected:
  //Mouse Handle
  HANDLE m_hMouse;
  HRESULT hr;

private:
//Joystick callbacks friend BOOL __stdcall EnumAxesCallback( const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* pContext ); friend BOOL __stdcall EnumJoysticksCallback( const DIDEVICEINSTANCE* pdidInstance, VOID* pContext ); }; //Global CDirectInput8 Instance extern CDirectInput8* g_pDI8;

Downloads

Download source (includes implementation) - 5 Kb


Comments

  • Synthesize

    Posted by Legacy on 12/15/2001 12:00am

    Originally posted by: Slick

    Any comments on how best to synthesize some keystrokes in directx after getting the keyboard input? Is sendinput or keybd_event all you can use?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds