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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds