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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds