| 1 | #include <stdio.h> |
| 2 | #include <SDL.h> |
| 3 | #include <SDL/SDL_mixer.h> |
| 4 | #include "sound.h" |
| 5 | |
| 6 | Mix_Music *my_music = NULL; |
| 7 | int play_sounds = FALSE; |
| 8 | |
| 9 | int Init_Audio(int allow_sounds) |
| 10 | { |
| 11 | my_music = NULL; |
| 12 | int audio_rate = 22050; |
| 13 | Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */ |
| 14 | int audio_channels = 2; |
| 15 | int audio_buffers = 4096; |
| 16 | |
| 17 | if (allow_sounds) |
| 18 | Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers); |
| 19 | |
| 20 | play_sounds = allow_sounds; |
| 21 | |
| 22 | return TRUE; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | void Sound_Done() |
| 27 | { |
| 28 | if (my_music) |
| 29 | { |
| 30 | Mix_HaltMusic(); |
| 31 | Mix_FreeMusic(my_music); |
| 32 | my_music = NULL; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | |
| 37 | |
| 38 | int Play_Sound(char *sound_file) |
| 39 | { |
| 40 | // maybe sound if disabled? |
| 41 | if (! play_sounds) |
| 42 | return FALSE; |
| 43 | |
| 44 | // something else is playing |
| 45 | if (my_music) |
| 46 | return FALSE; |
| 47 | |
| 48 | // bad pointer? |
| 49 | if (! sound_file) |
| 50 | return FALSE; |
| 51 | |
| 52 | my_music = Mix_LoadMUS(sound_file); |
| 53 | if (my_music) |
| 54 | { |
| 55 | Mix_PlayMusic(my_music, 0); |
| 56 | Mix_HookMusicFinished(Sound_Done); |
| 57 | return TRUE; |
| 58 | } |
| 59 | return FALSE; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | void Clean_Up_Audio() |
| 65 | { |
| 66 | // sound was disabled, nothing to clean |
| 67 | if (! play_sounds) |
| 68 | return; |
| 69 | |
| 70 | Mix_HaltMusic(); |
| 71 | if (my_music) |
| 72 | Mix_FreeMusic(my_music); |
| 73 | my_music = NULL; |
| 74 | Mix_CloseAudio(); |
| 75 | } |
| 76 |