home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / directx / duel / sfx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-15  |  7.0 KB  |  210 lines

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