Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
6.0 KiB

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