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 / foxbear / fbsound.c < prev    next >
C/C++ Source or Header  |  1997-07-14  |  6KB  |  288 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1997 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File:       fbsound.c
  6.  *  Content:    Game sound effect routines
  7.  *
  8.  ***************************************************************************/
  9. #include "foxbear.h"
  10.     
  11. /*
  12.  * Array of pointers to our sound effects
  13.  */
  14. LPDIRECTSOUND        lpDS;
  15. LPDIRECTSOUNDBUFFER     lpSoundEffects[NUM_SOUND_EFFECTS];
  16.  
  17. char szSoundEffects[NUM_SOUND_EFFECTS][MAX_PATH] =
  18. {
  19.     "STOP",
  20.     "THROW",
  21.     "JUMP",
  22.     "STUNNED",
  23.     "STRIKE02",
  24.     "MISS02"
  25. };
  26.  
  27. /*
  28.  * DSEnable
  29.  *
  30.  * Figures out whether or not to use DirectSound, based on an entry
  31.  * in WIN.INI.  Sets a module-level flag and goes about creating the
  32.  * DirectSound object if necessary.  Returns TRUE if successful.
  33.  */
  34. BOOL DSEnable( HWND hwnd )
  35. {
  36.     HRESULT        dsrval;
  37.     BOOL                bUseDSound;
  38.  
  39.     bUseDSound = GetProfileInt("FoxBear", "use_dsound", bWantSound);
  40.  
  41.     if (!bUseDSound)
  42.     {
  43.         lpDS = NULL;
  44.         return TRUE;
  45.     }
  46.  
  47.     if (lpDS != NULL)
  48.     {
  49.         Msg( "DSEnable, already enabled" );
  50.         return TRUE;
  51.     }
  52.  
  53.     dsrval = DirectSoundCreate(NULL, &lpDS, NULL);
  54.  
  55.     if (dsrval != DS_OK)
  56.     {
  57.         Msg("DirectSoundCreate FAILED");
  58.         return FALSE;
  59.     }
  60.  
  61.  
  62.     dsrval = IDirectSound_SetCooperativeLevel(lpDS, hwnd, DSSCL_NORMAL);
  63.  
  64.     if (dsrval != DS_OK)
  65.     {
  66.         DSDisable();
  67.         Msg("SetCooperativeLevel FAILED");
  68.         return FALSE;
  69.     }
  70.  
  71.     return TRUE;
  72.  
  73. } /* DSEnable */
  74.  
  75.  
  76. /*
  77.  * DSDisable
  78.  *
  79.  * Turn off DirectSound
  80.  */
  81. BOOL DSDisable( void )
  82. {
  83.     if (lpDS == NULL)
  84.     {
  85.     return TRUE;
  86.     }
  87.  
  88.     IDirectSound_Release(lpDS);
  89.     lpDS = NULL;
  90.  
  91.     return TRUE;
  92.  
  93. } /* DSDisable */
  94.  
  95. /*
  96.  * InitSound
  97.  *
  98.  * Sets up the DirectSound object and loads all sounds into secondary
  99.  * DirectSound buffers.  Returns FALSE on error, or TRUE if successful
  100.  */
  101. BOOL InitSound( HWND hwndOwner )
  102. {
  103.     int idx;
  104.     DSBUFFERDESC dsBD;
  105.     IDirectSoundBuffer *lpPrimary;
  106.  
  107.     DSEnable(hwndOwner);
  108.  
  109.     if (lpDS == NULL)
  110.         return TRUE;
  111.  
  112.     /*
  113.      * Load all sounds -- any that can't load for some reason will have NULL
  114.      * pointers instead of valid SOUNDEFFECT data, and we will know not to
  115.      * play them later on.
  116.      */
  117.     for( idx = 0; idx < NUM_SOUND_EFFECTS; idx++ )
  118.     {
  119.         if (SoundLoadEffect((EFFECT)idx))
  120.         {
  121.             DSBCAPS  caps;
  122.  
  123.             caps.dwSize = sizeof(caps);
  124.             IDirectSoundBuffer_GetCaps(lpSoundEffects[idx], &caps);
  125.  
  126.             if (caps.dwFlags & DSBCAPS_LOCHARDWARE)
  127.                 Msg( "Sound effect %s in hardware", szSoundEffects[idx]);
  128.             else
  129.                 Msg( "Sound effect %s in software", szSoundEffects[idx]);
  130.         }
  131.         else
  132.         {
  133.             Msg( "cant load sound effect %s", szSoundEffects[idx]);
  134.         }
  135.     }
  136.  
  137.     /*
  138.      * get the primary buffer and start it playing
  139.      *
  140.      * by playing the primary buffer, DirectSound knows to keep the
  141.      * mixer active, even though we are not making any noise.
  142.      */
  143.  
  144.     ZeroMemory( &dsBD, sizeof(DSBUFFERDESC) );
  145.     dsBD.dwSize = sizeof(dsBD);
  146.     dsBD.dwFlags = DSBCAPS_PRIMARYBUFFER;
  147.  
  148.     if (SUCCEEDED(IDirectSound_CreateSoundBuffer(lpDS, &dsBD, &lpPrimary, NULL)))
  149.     {
  150.         if (!SUCCEEDED(IDirectSoundBuffer_Play(lpPrimary, 0, 0, DSBPLAY_LOOPING)))
  151.         {
  152.             Msg("Unable to play Primary sound buffer");
  153.         }
  154.  
  155.         IDirectSoundBuffer_Release(lpPrimary);
  156.     }
  157.     else
  158.     {
  159.         Msg("Unable to create Primary sound buffer");
  160.     }
  161.  
  162.     return TRUE;
  163.  
  164. } /* InitSound */
  165.  
  166. /*
  167.  * DestroySound
  168.  *
  169.  * Undoes everything that was done in a InitSound call
  170.  */
  171. BOOL DestroySound( void )
  172. {
  173.     DWORD    idxKill;
  174.     
  175.     for( idxKill = 0; idxKill < NUM_SOUND_EFFECTS; idxKill++ )
  176.     {
  177.     SoundDestroyEffect( (EFFECT)idxKill );
  178.     }
  179.  
  180.     DSDisable();
  181.     return TRUE;
  182.  
  183. } /* DestroySound */
  184.  
  185. /*
  186.  * SoundDestroyEffect
  187.  *
  188.  * Frees up resources associated with a sound effect
  189.  */
  190. BOOL SoundDestroyEffect( EFFECT sfx )
  191. {
  192.     if(lpSoundEffects[sfx])
  193.     {
  194.         IDirectSoundBuffer_Release(lpSoundEffects[sfx]);
  195.         lpSoundEffects[sfx] = NULL;
  196.     }
  197.     return TRUE;
  198.  
  199. } /* SoundDestryEffect */
  200.  
  201. /*
  202.  * SoundLoadEffect
  203.  *
  204.  * Initializes a sound effect by loading the WAV file from a resource
  205.  */
  206. BOOL SoundLoadEffect( EFFECT sfx )
  207. {
  208.     if (lpDS && lpSoundEffects[sfx] == NULL && *szSoundEffects[sfx])
  209.     {
  210.         //
  211.         //  use DSLoadSoundBuffer (in ..\misc\dsutil.c) to load
  212.         //  a sound from a resource.
  213.         //
  214.         lpSoundEffects[sfx] = DSLoadSoundBuffer(lpDS, szSoundEffects[sfx]);
  215.     }
  216.  
  217.     return lpSoundEffects[sfx] != NULL;
  218.  
  219. } /* SoundLoadEffect */
  220.  
  221. /*
  222.  * SoundPlayEffect
  223.  *
  224.  * Plays the sound effect specified.  
  225.  * Returns TRUE if succeeded.
  226.  */
  227. BOOL SoundPlayEffect( EFFECT sfx )
  228. {
  229.     HRESULT     dsrval;
  230.     IDirectSoundBuffer *pdsb = lpSoundEffects[sfx];
  231.     
  232.     if( !lpDS || !pdsb )
  233.     {
  234.     return FALSE;
  235.     }
  236.     
  237.     /*
  238.      * Rewind the play cursor to the start of the effect, and play
  239.      */
  240.     IDirectSoundBuffer_SetCurrentPosition(pdsb, 0);
  241.     dsrval = IDirectSoundBuffer_Play(pdsb, 0, 0, 0);
  242.  
  243.     if (dsrval == DSERR_BUFFERLOST)
  244.     {
  245.         Msg("** %s needs restored", szSoundEffects[sfx]);
  246.  
  247.         dsrval = IDirectSoundBuffer_Restore(pdsb);
  248.  
  249.         if (dsrval == DS_OK)
  250.         {
  251.             if (DSReloadSoundBuffer(pdsb, szSoundEffects[sfx]))
  252.             {
  253.                 Msg("** %s has been restored", szSoundEffects[sfx]);
  254.                 IDirectSoundBuffer_SetCurrentPosition(pdsb, 0);
  255.                 dsrval = IDirectSoundBuffer_Play(pdsb, 0, 0, 0);
  256.             }
  257.             else
  258.             {
  259.                 dsrval = E_FAIL;
  260.             }
  261.         }
  262.     }
  263.  
  264.     return (dsrval == DS_OK);
  265.  
  266. } /* SoundPlayEffect */
  267.  
  268. /*
  269.  * SoundStopEffect
  270.  *
  271.  * Stops the sound effect specified.
  272.  * Returns TRUE if succeeded.
  273.  */
  274. BOOL SoundStopEffect( EFFECT sfx )
  275. {
  276.     HRESULT    dsrval;
  277.  
  278.     if( !lpDS || !lpSoundEffects[sfx] )
  279.     {
  280.     return FALSE;
  281.     }
  282.  
  283.     dsrval = IDirectSoundBuffer_Stop(lpSoundEffects[sfx]);
  284.  
  285.     return SUCCEEDED(dsrval);
  286.  
  287. } /* SoundStopEffect */
  288.