home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / DIRECTX2 / WATCOM / DXSDKWAT.ZIP / SAMPLES / FOXBEAR / FBSOUND.C next >
Encoding:
C/C++ Source or Header  |  1996-05-30  |  6.4 KB  |  289 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1996 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.         #ifdef DEBUG
  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.         #endif
  131.         }
  132.         else
  133.         {
  134.             Msg( "cant load sound effect %s", szSoundEffects[idx]);
  135.         }
  136.     }
  137.  
  138.     /*
  139.      * get the primary buffer and start it playing
  140.      *
  141.      * by playing the primary buffer, DirectSound knows to keep the
  142.      * mixer active, even though we are not making any noise.
  143.      */
  144.  
  145.     ZeroMemory( &dsBD, sizeof(DSBUFFERDESC) );
  146.     dsBD.dwSize = sizeof(dsBD);
  147.     dsBD.dwFlags = DSBCAPS_PRIMARYBUFFER;
  148.  
  149.     if (SUCCEEDED(IDirectSound_CreateSoundBuffer(lpDS, &dsBD, &lpPrimary, NULL)))
  150.     {
  151.         if (!SUCCEEDED(IDirectSoundBuffer_Play(lpPrimary, 0, 0, DSBPLAY_LOOPING)))
  152.         {
  153.             Msg("Unable to play Primary sound buffer");
  154.         }
  155.  
  156.         IDirectSoundBuffer_Release(lpPrimary);
  157.     }
  158.     else
  159.     {
  160.         Msg("Unable to create Primary sound buffer");
  161.     }
  162.  
  163.     return TRUE;
  164.  
  165. } /* InitSound */
  166.  
  167. /*
  168.  * DestroySound
  169.  *
  170.  * Undoes everything that was done in a InitSound call
  171.  */
  172. BOOL DestroySound( void )
  173. {
  174.     DWORD       idxKill;
  175.     
  176.     for( idxKill = 0; idxKill < NUM_SOUND_EFFECTS; idxKill++ )
  177.     {
  178.         SoundDestroyEffect( (EFFECT)idxKill );
  179.     }
  180.  
  181.     DSDisable();
  182.     return TRUE;
  183.  
  184. } /* DestroySound */
  185.  
  186. /*
  187.  * SoundDestroyEffect
  188.  *
  189.  * Frees up resources associated with a sound effect
  190.  */
  191. BOOL SoundDestroyEffect( EFFECT sfx )
  192. {
  193.     if(lpSoundEffects[sfx])
  194.     {
  195.         IDirectSoundBuffer_Release(lpSoundEffects[sfx]);
  196.         lpSoundEffects[sfx] = NULL;
  197.     }
  198.     return TRUE;
  199.  
  200. } /* SoundDestryEffect */
  201.  
  202. /*
  203.  * SoundLoadEffect
  204.  *
  205.  * Initializes a sound effect by loading the WAV file from a resource
  206.  */
  207. BOOL SoundLoadEffect( EFFECT sfx )
  208. {
  209.     if (lpDS && lpSoundEffects[sfx] == NULL && *szSoundEffects[sfx])
  210.     {
  211.         //
  212.         //  use DSLoadSoundBuffer (in ..\misc\dsutil.c) to load
  213.         //  a sound from a resource.
  214.         //
  215.         lpSoundEffects[sfx] = DSLoadSoundBuffer(lpDS, szSoundEffects[sfx]);
  216.     }
  217.  
  218.     return lpSoundEffects[sfx] != NULL;
  219.  
  220. } /* SoundLoadEffect */
  221.  
  222. /*
  223.  * SoundPlayEffect
  224.  *
  225.  * Plays the sound effect specified.  
  226.  * Returns TRUE if succeeded.
  227.  */
  228. BOOL SoundPlayEffect( EFFECT sfx )
  229. {
  230.     HRESULT     dsrval;
  231.     IDirectSoundBuffer *pdsb = lpSoundEffects[sfx];
  232.     
  233.     if( !lpDS || !pdsb )
  234.     {
  235.         return FALSE;
  236.     }
  237.     
  238.     /*
  239.      * Rewind the play cursor to the start of the effect, and play
  240.      */
  241.     IDirectSoundBuffer_SetCurrentPosition(pdsb, 0);
  242.     dsrval = IDirectSoundBuffer_Play(pdsb, 0, 0, 0);
  243.  
  244.     if (dsrval == DSERR_BUFFERLOST)
  245.     {
  246.         Msg("** %s needs restored", szSoundEffects[sfx]);
  247.  
  248.         dsrval = IDirectSoundBuffer_Restore(pdsb);
  249.  
  250.         if (dsrval == DS_OK)
  251.         {
  252.             if (DSReloadSoundBuffer(pdsb, szSoundEffects[sfx]))
  253.             {
  254.                 Msg("** %s has been restored", szSoundEffects[sfx]);
  255.                 IDirectSoundBuffer_SetCurrentPosition(pdsb, 0);
  256.                 dsrval = IDirectSoundBuffer_Play(pdsb, 0, 0, 0);
  257.             }
  258.             else
  259.             {
  260.                 dsrval = E_FAIL;
  261.             }
  262.         }
  263.     }
  264.  
  265.     return (dsrval == DS_OK);
  266.  
  267. } /* SoundPlayEffect */
  268.  
  269. /*
  270.  * SoundStopEffect
  271.  *
  272.  * Stops the sound effect specified.
  273.  * Returns TRUE if succeeded.
  274.  */
  275. BOOL SoundStopEffect( EFFECT sfx )
  276. {
  277.     HRESULT     dsrval;
  278.  
  279.     if( !lpDS || !lpSoundEffects[sfx] )
  280.     {
  281.         return FALSE;
  282.     }
  283.  
  284.     dsrval = IDirectSoundBuffer_Stop(lpSoundEffects[sfx]);
  285.  
  286.     return SUCCEEDED(dsrval);
  287.  
  288. } /* SoundStopEffect */
  289.