home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / gamesystem / source / gs_mouse.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-29  |  4.7 KB  |  218 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Module:    gsCMouse
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    12/03/00
  8. //
  9. // Base:    gsCInput
  10. //
  11. // Derived:    None
  12. //
  13. //-------------------------------------------------------------
  14.  
  15. #include "gamesystem.h"
  16.  
  17. //-------------------------------------------------------------
  18.  
  19. gsLPDIRECTINPUTDEVICE gsCMouse::m_mouse_device = 0;
  20.  
  21. //-------------------------------------------------------------
  22.  
  23. gsCMouse::gsCMouse()
  24. {
  25.     m_position = gsCPoint(0,0);
  26.  
  27.     for (int i = 0; i < gsMOUSE_NUM_BUTTONS; i++)
  28.         m_buttons[i] = false;
  29. }
  30.  
  31. //-------------------------------------------------------------
  32.  
  33. gsCMouse::~gsCMouse()
  34. {
  35.     destroy();
  36. }
  37.  
  38. //-------------------------------------------------------------
  39.  
  40. bool gsCMouse::create()
  41. {
  42.     if (!m_direct_input) {
  43.         gsREPORT("gsCMouse::create called with no direct input device");
  44.         return false;
  45.         }
  46.  
  47.     if (m_mouse_device)
  48.         return true;
  49.  
  50.     HRESULT hr;
  51.  
  52. #ifdef gsDIRECTX_NT_COMPATIBLE
  53.  
  54.     hr = m_direct_input->CreateDevice(GUID_SysMouse,
  55.                                       &m_mouse_device,
  56.                                       NULL);
  57.  
  58. #else
  59.     
  60.     hr = m_direct_input->CreateDeviceEx(GUID_SysMouse,
  61.                                         gsID_DIRECTINPUTDEVICE,
  62.                                         (void **) &m_mouse_device,
  63.                                         NULL);
  64. #endif
  65.  
  66.     if (hr != DI_OK) {
  67.         gsREPORT("gsCMouse::create couldn't create device");
  68.         return false;
  69.         }
  70.  
  71.     hr = m_mouse_device->SetDataFormat(&c_dfDIMouse);
  72.  
  73.     if (hr != DI_OK) {
  74.         gsREPORT("gsCMouse::create couldn't set data format");
  75.         return false;
  76.         }
  77.  
  78.     hr = m_mouse_device->SetCooperativeLevel(gsCApplication::getWindow(),
  79.                                              DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
  80.  
  81.     if (hr != DI_OK) {
  82.         gsREPORT("gsCMouse::create couldn't set co-op level");
  83.         return false;
  84.         }
  85.  
  86.     DIPROPDWORD dipdw;
  87.  
  88.     dipdw.diph.dwSize = sizeof(DIPROPDWORD);
  89.     dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  90.     dipdw.diph.dwObj = 0;
  91.     dipdw.diph.dwHow = DIPH_DEVICE;
  92.     dipdw.dwData = DIPROPAXISMODE_ABS;
  93.  
  94.     hr = m_mouse_device->SetProperty(DIPROP_AXISMODE,&dipdw.diph);
  95.  
  96.     if (hr != DI_OK) {
  97.         gsREPORT("gsCMouse::create couldn't set axis mode");
  98.         return false;
  99.         }
  100.  
  101.     m_active_inputs.addItem(this);
  102.  
  103.     gsREPORT("gsCMouse device created");
  104.  
  105.     return true;
  106. }
  107.  
  108. //-------------------------------------------------------------
  109.  
  110. bool gsCMouse::destroy()
  111. {
  112.     if (m_mouse_device) {
  113.  
  114.         m_active_inputs.removeItem(this);
  115.  
  116.         m_mouse_device->Unacquire();
  117.         m_mouse_device->Release();
  118.         m_mouse_device = 0;
  119.  
  120.         gsREPORT("gsCMouse device destroyed");
  121.         }
  122.  
  123.     return true;
  124. }
  125.  
  126. //-------------------------------------------------------------
  127.  
  128. bool gsCMouse::acquire()
  129. {
  130.     if (!m_mouse_device) {
  131.         gsREPORT("gsCMouse::acquire called with no mouse device");
  132.         return false;
  133.         }
  134.     
  135.     HRESULT hr;
  136.     
  137.     if (gsCApplication::isActive()) {
  138.         hr = m_mouse_device->Acquire();
  139.  
  140.         if (hr != DI_OK) {
  141.             gsREPORT("gsCMouse::acquire failed");
  142.             return false;
  143.             }
  144.         }
  145.     else {
  146.         hr = m_mouse_device->Unacquire();
  147.  
  148.         if (hr != DI_OK) {
  149.             gsREPORT("gsCMouse::acquire unacquire failed");
  150.             return false;
  151.             }
  152.         }
  153.  
  154.     return true;
  155. }
  156.  
  157. //-------------------------------------------------------------
  158.  
  159. bool gsCMouse::update()
  160. {
  161.     if (m_mouse_device) {
  162.         HRESULT hr = DIERR_INPUTLOST;
  163.  
  164.         DIMOUSESTATE m_mouse_state = { 0 };
  165.                 
  166.         while (hr == DIERR_INPUTLOST) {
  167.             hr = m_mouse_device->GetDeviceState(sizeof(DIMOUSESTATE),&m_mouse_state);
  168.             
  169.             if (hr == DIERR_INPUTLOST) {
  170.                 hr = m_mouse_device->Acquire();
  171.                 if (hr != DI_OK) {
  172.                     gsREPORT("gsCMouse::update couldn't acquire device");
  173.                     return false;
  174.                     }
  175.                 }
  176.             }
  177.  
  178.         if (hr != DI_OK) {
  179.             gsREPORT("gsCMouse::update couldn't get device state");
  180.             return false;
  181.             }
  182.  
  183.         m_position = gsCPoint(m_mouse_state.lX,m_mouse_state.lY);
  184.         m_buttons[gsMOUSE_LEFT] = ((m_mouse_state.rgbButtons[0] & 0x80) != 0);
  185.         m_buttons[gsMOUSE_RIGHT] = ((m_mouse_state.rgbButtons[1] & 0x80) != 0);
  186.         m_buttons[gsMOUSE_MIDDLE] = ((m_mouse_state.rgbButtons[2] & 0x80) != 0);
  187.  
  188.         return true;
  189.         }
  190.  
  191.     gsREPORT("gsCMouse::update called with no device created");
  192.     
  193.     return false;
  194. }
  195.  
  196. //-------------------------------------------------------------
  197.  
  198. bool gsCMouse::isActive()
  199. {
  200.     return m_mouse_device != 0;
  201. }
  202.  
  203. //-------------------------------------------------------------
  204.  
  205. gsCPoint gsCMouse::getPosition()
  206. {
  207.     return m_position;
  208. }
  209.  
  210. //-------------------------------------------------------------
  211.  
  212. bool gsCMouse::testButton(gsMouseButton button)
  213. {
  214.     return m_buttons[button];
  215. }
  216.  
  217. //-------------------------------------------------------------
  218.