home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / CDX.ZIP / Src / Cdi / Mouse.cpp < prev   
Encoding:
C/C++ Source or Header  |  1998-09-14  |  2.2 KB  |  77 lines

  1. //////////////////////////////////////////////////////////////////////////////////
  2. // Project Name: [ CDI Class Library - CDI.lib ]
  3. // Author:       [ Dan Farley - 97308096@brookes.ac.uk ]
  4. // Source File:  [ CDI_Mouse Implementation ]
  5. // Revision:     [ 1.6 ]
  6. //////////////////////////////////////////////////////////////////////////////////
  7. #include "CDI.h"
  8.  
  9. //////////////////////////////////////////////////////////////////////////////////
  10. // CDI_Mouse Constructor
  11. //////////////////////////////////////////////////////////////////////////////////
  12. CDI_Mouse::CDI_Mouse(void)
  13. {
  14.     m_Device = NULL;
  15.     m_bActive = FALSE;
  16.     m_X = m_Y = m_Z = 0;
  17. }
  18.  
  19. //////////////////////////////////////////////////////////////////////////////////
  20. // CDI_Mouse Destructor
  21. //////////////////////////////////////////////////////////////////////////////////
  22. CDI_Mouse::~CDI_Mouse(void)
  23. {
  24.     m_Device->Unacquire();
  25.     RELEASE(m_Device);
  26. }
  27.  
  28. //////////////////////////////////////////////////////////////////////////////////
  29. // CDI_Mouse Create
  30. //////////////////////////////////////////////////////////////////////////////////
  31. HRESULT CDI_Mouse::Create(CDI_Input* input, void* hwnd)
  32. {
  33.     HRESULT rval;
  34.     LPDIRECTINPUTDEVICE lpDID = NULL;
  35.  
  36.     // Create the mouse device
  37.     rval = input->GetDI()->CreateDevice(GUID_SysMouse, &lpDID, NULL);
  38.     if(rval == DI_OK)
  39.     {
  40.         rval = lpDID->QueryInterface(IID_IDirectInputDevice2, (LPVOID*)&m_Device);
  41.         if(rval != DI_OK) return rval;
  42.  
  43.         RELEASE(lpDID);
  44.  
  45.         m_Device->SetDataFormat(&c_dfDIMouse);
  46.         m_Device->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
  47.  
  48.         rval = m_Device->Acquire();
  49.         if(rval != DI_OK) return rval;
  50.  
  51.         m_bActive = TRUE;
  52.     }
  53.  
  54.     return rval;
  55. }
  56.  
  57. //////////////////////////////////////////////////////////////////////////////////
  58. // CDI_Mouse Update
  59. //////////////////////////////////////////////////////////////////////////////////
  60. void CDI_Mouse::Update(void)
  61. {
  62.     if(m_bActive)
  63.     {
  64.         DIMOUSESTATE MouseState;
  65.  
  66.         if(m_Device->GetDeviceState(sizeof(MouseState), &MouseState) == (DIERR_INPUTLOST | DIERR_NOTACQUIRED))
  67.             m_Device->Acquire();
  68.  
  69.         m_X = MouseState.lX;
  70.         m_X = MouseState.lY;
  71.         m_Z = MouseState.lZ;
  72.  
  73.         for(int i = 0; i < 4; i++)
  74.             m_Buttons[i] = MouseState.rgbButtons[i];
  75.     }
  76. }
  77.