dxmouse.cpp
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:3k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. #include "dxmouse.h"
  2. bool dxmouse::mousedown(int button)
  3. {
  4. return mouseState.rgbButtons[button] & 0x80;
  5. }
  6. // 初始化创建及设置,并获得鼠标输入状态
  7. bool dxmouse::Init(void)
  8. {
  9. HRESULT hr;
  10. // 创建Direct输入对象
  11. hr= (DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&lpdi, NULL));
  12. if(FAILED(hr))
  13. {
  14. MessageBox(hWnd, "Could not create main DInput object", "ERROR", MB_OK);
  15. return false;
  16. }
  17. // 创建键盘输入设备
  18. hr= (lpdi->CreateDevice(GUID_SysKeyboard, &lpdiKeyboard, NULL));
  19. if(FAILED(hr))
  20. {
  21. MessageBox(hWnd, "Could not create keyboard's object",  "ERROR",MB_OK);
  22. Shutdown();
  23. return false;
  24. }
  25. // 设置键盘数据格式
  26. hr= (lpdiKeyboard->SetDataFormat(&c_dfDIKeyboard));
  27. if(FAILED(hr))
  28. {
  29. MessageBox(hWnd, "Could not set keyboard's data format","ERROR",MB_OK);
  30. Shutdown();
  31. return false;
  32. }
  33. hr= (lpdiKeyboard->SetCooperativeLevel(hWnd, DISCL_BACKGROUND|DISCL_NONEXCLUSIVE));
  34. if(FAILED(hr))
  35. {
  36. MessageBox(hWnd, "Could not set keyboard's cooperation level", "ERROR", MB_OK);
  37. Shutdown();
  38. return false;
  39. }
  40. lpdiKeyboard->Acquire();
  41. // 创建鼠标输入设备
  42. hr= lpdi->CreateDevice(GUID_SysMouse, &lpdiMouse, NULL);
  43. if(FAILED(hr))
  44. {
  45. MessageBox(hWnd, "Could not set create the mouse device", "ERROR", MB_OK);
  46. Shutdown();
  47. return FALSE;
  48. }
  49. // 设置鼠标数据格式
  50. hr= lpdiMouse->SetDataFormat(&c_dfDIMouse);
  51. if(FAILED(hr)) 
  52. {
  53. MessageBox(hWnd, "Could not set the mouse's data format", "ERROR", MB_OK);
  54. Shutdown();
  55. return FALSE;
  56. }
  57. hr= lpdiMouse->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
  58. if(FAILED(hr)) 
  59. {
  60. MessageBox(hWnd, "Could not set the mouse's behavior", "ERROR", MB_OK);
  61. Shutdown();
  62. return FALSE;
  63. }
  64. hr= lpdiMouse->Acquire();
  65. if(FAILED(hr))
  66. {
  67. MessageBox(hWnd, "Could not acquire the mouse", "ERROR", MB_OK);
  68. Shutdown();
  69. }
  70. // 获得鼠标设备状态
  71. hr= lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseState);
  72. if(FAILED(hr))
  73. {
  74. MessageBox(hWnd, "The mouse has been lost in initialization", "ERROR", MB_OK);
  75. Shutdown();
  76. }
  77. return true;
  78. }
  79. // 遇到错误调用
  80. void dxmouse::Shutdown(void)
  81. {
  82. if(lpdiMouse!=NULL)
  83. {
  84. lpdiMouse->Unacquire();
  85. lpdiMouse->Release();
  86. lpdiMouse= NULL;
  87. }
  88. if(lpdiKeyboard!=NULL)
  89. {
  90. lpdiKeyboard->Unacquire();
  91. lpdiKeyboard->Release();
  92. lpdiKeyboard= NULL;
  93. }
  94. if(lpdi!=NULL)
  95. {
  96. lpdi->Release();
  97. lpdi=NULL;
  98. }
  99. }
  100. // 更新信息
  101. void dxmouse::Update(void)
  102. {
  103. HRESULT hr;
  104. // 检测键盘设备,若丢失,重找回
  105.     hr= (lpdiKeyboard->GetDeviceState(sizeof(UCHAR[256]),(LPVOID)&keyBuffer));
  106.     if(FAILED(hr))
  107.         if(hr==DIERR_INPUTLOST) 
  108. {
  109. hr= (lpdiKeyboard->Acquire());
  110. if(FAILED(hr))
  111. {
  112. MessageBox(hWnd, "Keyboard has been lost", "ERROR", MB_OK);
  113. Shutdown();
  114. }
  115. }
  116. }
  117. // 检测鼠标设备,若丢失,重找回
  118.     hr= (lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&mouseState));
  119.     if(FAILED(hr))
  120.         if(hr==DIERR_INPUTLOST) 
  121. {
  122. hr= (lpdiMouse->Acquire());
  123. if(FAILED(hr))
  124. {
  125. MessageBox(hWnd, "The mouse has been lost", "ERROR", MB_OK);
  126. Shutdown();
  127. }
  128. }
  129. }
  130. // 得到有关鼠标的信息
  131. GetWheelMovement();
  132. GetMouseMovement();
  133. }
  134. void dxmouse::GetWheelMovement(void)
  135. {
  136. z= mouseState.lZ;
  137. }
  138. void dxmouse::GetMouseMovement(void)
  139. {
  140. x= mouseState.lX;
  141. y= mouseState.lY;
  142. }