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: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds