home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / ReplicaNetFreewareV5_4.exe / data1.cab / Program_Executable_Files / Common3DApp / Src / diutil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  8.2 KB  |  282 lines

  1. //-----------------------------------------------------------------------------
  2. // File: DIUtil.cpp
  3. //
  4. // Desc: DirectInput framework class using semantic mapping.  Feel free to use 
  5. //       this class as a starting point for adding extra functionality.
  6. //
  7. // Copyright (C) 1995-2000 Microsoft Corporation. All Rights Reserved.
  8. //-----------------------------------------------------------------------------
  9. #define DIRECTINPUT_VERSION 0x0800
  10. #define STRICT
  11. #include <basetsd.h>
  12. #include <tchar.h>
  13. #include <stdio.h>
  14. #include "DIUtil.h"
  15. #include "DXUtil.h"
  16.  
  17.  
  18.  
  19.  
  20.  
  21. //-----------------------------------------------------------------------------
  22. // Name:
  23. // Desc:
  24. //-----------------------------------------------------------------------------
  25. CInputDeviceManager::CInputDeviceManager()
  26. {
  27.     //call CoInitialize();
  28.     CoInitialize(NULL);
  29.  
  30.     m_dwNumDevices = 0;
  31.     m_pDI          = NULL;
  32. }
  33.  
  34.  
  35.  
  36.  
  37. //-----------------------------------------------------------------------------
  38. // Name:
  39. // Desc:
  40. //-----------------------------------------------------------------------------
  41. CInputDeviceManager::~CInputDeviceManager()
  42. {
  43.     // Release() all devices
  44.     for( DWORD i=0; i<m_dwNumDevices; i++ )
  45.     {
  46.         m_pdidDevices[i]->Unacquire();
  47.         m_pdidDevices[i]->Release();
  48.         m_pdidDevices[i] = NULL;
  49.     }
  50.  
  51.     // Release() base object
  52.     SAFE_RELEASE( m_pDI );
  53.  
  54.     //call CoUninitialize();
  55.     CoUninitialize();
  56. }
  57.  
  58.  
  59.  
  60.  
  61. //-----------------------------------------------------------------------------
  62. // Name:
  63. // Desc:
  64. //-----------------------------------------------------------------------------
  65. HRESULT CInputDeviceManager::GetDevices( LPDIRECTINPUTDEVICE8** ppDevice, 
  66.                                          DWORD* pdwCount )
  67. {
  68.     if( NULL==ppDevice || NULL==pdwCount )
  69.         return E_INVALIDARG;
  70.  
  71.     (*ppDevice) = m_pdidDevices;
  72.     (*pdwCount) = m_dwNumDevices;
  73.  
  74.     return S_OK;
  75. }
  76.  
  77.  
  78.  
  79.  
  80. //-----------------------------------------------------------------------------
  81. // Name:
  82. // Desc:
  83. //-----------------------------------------------------------------------------
  84. HRESULT CInputDeviceManager::AddDevice( const DIDEVICEINSTANCE* pdidi, 
  85.                                         const LPDIRECTINPUTDEVICE8 pdidDevice )
  86. {
  87.     HRESULT hr;
  88.     DWORD   dwDeviceType = pdidi->dwDevType;
  89.  
  90.     pdidDevice->Unacquire();
  91.  
  92.     // Set the device's coop level
  93.     if( GET_DIDEVICE_TYPE(dwDeviceType) == DI8DEVTYPE_MOUSE )
  94.         hr = pdidDevice->SetCooperativeLevel( m_hWnd, DISCL_EXCLUSIVE|DISCL_FOREGROUND );
  95.     else
  96.         hr = pdidDevice->SetCooperativeLevel( m_hWnd, DISCL_NONEXCLUSIVE|DISCL_BACKGROUND );
  97.     if( FAILED(hr) )
  98.         return hr;
  99.  
  100.     // Set relative mode for mouse
  101.     if( GET_DIDEVICE_TYPE(dwDeviceType) == DI8DEVTYPE_MOUSE )
  102.     {
  103.         DIPROPDWORD dipdw;
  104.         dipdw.diph.dwSize       = sizeof(DIPROPDWORD);
  105.         dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  106.         dipdw.diph.dwObj        = 0;
  107.         dipdw.diph.dwHow        = DIPH_DEVICE;
  108.         dipdw.dwData            = DIPROPAXISMODE_REL;
  109.         hr = pdidDevice->SetProperty( DIPROP_AXISMODE, &dipdw.diph );
  110.     }
  111.  
  112.     m_pdidDevices[m_dwNumDevices] = pdidDevice;
  113.     m_pdidDevices[m_dwNumDevices]->AddRef();
  114.  
  115.     hr = m_pdidDevices[m_dwNumDevices]->BuildActionMap( &m_diaf, m_strUserName, 0 );
  116.     hr = m_pdidDevices[m_dwNumDevices]->SetActionMap( &m_diaf, m_strUserName, 0 );
  117.  
  118.     m_dwNumDevices++;
  119.  
  120.     // Continue enumerating suitable devices
  121.     return S_OK;
  122. }
  123.  
  124.  
  125.  
  126.  
  127. //-----------------------------------------------------------------------------
  128. // Name:
  129. // Desc:
  130. //-----------------------------------------------------------------------------
  131. BOOL CALLBACK EnumSuitableDevicesCB( LPCDIDEVICEINSTANCE pdidi, 
  132.                                      LPDIRECTINPUTDEVICE8A pdidDevice, 
  133.                                      DWORD dwFlags, DWORD dwDeviceRemaining,
  134.                                      VOID* pContext )
  135. {
  136.     // Add the device to the device manager's internal list
  137.     ((CInputDeviceManager*)pContext)->AddDevice( pdidi, pdidDevice );
  138.  
  139.     // Continue enumerating suitable devices
  140.     return DIENUM_CONTINUE;
  141. }
  142.  
  143.  
  144.  
  145.  
  146. //-----------------------------------------------------------------------------
  147. // Name:
  148. // Desc:
  149. //-----------------------------------------------------------------------------
  150. HRESULT CInputDeviceManager::SetActionFormat( DIACTIONFORMAT& diaf, BOOL bReenumerate )
  151. {
  152.     HRESULT hr = S_OK;
  153.  
  154.     // Store the new action format
  155.     m_diaf = diaf;
  156.  
  157.     // Only destroy and re-enumerate devices if the caller explicitly wants to. The 
  158.     // device list may be used within a loop, and kicking off an enumeration while 
  159.     // the device array is in use would cause problems.
  160.     if( bReenumerate )
  161.     {
  162.         // Cleanup any previously enumerated devices
  163.         for( DWORD i=0; i<m_dwNumDevices; i++ )
  164.         {
  165.             m_pdidDevices[i]->Unacquire();
  166.             m_pdidDevices[i]->Release();
  167.             m_pdidDevices[i] = NULL;
  168.         }
  169.         m_dwNumDevices = 0;
  170.  
  171.         // Enumerate suitable DirectInput devices
  172.         hr = m_pDI->EnumDevicesBySemantics( m_strUserName, &m_diaf, 
  173.                                             EnumSuitableDevicesCB, this, 0L );
  174.     }
  175.     else // Just apply the new maps.
  176.     {
  177.         // Devices must be unacquired to have a new action map set.
  178.         UnacquireDevices();
  179.  
  180.         // Apply the new action map to the current devices.
  181.         for( DWORD i=0; i<m_dwNumDevices; i++ )
  182.         {
  183.             m_pdidDevices[i]->BuildActionMap( &m_diaf, m_strUserName, 0 );
  184.             m_pdidDevices[i]->SetActionMap( &m_diaf, m_strUserName, 0 );
  185.         }
  186.     }
  187.  
  188.     if( FAILED(hr) )
  189.         return hr;
  190.  
  191.     return S_OK;
  192. }
  193.  
  194.  
  195.  
  196.  
  197. //-----------------------------------------------------------------------------
  198. // Name: Create()
  199. // Desc:
  200. //-----------------------------------------------------------------------------
  201. HRESULT CInputDeviceManager::Create( HWND hWnd, TCHAR* strUserName, 
  202.                                      DIACTIONFORMAT& diaf )
  203. {
  204.     HRESULT hr;
  205.  
  206.     // Store data
  207.     m_hWnd        = hWnd;
  208.     m_strUserName = strUserName;
  209.     
  210.     // Create the base DirectInput object
  211.     hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, 
  212.                               IID_IDirectInput8, (VOID**)&m_pDI, NULL );
  213.     if( FAILED(hr) )
  214.         return hr;
  215.  
  216.     SetActionFormat( diaf, TRUE );
  217.  
  218.     return S_OK;
  219. }
  220.  
  221.  
  222.  
  223.  
  224. //-----------------------------------------------------------------------------
  225. // Name: Create()
  226. // Desc:
  227. //-----------------------------------------------------------------------------
  228. HRESULT CInputDeviceManager::ConfigureDevices( HWND hWnd, IUnknown* pSurface,
  229.                                                VOID* ConfigureDevicesCB,
  230.                                                DWORD dwFlags )
  231. {
  232.     HRESULT hr;
  233.  
  234.     // Initialize all the colors here
  235.     DICOLORSET dics;
  236.     ZeroMemory(&dics, sizeof(DICOLORSET));
  237.     dics.dwSize = sizeof(DICOLORSET);
  238.  
  239.     // Fill in all the params
  240.     DICONFIGUREDEVICESPARAMS dicdp;
  241.     ZeroMemory(&dicdp, sizeof(dicdp));
  242.     dicdp.dwSize = sizeof(dicdp);
  243.     dicdp.dwcFormats     = 1;
  244.     dicdp.lprgFormats    = &m_diaf;
  245.     dicdp.hwnd           = hWnd;
  246.     dicdp.lpUnkDDSTarget = pSurface;
  247.  
  248.     if( m_strUserName )
  249.     {
  250.         dicdp.dwcUsers       = 1;
  251.         dicdp.lptszUserNames = m_strUserName;
  252.     }
  253.  
  254.     // Unacquire the devices so that mouse doesn't control the game while in control panel
  255.     UnacquireDevices();
  256.  
  257.     hr = m_pDI->ConfigureDevices( (LPDICONFIGUREDEVICESCALLBACK)ConfigureDevicesCB, 
  258.                                   &dicdp, dwFlags, NULL );
  259.  
  260.     if( dwFlags & DICD_EDIT )
  261.     {
  262.         // Re-set up the devices
  263.         hr = SetActionFormat( m_diaf, TRUE );
  264.     }
  265.  
  266.     return hr;
  267. }
  268.  
  269.  
  270.  
  271. //-----------------------------------------------------------------------------
  272. // Name: UnacquireDevices()
  273. // Desc:
  274. //-----------------------------------------------------------------------------
  275. VOID CInputDeviceManager::UnacquireDevices()
  276. {
  277.     for( DWORD i=0; i<m_dwNumDevices; i++ )
  278.         m_pdidDevices[i]->Unacquire();
  279. }
  280.  
  281.  
  282.