home *** CD-ROM | disk | FTP | other *** search
/ WarCraft 2000 - Nuclear Epidemic / W2000.nrg / SOURCE.War2000 / Cdirsnd.cpp < prev    next >
C/C++ Source or Header  |  1998-09-16  |  8KB  |  254 lines

  1. ///////////////////////////////////////////////////////////
  2. // CDIRSND.CPP -- Implementation file for the CDirSound
  3. //                class.
  4. ///////////////////////////////////////////////////////////
  5.  
  6. //#include <stdafx.h>
  7. #include "cdirsnd.h"
  8. ///////////////////////////////////////////////////////////
  9. // CDirSound::CDirSound()
  10. //
  11. // This is the class's constructor.
  12. ///////////////////////////////////////////////////////////
  13. void CDirSound::CreateDirSound(HWND hWnd)
  14. {
  15.     // Initialize class data members.
  16.     m_hWindow = hWnd;
  17.     m_pDirectSoundObj = NULL;
  18.     m_currentBufferNum = 0;
  19.  
  20.     for (UINT x=0; x<MAXSND1; ++x)
  21.     {
  22.         m_bufferPointers[x] = NULL;
  23.         m_bufferSizes[x] = 0;
  24.     }
  25.  
  26.     // Create the main DirectSound object.
  27.     HRESULT result =
  28.         DirectSoundCreate(NULL, &m_pDirectSoundObj, NULL);
  29.     if (result == DS_OK)
  30.     {
  31.         // Set the priority level.
  32.         result = m_pDirectSoundObj->
  33.             SetCooperativeLevel(m_hWindow, DSSCL_NORMAL);
  34.         if (result != DS_OK)
  35.             m_pDirectSoundObj = NULL;
  36.     }
  37.  
  38. }
  39.  
  40. ///////////////////////////////////////////////////////////
  41. // CDirSound::~CDirSound()
  42. //
  43. // This is the class's destructor.
  44. ///////////////////////////////////////////////////////////
  45. CDirSound::~CDirSound()
  46. {
  47.     if (m_pDirectSoundObj != NULL)
  48.         ReleaseAll();
  49. }
  50.  
  51. ///////////////////////////////////////////////////////////
  52. // CDirSound::ReleaseAll()
  53. //
  54. // This member function releases all the DirectSound
  55. // objects created in the class.
  56. ///////////////////////////////////////////////////////////
  57. void CDirSound::ReleaseAll()
  58. {
  59.     // Release all sound buffers.
  60.     for (UINT x=1; x<=m_currentBufferNum; ++x)
  61.         m_bufferPointers[x]->Release();
  62.  
  63.     // Release the DirectSound object.
  64.     if (m_pDirectSoundObj != NULL)
  65.         m_pDirectSoundObj->Release();
  66. }
  67.  
  68. ///////////////////////////////////////////////////////////
  69. // CDirSound::CreateSoundBuffer()
  70. //
  71. // This member function creates secondary sound buffers.
  72. // The class can accommodate up to MAXSND such buffers.
  73. ///////////////////////////////////////////////////////////
  74. UINT CDirSound::CreateSoundBuffer(CWave* pWave)
  75. {
  76.     DSBUFFERDESC dsBufferDesc;
  77.     HRESULT hResult;
  78.  
  79.     // Make sure there's room for another buffer.
  80.     if (m_currentBufferNum == MAXSND)
  81.         return 0;
  82.  
  83.     // Calculate the next available buffer number.
  84.     ++m_currentBufferNum;
  85.  
  86.     // Initialize the DSBUFFERDESC structure.
  87.     LPWAVEFORMATEX pWaveFormat = pWave->GetWaveFormatPtr();
  88.     memset(&dsBufferDesc, 0, sizeof(DSBUFFERDESC));
  89.     dsBufferDesc.dwSize = sizeof(DSBUFFERDESC);
  90.     dsBufferDesc.dwFlags = DSBCAPS_CTRLDEFAULT;
  91.     dsBufferDesc.dwBufferBytes = pWave->GetWaveSize();
  92.     dsBufferDesc.lpwfxFormat = (LPWAVEFORMATEX) pWaveFormat;
  93.  
  94.     // Create the secondary sound buffer.
  95.     hResult = m_pDirectSoundObj->CreateSoundBuffer(&dsBufferDesc,
  96.         &m_bufferPointers[m_currentBufferNum], NULL);
  97.     if (hResult != DS_OK)
  98.         return 0;
  99.  
  100.     // Save the buffer size.
  101.     m_bufferSizes[m_currentBufferNum] = dsBufferDesc.dwBufferBytes;
  102.  
  103.     return m_currentBufferNum;
  104. }
  105.  
  106. ///////////////////////////////////////////////////////////
  107. // CDirSound::CopyWaveToBuffer()
  108. //
  109. // This function copies wave data to a DirectSound buffer.
  110. ///////////////////////////////////////////////////////////
  111. BOOL CDirSound::CopyWaveToBuffer(CWave* pWave, UINT bufferNum)
  112. {
  113.     LPVOID pSoundBlock1;
  114.     LPVOID pSoundBlock2;
  115.     DWORD bytesSoundBlock1;
  116.     DWORD bytesSoundBlock2;
  117.     HRESULT result;
  118.  
  119.     // Check for a valid buffer number.
  120.     if ((bufferNum > m_currentBufferNum) || (bufferNum == 0))
  121.         return FALSE;
  122.  
  123.     // Get a pointer to the requested buffer.
  124.     LPDIRECTSOUNDBUFFER pSoundBuffer = 
  125.         m_bufferPointers[bufferNum];
  126.  
  127.     // Lock the buffer.
  128.     result = pSoundBuffer->Lock(0, m_bufferSizes[bufferNum],
  129.         &pSoundBlock1, &bytesSoundBlock1,
  130.         &pSoundBlock2, &bytesSoundBlock2, 0);
  131.     if (result != DS_OK)
  132.         return FALSE;
  133.  
  134.     // Copy the data into the buffer.
  135.     char* pWaveData = pWave->GetWaveDataPtr();
  136.     DWORD waveSize = pWave->GetWaveSize();
  137.     memcpy((void*)pSoundBlock1, pWaveData, waveSize);
  138.  
  139.     // Unlock the buffer.
  140.     pSoundBuffer->Unlock(pSoundBlock1, bytesSoundBlock1,
  141.             pSoundBlock2, bytesSoundBlock2);
  142.  
  143.     return TRUE;
  144. }
  145.  
  146. ///////////////////////////////////////////////////////////
  147. // CDirSound::DirectSoundOK()
  148. //
  149. // This member function returns TRUE if the DirectSound
  150. // object was created and initialized okay. Otherwise, it
  151. // returns FALSE.
  152. ///////////////////////////////////////////////////////////
  153. BOOL CDirSound::DirectSoundOK()
  154. {
  155.     if (m_pDirectSoundObj == NULL)
  156.         return FALSE;
  157.  
  158.     return TRUE;
  159. }
  160.  
  161. ///////////////////////////////////////////////////////////
  162. // CDirSound::PlaySound()
  163. //
  164. // This member function plays the sound stored in the given
  165. // buffer.
  166. ///////////////////////////////////////////////////////////
  167. BOOL CDirSound::PlaySound(UINT bufferNum)
  168. {
  169.     HRESULT result;
  170.  
  171.     // Check for a valid buffer number.
  172.     if ((bufferNum > m_currentBufferNum) || (bufferNum == 0))
  173.         return FALSE;
  174.  
  175.     // Get a pointer to the requested buffer.
  176.     LPDIRECTSOUNDBUFFER pSoundBuffer = m_bufferPointers[bufferNum];
  177.  
  178.     // Make sure the buffer is set to play from the beginning.
  179.     result = pSoundBuffer->SetCurrentPosition(0);
  180.     if (result != DS_OK)
  181.         return FALSE;
  182.  
  183.     // Play the sound.
  184.     result = pSoundBuffer->Play(0, 0, 0);
  185.     if (result != DS_OK)
  186.         return FALSE;
  187.  
  188.     return TRUE;
  189. }
  190. void CDirSound::SetVolume(UINT bufferNum,int vol){
  191.     // Check for a valid buffer number.
  192.     if ((bufferNum > m_currentBufferNum) || (bufferNum == 0))
  193.         return;
  194.  
  195.     // Get a pointer to the requested buffer.
  196.     LPDIRECTSOUNDBUFFER pSoundBuffer = m_bufferPointers[bufferNum];
  197.  
  198.     // Make sure the buffer is set to play from the beginning.
  199.     pSoundBuffer->SetVolume(vol);
  200. };
  201. void CDirSound::SetPan(UINT bufferNum,int pan){
  202.     // Check for a valid buffer number.
  203.     if ((bufferNum > m_currentBufferNum) || (bufferNum == 0))
  204.         return;
  205.  
  206.     // Get a pointer to the requested buffer.
  207.     LPDIRECTSOUNDBUFFER pSoundBuffer = m_bufferPointers[bufferNum];
  208.  
  209.     // Make sure the buffer is set to play from the beginning.
  210.     pSoundBuffer->SetPan(pan);
  211. };
  212. ///////////////////////////////////////////////////////////
  213. // CDirSound::StopSound()
  214. //
  215. // This member function stops the sound stored in the given
  216. // buffer.
  217. ///////////////////////////////////////////////////////////
  218. BOOL CDirSound::StopSound(UINT bufferNum)
  219. {
  220.     HRESULT result;
  221.  
  222.     // Check for a valid buffer number.
  223.     if ((bufferNum > m_currentBufferNum) || (bufferNum == 0))
  224.         return FALSE;
  225.  
  226.     // Get a pointer to the requested buffer.
  227.     LPDIRECTSOUNDBUFFER pSoundBuffer = m_bufferPointers[bufferNum];
  228.  
  229.     // Make sure the buffer is set to play from the beginning.
  230.     result = pSoundBuffer->Stop();
  231.     if (result != DS_OK)
  232.         return FALSE;
  233.  
  234.     return TRUE;
  235. }
  236. int CDirSound::GetPos(UINT bufferNum)
  237. {
  238.     HRESULT result;
  239.  
  240.     // Check for a valid buffer number.
  241.     if ((bufferNum > m_currentBufferNum) || (bufferNum == 0))
  242.         return 0;
  243.  
  244.     // Get a pointer to the requested buffer.
  245.     LPDIRECTSOUNDBUFFER pSoundBuffer = m_bufferPointers[bufferNum];
  246.  
  247.     // Make sure the buffer is set to play from the beginning.
  248.     DWORD pos;
  249.     result = pSoundBuffer->GetCurrentPosition(&pos,NULL);
  250.     return pos;
  251.  
  252. }
  253.  
  254.