SFX.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:7k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File:       sfx.c
  6.  *  Content:    Routines
  7.  *
  8.  *
  9.  ***************************************************************************/
  10. #include "resource.h"
  11. #include "duel.h"
  12. #include "ds3dutil.h"
  13. #include "sfx.h"
  14. /***************************** EXTERNALS ***************************/
  15. extern  HWND ghWndMain;
  16. /******************************* GLOBAL VARIABLES ***************************/
  17. LPDIRECTSOUND                           glpDirectSound;
  18. LPDIRECTSOUND3DLISTENER                      glpDirectSound3DListener;
  19. LPDIRECTSOUNDBUFFER                     glpPrimaryBuffer;
  20. LPWAVEDATA                              gSoundEffect[MAX_SOUNDS];
  21. _TCHAR *szResourceName[MAX_SOUNDS] =
  22. {_T("BFIRE"),  //Bullet Firing
  23.  _T("SBOOM"),  //Ship Exploding
  24.  _T("SENGINE"),//Ship Engine
  25.  _T("SSTART"), //Starting Engine
  26.  _T("SSTOP"),  //Stopping Engine (key 5)
  27.  _T("SBOUNCE"),//Bouncing off a block or window edge
  28.  _T("LBOOM")}; //Block destruction
  29. BOOL gbSoundInitialized = FALSE; //did the sound engine initialize or not?
  30. /***************************** FUNCTION PROTOTYPES **************************/
  31. BOOL Init_Sounds(void);
  32. void Free_Sounds(void);
  33. BOOL Init_Globals(void);
  34. void Free_Globals(void);
  35. /****************************************************************************
  36. FUNCTION:   InitSfx
  37. PURPOSE:    Initializes global variables, then loads the gSoundEffect "objects"
  38.             with sound data.  This should always return TRUE.  If for some reason
  39.             sound can't be initialized, then it will note that fact and still
  40.             return TRUE.
  41. *****************************************************************************/
  42. BOOL InitSfx(void)
  43. {
  44. if (Init_Globals())
  45.     {
  46.     if (Init_Sounds())
  47.         {
  48.         gbSoundInitialized=TRUE;
  49.         return TRUE;
  50.         }
  51.     Free_Globals();
  52.     }
  53. gbSoundInitialized = FALSE;
  54. return TRUE;
  55. };
  56. /****************************************************************************
  57. FUNCTION:   CleanupSfx
  58. PURPOSE:    Frees everything that was allocated by InitSfx, if it was 
  59.             successfully initialized in the beginning.
  60. *****************************************************************************/
  61. void CleanupSfx(void)
  62. {
  63. if (gbSoundInitialized)
  64.     {
  65.     Free_Sounds();
  66.     Free_Globals();
  67.     }
  68. };
  69. /****************************************************************************
  70. FUNCTION:   Init_Globals
  71. PURPOSE:    Initializes the three main global variables. After this is done,
  72.             we should have allocated:
  73.             a. A DirectSound Object
  74.             b. A DirectSound3DListener Object
  75.             c. A Primary Buffer (16 bit, stereo, 22050khz)
  76.             These are all global.
  77. *****************************************************************************/
  78. BOOL Init_Globals(void)
  79. {
  80. PCMWAVEFORMAT pcmwf;
  81. DSBUFFERDESC dsbdesc;
  82. if (DS_OK == DirectSoundCreate(NULL, &glpDirectSound, NULL))
  83.     {
  84.     memset(&pcmwf, 0, sizeof(PCMWAVEFORMAT));
  85.     pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM;
  86.     pcmwf.wf.nChannels = 2;
  87.     pcmwf.wf.nSamplesPerSec = 22050;
  88.     pcmwf.wf.nBlockAlign = 4;
  89.     pcmwf.wf.nAvgBytesPerSec = pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;
  90.     pcmwf.wBitsPerSample = 16;
  91.     memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
  92.     dsbdesc.dwSize = sizeof(DSBUFFERDESC);
  93.     dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRL3D;
  94.     dsbdesc.dwBufferBytes = 0; //dwBufferBytes and lpwfxFormat must be set this way.
  95.     dsbdesc.lpwfxFormat = NULL;
  96.     if (DS_OK== IDirectSound_SetCooperativeLevel(glpDirectSound, ghWndMain,DSSCL_NORMAL))    
  97.         {
  98.         if (DS_OK== IDirectSound_CreateSoundBuffer(glpDirectSound, &dsbdesc, &glpPrimaryBuffer, NULL))
  99.             {
  100.             if (DS_OK==glpPrimaryBuffer->lpVtbl->QueryInterface(glpPrimaryBuffer, &IID_IDirectSound3DListener, (void **)&glpDirectSound3DListener))
  101.                 {
  102.                 if (DS_OK==IDirectSound3DListener_SetPosition(glpDirectSound3DListener, 0.f, 0.f, -1.f, DS3D_DEFERRED))
  103.                     {
  104.                     if (DS_OK==IDirectSound3DListener_SetDopplerFactor(glpDirectSound3DListener, 9.9f ,DS3D_DEFERRED))
  105.                         {
  106.                         if (DS_OK==IDirectSound3DListener_SetRolloffFactor(glpDirectSound3DListener, 0.25f ,DS3D_DEFERRED))
  107.                             {
  108.                             if (DS_OK==IDirectSound3DListener_CommitDeferredSettings(glpDirectSound3DListener))
  109.                                 {
  110.                                 return TRUE;
  111.                                 }
  112.                             }
  113.                         }
  114.                     }
  115.                 IDirectSound3DListener_Release(glpDirectSound3DListener);
  116.                 }
  117.             glpPrimaryBuffer->lpVtbl->Release(glpPrimaryBuffer);
  118.             }        
  119.         }
  120.     IDirectSound_Release(glpDirectSound);
  121.     }
  122. return (FALSE);
  123. };
  124. /*****************************************************************************
  125. FUNCTION:   Free_Globals
  126. PURPOSE:    Frees up all three of the global interfaces that were created by
  127.             Init_Globabls.
  128.     NOTES:      Free_Sounds MUST be called before this.
  129. *****************************************************************************/
  130. void Free_Globals(void)
  131. {
  132. if (glpPrimaryBuffer!=NULL)
  133.     glpPrimaryBuffer->lpVtbl->Release(glpPrimaryBuffer);
  134. if (glpDirectSound3DListener!=NULL)
  135.     IDirectSound3DListener_Release(glpDirectSound3DListener);
  136. if (glpDirectSound!=NULL)
  137.     IDirectSound_Release(glpDirectSound);
  138. };
  139. /****************************************************************************
  140. FUNCTION:   Init_Sounds
  141. PURPOSE:    Loads the gSoundEffect array (of sound objects) with the correct
  142.             data from the WAVE resources, so that only a GetBuffers call needs
  143.             to be made to get access to the sound effects.
  144. NOTES:      If this fails, all sound objects will end up uninitialized.
  145.             Init_Globals MUST be called before this function.
  146. *****************************************************************************/
  147. BOOL Init_Sounds(void)
  148. {
  149. int i;
  150. for (i=0; i<MAX_SOUNDS; i++)
  151.     {
  152.     if (FALSE==WaveInit(&gSoundEffect[i], glpDirectSound, szResourceName[i]))    
  153.         {
  154.         gSoundEffect[i] = NULL;
  155.         for (--i; i>=0; i--)
  156.             {
  157.             WaveFree(gSoundEffect[i]);
  158.             gSoundEffect[i] = NULL;
  159.             }
  160.         ShowError(IDS_DSOUND_LOADWAVES);
  161.         return FALSE;
  162.         }
  163.     }
  164. return TRUE;
  165. };
  166. /*****************************************************************************
  167. FUNCTION:   Free_Sounds
  168. PURPOSE:    Frees up any sound objects that were loaded into memory.
  169. *****************************************************************************/
  170. void Free_Sounds(void)
  171. {int i;
  172. for (i=0; i<MAX_SOUNDS; i++)
  173.     {
  174.     if (gSoundEffect[i]!=NULL)
  175.         {
  176.         WaveFree(gSoundEffect[i]);
  177.         gSoundEffect[i] = NULL;
  178.         }
  179.     }
  180. };