Mouse.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:4k
源码类别:

游戏

开发平台:

Visual C++

  1. /*******************************************************************
  2.  *         Advanced 3D Game Programming using DirectX 7.0
  3.  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4.  *   Title: Mouse.cpp
  5.  *    Desc: Wrapper of a DirectInput mouse object
  6.  *          
  7.  * copyright (c) 1999 by Adrian Perez
  8.  * See license.txt for modification and distribution information
  9.  ******************************************************************/
  10. #include <Windows.h>
  11. #include "GameErrors.h"
  12. #include "InputLayer.h"
  13. #include "Mouse.h"
  14. cMouse::cMouse(  HWND hWnd, bool bExclusive )
  15. {
  16. m_pTarget = NULL;
  17. HRESULT hr;
  18. /**
  19.  * Create the device
  20.  */
  21. hr = Input()->GetDInput()->CreateDevice(
  22. GUID_SysMouse, 
  23.         &m_pDevice, 
  24. NULL);
  25. if( FAILED( hr ))
  26. {
  27. throw cGameError("[cMouse::Init]: Couldn't create the device!n");
  28. }
  29. /**
  30.  * Set the data format
  31.  */
  32. hr = m_pDevice->SetDataFormat(&c_dfDIMouse);
  33.   if( FAILED( hr ))
  34. {
  35. SafeRelease( m_pDevice );
  36. throw cGameError("[cMouse::Init]: SetDataFormat failedn");
  37. }
  38. /**
  39.  * Set the cooperative level
  40.  */
  41. if( bExclusive )
  42. {
  43. hr = m_pDevice->SetCooperativeLevel( hWnd, DISCL_EXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND );
  44. }
  45. else
  46. {
  47. hr = m_pDevice->SetCooperativeLevel( hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
  48. }
  49. if( FAILED( hr ))
  50. {
  51. SafeRelease( m_pDevice );
  52. throw cGameError("[cMouse::Init]: SetCooperativeLevel failedn");
  53. }
  54. // CHANGE: we used to grab the mouse here, but this would cause
  55. // a crash if the app didn't have focus when it started up.  This is,
  56. // of course, an undesirable trait :)
  57. // Instead we just set the mouse initially to be stationary, with no
  58. // buttons pressed.  We can't do a GetDeviceState here since we don't
  59. // have the device acquired.
  60. m_lastState.lX = 0;
  61. m_lastState.lY = 0;
  62. m_lastState.lZ = 0;
  63. m_lastState.rgbButtons[0] = 0;
  64. m_lastState.rgbButtons[1] = 0;
  65. m_lastState.rgbButtons[2] = 0;
  66. m_lastState.rgbButtons[3] = 0;
  67. }
  68. cMouse::~cMouse()
  69. {
  70. if( m_pDevice )
  71. {
  72. m_pDevice->Unacquire();
  73. SafeRelease( m_pDevice );
  74. }
  75. }
  76. void cMouse::SetReceiver( iMouseReceiver* pTarget )
  77. {
  78. m_pTarget = pTarget;
  79. }
  80. eResult cMouse::Update()
  81. {
  82. DIMOUSESTATE currState;
  83.     HRESULT  hr; 
  84.  
  85. // CHANGE: Polling can't hurt, but it can help.
  86.     hr = m_pDevice->Poll(); 
  87. hr = m_pDevice->GetDeviceState( sizeof(DIMOUSESTATE), (void*)&currState );
  88.     if( FAILED(hr) )
  89.     { 
  90. hr = m_pDevice->Acquire();
  91. if( FAILED( hr ) )
  92. {
  93. return resFailed;
  94. }
  95.     hr = m_pDevice->Poll(); 
  96.     hr = m_pDevice->GetDeviceState( sizeof(DIMOUSESTATE),(void*)&currState ); 
  97. if( FAILED( hr ) )
  98. {
  99. return resFailed;
  100. }
  101.     } 
  102. if( m_pTarget )
  103. {
  104. int dx = currState.lX;
  105. int dy = currState.lY;
  106. if( dx || dy )
  107. {
  108. m_pTarget->MouseMoved( dx, dy );
  109. }
  110. if( currState.rgbButtons[0] & 0x80 )
  111. {
  112. // the button got pressed.
  113. m_pTarget->MouseButtonDown( 0 );
  114. }
  115. if( currState.rgbButtons[1] & 0x80 )
  116. {
  117. // the button got pressed.
  118. m_pTarget->MouseButtonDown( 1 );
  119. }
  120. if( currState.rgbButtons[2] & 0x80 )
  121. {
  122. // the button got pressed.
  123. m_pTarget->MouseButtonDown( 2 );
  124. }
  125. if( !(currState.rgbButtons[0] & 0x80) && (m_lastState.rgbButtons[0] & 0x80) )
  126. {
  127. // the button got released.
  128. m_pTarget->MouseButtonUp( 0 );
  129. }
  130. if( !(currState.rgbButtons[1] & 0x80) && (m_lastState.rgbButtons[1] & 0x80) )
  131. {
  132. // the button got released.
  133. m_pTarget->MouseButtonUp( 1 );
  134. }
  135. if( !(currState.rgbButtons[2] & 0x80) && (m_lastState.rgbButtons[2] & 0x80) )
  136. {
  137. // the button got released.
  138. m_pTarget->MouseButtonUp( 2 );
  139. }
  140. }
  141. m_lastState = currState;
  142. return resAllGood;
  143. }
  144. eResult cMouse::Acquire()
  145. {
  146. HRESULT hr = m_pDevice->Acquire();
  147. if( FAILED(hr) )
  148. {
  149. return resFailed;
  150. }
  151. return resAllGood;
  152. }
  153. void cMouse::UnAcquire()
  154. {
  155. m_pDevice->Unacquire();
  156. }