cSoundInterface.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:4k
源码类别:

游戏

开发平台:

Visual C++

  1. // CMAIN LIB - APPLICATION AND DIRECT WRAPPER
  2. //
  3. // Written by Mauricio Teichmann Ritter
  4. //
  5. // Copyright (C) 2002, Brazil. All rights reserved.
  6. // 
  7. //
  8. // cSoundInterface.cpp: implementation of the cSoundInterface class.
  9. //
  10. //////////////////////////////////////////////////////////////////////
  11. #include "stdafx.h"
  12. #include "cSoundInterface.h"
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. LPDIRECTSOUND8 cSoundInterface::m_pDS = NULL;
  17. LPDIRECTSOUNDBUFFER cSoundInterface::m_pDSBPrimary = NULL;
  18. cSoundInterface::cSoundInterface()
  19. {
  20. }
  21. cSoundInterface::~cSoundInterface()
  22. {
  23. }
  24. HRESULT cSoundInterface::Initialize( HWND  hWnd, 
  25.                                    DWORD dwCoopLevel, 
  26.                                    DWORD dwPrimaryChannels, 
  27.                                    DWORD dwPrimaryFreq, 
  28.                                    DWORD dwPrimaryBitRate)
  29. {
  30.     HRESULT             hr;
  31.     if( m_pDS )
  32. {
  33. m_pDS->Release();
  34. m_pDS = NULL;
  35. }
  36.     // Create IDirectSound using the primary sound device
  37.     if( FAILED( hr = DirectSoundCreate8( NULL, &m_pDS, NULL ) ) )
  38.         return DXTRACE_ERR( TEXT("DirectSoundCreate8"), hr );
  39.     // Set DirectSound coop level 
  40.     if( FAILED( hr = m_pDS->SetCooperativeLevel( hWnd, dwCoopLevel ) ) )
  41.         return DXTRACE_ERR( TEXT("SetCooperativeLevel"), hr );
  42.     
  43.     // Set primary buffer format
  44.     if( FAILED( hr = SetPrimaryBufferFormat( dwPrimaryChannels, dwPrimaryFreq, dwPrimaryBitRate ) ) )
  45.         return DXTRACE_ERR( TEXT("SetPrimaryBufferFormat"), hr );
  46.     return S_OK;
  47. }
  48. HRESULT cSoundInterface::SetPrimaryBufferFormat( DWORD dwPrimaryChannels, 
  49.                                                DWORD dwPrimaryFreq, 
  50.                                                DWORD dwPrimaryBitRate )
  51. {
  52.     HRESULT             hr;
  53.    
  54.    if( m_pDS == NULL )
  55.         return CO_E_NOTINITIALIZED;
  56.     // Get the primary buffer 
  57.     DSBUFFERDESC dsbd;
  58.     ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
  59.     dsbd.dwSize        = sizeof(DSBUFFERDESC);
  60.     dsbd.dwFlags       = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRL3D;
  61.     dsbd.dwBufferBytes = 0;
  62.     dsbd.lpwfxFormat   = NULL;
  63.        
  64.     if( FAILED( hr = m_pDS->CreateSoundBuffer( &dsbd, &m_pDSBPrimary, NULL ) ) )
  65.         return DXTRACE_ERR( TEXT("CreateSoundBuffer"), hr );
  66.     WAVEFORMATEX wfx;
  67.     ZeroMemory( &wfx, sizeof(WAVEFORMATEX) ); 
  68.     wfx.wFormatTag      = WAVE_FORMAT_PCM; 
  69.     wfx.nChannels       = (WORD) dwPrimaryChannels; 
  70.     wfx.nSamplesPerSec  = dwPrimaryFreq; 
  71.     wfx.wBitsPerSample  = (WORD) dwPrimaryBitRate; 
  72.     wfx.nBlockAlign     = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels);
  73.     wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
  74.     if( FAILED( hr = m_pDSBPrimary->SetFormat(&wfx) ) )
  75.         return DXTRACE_ERR( TEXT("SetFormat"), hr );
  76.     return S_OK;
  77. }
  78. LPDIRECTSOUND8 cSoundInterface::GetDirectSound()
  79. {
  80. return m_pDS;
  81. }
  82. void cSoundInterface::Destroy()
  83. {
  84.     if( m_pDSBPrimary )
  85. {
  86. m_pDSBPrimary->Release();
  87. m_pDSBPrimary = NULL;
  88. }
  89. if(m_pDS)
  90. {
  91. m_pDS->Release();
  92. m_pDS = NULL;
  93. }
  94. }
  95. void cSoundInterface::SetListernerPosition(float fX, float fY, float fZ)
  96. {
  97. IDirectSound3DListener8 *pListener=NULL;
  98. m_pDSBPrimary->QueryInterface(IID_IDirectSound3DListener, (void**) &pListener);
  99. if(pListener)
  100. {
  101. pListener->SetPosition(fX, fY, fZ, DS3D_IMMEDIATE);
  102. pListener->SetOrientation(fX, fY, fZ + 1.0f, fX, fY + 1.0f, fZ, DS3D_IMMEDIATE );
  103. }
  104. else
  105. DXTRACE_MSG("NO LISTENER INTERFACE");
  106. }