2 Copyright (C) 2004 Parallel Realities
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 for (int i = 0 ; i < MAX_SOUNDS ; i++)
37 levelMusicName[0] = 0;
40 void Audio::setSoundVolume(int soundVolume)
42 this->soundVolume = soundVolume;
44 Mix_Volume(-1, soundVolume);
47 void Audio::setMusicVolume(int musicVolume)
49 this->musicVolume = musicVolume;
52 Mix_VolumeMusic(musicVolume);
56 void Audio::registerEngine(Engine *engine)
58 this->engine = engine;
61 bool Audio::loadSound(int i, const char *filename)
63 if (!engine->useAudio)
70 printf("ERROR: SOUND INDEX IS HIGHER THAN MAXIMUM ALLOWED %d >= %d\n", i, MAX_SOUNDS);
76 Mix_FreeChunk(sound[i]);
81 engine->unpack(filename, PAK_SOUND);
82 sound[i] = Mix_LoadWAV_RW(engine->sdlrw, 1);
84 sound[i] = Mix_LoadWAV(filename);
89 debug(("WARNING - Failed to load %s\n", filename));
96 bool Audio::loadMusic(const char *filename)
98 char tempPath[PATH_MAX];
100 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
102 if (!engine->useAudio)
109 SDL_Delay(250); // wait a bit, just to be sure!
115 Mix_FreeMusic(music);
120 engine->unpack(filename, PAK_MUSIC);
121 music = Mix_LoadMUS(tempPath);
123 music = Mix_LoadMUS(filename);
128 debug(("WARNING - Failed to load %s\n", filename));
132 strncpy(levelMusicName, filename, sizeof levelMusicName);
137 void Audio::playSound(int snd, int channel)
139 if ((!engine->useAudio) || (soundVolume == 0))
147 Mix_Volume(channel, soundVolume);
149 Mix_PlayChannel(channel, sound[snd], 0);
152 void Audio::playMusic()
154 if (!engine->useAudio)
162 Mix_PlayMusic(music, -1);
164 Mix_VolumeMusic(musicVolume);
167 void Audio::playMusicOnce()
169 if (!engine->useAudio)
177 Mix_PlayMusic(music, 0);
179 Mix_VolumeMusic(musicVolume);
182 bool Audio::loadGameOverMusic()
184 char tempPath[PATH_MAX];
186 snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
188 if (!engine->useAudio)
194 SDL_Delay(250); // wait a bit, just to be sure!
200 Mix_FreeMusic(music);
205 engine->unpack("music/friendDied.mod", PAK_MUSIC);
206 music = Mix_LoadMUS(tempPath);
208 music = Mix_LoadMUS("music/friendDied.mod");
219 bool Audio::reloadLevelMusic()
221 // remove the Game Over music first...
227 Mix_FreeMusic(music);
231 return loadMusic(levelMusicName);
234 void Audio::playAmbiance()
236 if ((!engine->useAudio) || (soundVolume == 0))
246 Mix_PlayChannel(CH_AMBIANCE, sound[SND_AMBIANCE], -1);
249 void Audio::stopAmbiance()
251 if ((!engine->useAudio) || (soundVolume == 0))
254 Mix_HaltChannel(CH_AMBIANCE);
257 int Audio::playMenuSound(int sound)
259 if ((!engine->useAudio) || (soundVolume == 0))
262 if ((sound == 0) || (sound == 3))
266 playSound(SND_HIGHLIGHT, CH_ANY);
269 playSound(SND_SELECT, CH_ANY);
276 if (!engine->useAudio)
279 for (int i = 0 ; i < 8 ; i++)
287 if (!engine->useAudio)
295 for (int i = 0 ; i < 8 ; i++)
301 void Audio::stopMusic()
303 if (!engine->useAudio)
309 void Audio::fadeMusic()
311 if (!engine->useAudio)
314 Mix_FadeOutMusic(3500);
319 for (int i = 0 ; i < MAX_SOUNDS - 3 ; i++)
321 if (sound[i] != NULL)
323 Mix_FreeChunk(sound[i]);
332 Mix_FreeMusic(music);
337 if (quickSound != NULL)
338 Mix_FreeChunk(quickSound);
343 void Audio::destroy()
347 for (int i = MAX_SOUNDS - 3 ; i < MAX_SOUNDS ; i++)
349 if (sound[i] != NULL)
351 Mix_FreeChunk(sound[i]);