DXUtil.h
上传用户:ghyvgy
上传日期:2009-05-26
资源大小:547k
文件大小:4k
源码类别:

其他游戏

开发平台:

Python

  1. /*
  2. s_p_oneil@hotmail.com
  3. Copyright (c) 2000, Sean O'Neil
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice,
  8.   this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice,
  10.   this list of conditions and the following disclaimer in the documentation
  11.   and/or other materials provided with the distribution.
  12. * Neither the name of this project nor the names of its contributors
  13.   may be used to endorse or promote products derived from this software
  14.   without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef __DXUtil_h__
  28. #define __DXUtil_h__
  29. #include <dinput.h>
  30. class CInterfacePtrList : public CPtrList
  31. {
  32. public:
  33. CInterfacePtrList() : CPtrList() {}
  34. // WARNING! Multiple inheritance breaks if the pointer is not casted properly before this function is called
  35. // (Casting to IEventListener here also breaks multiple inheritance because there can be more than one IEventListener in the inheritance tree.)
  36. void AddListener(void *pListener)
  37. {
  38. for(CPtrListNode *pNode = GetHead(); pNode->IsInList(); pNode = pNode->GetNext())
  39. {
  40. if(pListener == pNode->GetData())
  41. return;
  42. }
  43. AddTail(pListener);
  44. }
  45. // WARNING! Multiple inheritance breaks if the pointer is not casted properly before this function is called
  46. // (Casting to IEventListener here also breaks multiple inheritance because there can be more than one IEventListener in the inheritance tree.)
  47. void RemoveListener(void *pListener)
  48. {
  49. for(CPtrListNode *pNode = GetHead(); pNode->IsInList(); pNode = pNode->GetNext())
  50. {
  51. if(pListener == pNode->GetData())
  52. {
  53. delete pNode;
  54. return;
  55. }
  56. }
  57. }
  58. };
  59. interface IKeyboardListener
  60. {
  61. virtual void OnKeyDown(int nKey) = 0;
  62. virtual void OnKeyUp(int nKey) = 0;
  63. };
  64. interface IMouseListener
  65. {
  66. virtual void OnMouseButtonDown(int nButton) = 0;
  67. virtual void OnMouseButtonUp(int nButton) = 0;
  68. };
  69. class CInputEngine
  70. {
  71. // Attributes
  72. protected:
  73. CInterfacePtrList m_iplKeyboardListeners;
  74. CInterfacePtrList m_iplMouseListeners;
  75. IDirectInput *m_pDirectInput;
  76. IDirectInputDevice *m_pKeyboard;
  77. IDirectInputDevice *m_pMouse;
  78. char m_kState[256];
  79. DIMOUSESTATE m_mState;
  80. // Operations
  81. public:
  82. CInputEngine() { m_pKeyboard = m_pMouse = NULL; }
  83. bool Attach();
  84. void Detach();
  85. void Pause();
  86. bool Restore();
  87. bool Poll();
  88. bool GetKeyState(int n) { return (m_kState[n] & 0x80) != 0; }
  89. bool GetMouseButtonState(int n) { return (m_mState.rgbButtons[n] & 0x80) != 0; }
  90. int GetMouseXAxis() { return m_mState.lX; }
  91. int GetMouseYAxis() { return m_mState.lY; }
  92. int GetMouseZAxis() { return m_mState.lZ; }
  93. void AddKeyboardListener(IKeyboardListener *pListener) { m_iplKeyboardListeners.AddListener(pListener); }
  94. void RemoveKeyboardListener(IKeyboardListener *pListener) { m_iplKeyboardListeners.RemoveListener(pListener); }
  95. void AddMouseListener(IMouseListener *pListener) { m_iplMouseListeners.AddListener(pListener); }
  96. void RemoveMouseListener(IMouseListener *pListener) { m_iplMouseListeners.RemoveListener(pListener); }
  97. };
  98. #endif // __DXUtil_h__