InputSystem.h
Upload User: xuczgm
Upload Date: 2022-04-25
Package Size: 8601k
Code Size: 2k
Category:

Special Effects

Development Platform:

Visual C++

  1. #ifndef __INPUT_SYSTEM_INCLUDED__
  2. #define __INPUT_SYSTEM_INCLUDED__
  3. #include <dinput.h>
  4. #define IS_USEKEYBOARD  1
  5. #define IS_USEMOUSE     2
  6. #define IS_USEJOYSTICK  4
  7. class CKeyboard
  8. {
  9. public:
  10.   CKeyboard(LPDIRECTINPUT8 pDI, HWND hwnd);
  11.   ~CKeyboard();
  12.   bool  KeyDown(int key) { return (m_keys[key] & 0x80) ? true : false; }
  13.   bool  KeyUp(int key) { return (m_keys[key] & 0x80) ? false : true; }
  14.   bool  Update();
  15.   void  Clear() { ZeroMemory(m_keys, 256 * sizeof(char)); }
  16.   bool  Acquire();
  17.   bool  Unacquire();
  18. private:
  19.   LPDIRECTINPUTDEVICE8  m_pDIDev;
  20.   char    m_keys[256];
  21. };
  22. class CMouse
  23. {
  24. public:
  25.   CMouse(LPDIRECTINPUT8 pDI, HWND hwnd, bool isExclusive = true);
  26.   ~CMouse();
  27.   bool  ButtonDown(int button) { return (m_state.rgbButtons[button] & 0x80) ? true : false; }
  28.   bool  ButtonUp(int button) { return (m_state.rgbButtons[button] & 0x80) ? false : true; }
  29.   void  GetMovement(int &dx, int &dy) { dx = m_state.lX; dy = m_state.lY; }
  30.   bool  Update();
  31.   bool  Acquire();
  32.   bool  Unacquire();
  33. private:
  34.   LPDIRECTINPUTDEVICE8  m_pDIDev;
  35.   DIMOUSESTATE          m_state;
  36. };
  37. class CJoystick
  38. {
  39. public:
  40.   CJoystick(LPDIRECTINPUT8 pDI, HINSTANCE appInstance);
  41.   ~CJoystick();
  42.   bool  Update();
  43.   bool  Acquire();
  44.   bool  Unacquire();
  45. private:
  46.   LPDIRECTINPUTDEVICE8  m_pDIDev;
  47. };
  48. class CInputSystem
  49. {
  50. public:
  51. CInputSystem() { }
  52. ~CInputSystem() { Shutdown(); }
  53.   bool  Initialize(HWND hwnd, HINSTANCE appInstance, bool isExclusive, DWORD flags = 0);
  54.   bool  Shutdown();
  55.   void  AcquireAll();
  56.   void  UnacquireAll();
  57.   CKeyboard  *GetKeyboard() { return m_pKeyboard; }
  58.   CMouse     *GetMouse()    { return m_pMouse; }
  59.   CJoystick  *GetJoystick() { return m_pJoystick; }
  60.   bool  Update();
  61.   bool  KeyDown(int key) { return (m_pKeyboard && m_pKeyboard->KeyDown(key)); }
  62.   bool  KeyUp(int key) { return (m_pKeyboard && m_pKeyboard->KeyUp(key)); }
  63.   bool  ButtonDown(int button) { return (m_pMouse && m_pMouse->ButtonDown(button)); }
  64.   bool  ButtonUp(int button) { return (m_pMouse && m_pMouse->ButtonUp(button)); }
  65.   void  GetMouseMovement(int &dx, int &dy) { if (m_pMouse) m_pMouse->GetMovement(dx, dy); }
  66. private:
  67.   CKeyboard  *m_pKeyboard;
  68.   CMouse     *m_pMouse;
  69.   CJoystick  *m_pJoystick;
  70.   
  71.   LPDIRECTINPUT8 m_pDI;
  72. };
  73. #endif //__INPUT_SYSTEM_INCLUDED__