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

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • Not long ago, security was viewed as one of the biggest obstacles to widespread adoption of cloud-based deployments for enterprise software solutions. However, the combination of advancing technology and an increasing variety of threats that companies must guard against is rapidly turning the tide. Cloud vendors typically offer a much higher level of data center and virtual system security than most organizations can or will build out on their own. Read this white paper to learn the five ways that cloud …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds