Removed that logo from this bramch aswell..
[dbw.git] / src / CAudio.cpp
blob8fa9cf0088aca86a9b9f9de927b9975ae72ec495
1 /*
2 Copyright © 2004 Parallel Realities
3 Copyright © 2007-2008 Kővágó Zoltán <DirtY.iCE.hu@gmail.com>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <stdexcept>
24 #include "CAudio.h"
25 #include "console/console.hpp"
26 #include "physfs/rwops.hpp"
28 Audio::Audio()
30 output = 2;
31 useSound = true;
32 useMusic = true;
34 for (int i = 0 ; i < MAX_SOUNDS ; i++)
36 sound[i] = NULL;
39 music = NULL;
40 quickSound = NULL;
42 strcpy(levelMusicName, "");
45 void Audio::setSoundVolume(int soundVolume)
47 this->soundVolume = soundVolume;
48 if (engine->useAudio)
49 Mix_Volume(-1, soundVolume);
52 void Audio::setMusicVolume(int musicVolume)
54 this->musicVolume = musicVolume;
55 if (engine->useAudio)
57 Mix_VolumeMusic(musicVolume);
61 void Audio::registerEngine(Engine *engine)
63 this->engine = engine;
66 /**
67 * Loads a sound file
68 * @param i: the id to load the sound to
69 * @param filename: the file to load (relative to datadir)
72 bool Audio::loadSound(int i, const char *filename)
74 if (!engine->useAudio)
76 return true;
79 if (i >= MAX_SOUNDS)
81 error << "Sound index is higher than maximum allowed (requested " << i << ", maximum is " << MAX_SOUNDS << ")" << std::endl
82 << "Ignoring, but this is an internal error, so report it!" << std::endl;
83 return false;
86 if (sound[i] != NULL)
88 Mix_FreeChunk(sound[i]);
89 sound[i] = NULL;
92 try
94 SDL_RWops * buffer = PhysFS::RWops(filename);
95 sound[i] = Mix_LoadWAV_RW(buffer, 1);
97 if (!sound[i])
99 Exception::SdlMixerError e(AT);
100 e.AddInfo("Mix_GetError()", Mix_GetError());
101 e.AddInfo("File tried to load", filename);
102 throw e;
105 catch (const Exception::Base& e)
107 error << e.GetDesc() << std::endl;
108 return false;
110 catch (const std::exception& e)
112 error << "Failed to load '" << filename << "'" << std::endl << "The error was: " << e.what() << std::endl;
113 return false;
115 catch (...)
117 error << "Failed to load '" << filename << "'" << std::endl << "Unknown error while loading." << std::endl;
118 return false;
121 return true;
124 bool Audio::loadMusic(const char *filename, const bool save)
126 if (!engine->useAudio)
128 return true;
131 if (music != NULL)
133 Mix_HaltMusic();
134 Mix_FreeMusic(music);
135 music = NULL;
136 SDL_FreeRW(music_rwops);
142 music_rwops = PhysFS::RWops(filename);
143 music = Mix_LoadMUS_RW(music_rwops);
145 if (!music)
147 Exception::SdlMixerError e(AT);
148 e.AddInfo("Mix_GetError()", Mix_GetError());
149 e.AddInfo("File tried to load", filename);
150 throw e;
153 catch (const Exception::Base& e)
155 error << e.GetDesc() << std::endl;
156 return false;
158 catch (const std::exception& e)
160 error << "Failed to load '" << filename << "'" << std::endl << "The error was: " << e.what() << std::endl;
161 return false;
163 catch (...)
165 error << "Failed to load '" << filename << "'" << std::endl << "Unknown error while loading." << std::endl;
166 return false;
169 if (save)
170 strcpy(levelMusicName, filename);
172 return true;
175 void Audio::playSound(int snd, int channel)
177 if ((!engine->useAudio) || (soundVolume == 0))
178 return;
180 if (!output)
182 return;
185 Mix_Volume(channel, soundVolume);
187 Mix_PlayChannel(channel, sound[snd], 0);
190 void Audio::playMusic()
192 if (!engine->useAudio)
193 return;
195 if (!output)
197 return;
200 Mix_PlayMusic(music, -1);
202 Mix_VolumeMusic(musicVolume);
205 void Audio::playMusicOnce()
207 if (!engine->useAudio)
208 return;
210 if (!output)
212 return;
215 Mix_PlayMusic(music, 0);
217 Mix_VolumeMusic(musicVolume);
220 bool Audio::loadGameOverMusic()
222 #if DEMO
223 return true;
224 #endif
226 return loadMusic("music/friendDied.mod", false);
229 bool Audio::reloadLevelMusic()
231 // remove the Game Over music first...
233 if (music != NULL)
235 Mix_HaltMusic();
236 Mix_FreeMusic(music);
237 music = NULL;
240 return loadMusic(levelMusicName);
243 void Audio::playAmbiance()
245 if ((!engine->useAudio) || (soundVolume == 0))
246 return;
248 if (!output)
250 return;
253 Mix_PlayChannel(CH_AMBIANCE, sound[SND_AMBIANCE], -1);
256 void Audio::stopAmbiance()
258 if ((!engine->useAudio) || (soundVolume == 0))
259 return;
261 Mix_HaltChannel(CH_AMBIANCE);
264 int Audio::playMenuSound(int sound)
266 if ((!engine->useAudio) || (soundVolume == 0))
267 return sound;
269 if ((sound == 0) || (sound == 3))
270 return sound;
272 if (sound == 1)
273 playSound(SND_HIGHLIGHT, CH_ANY);
275 if (sound == 2)
276 playSound(SND_SELECT, CH_ANY);
278 return sound;
281 void Audio::pause()
283 if (!engine->useAudio)
284 return;
286 for (int i = 0 ; i < 8 ; i++)
287 Mix_Pause(i);
289 Mix_PauseMusic();
292 void Audio::resume()
294 if (!engine->useAudio)
295 return;
297 if (!output)
299 return;
302 for (int i = 0 ; i < 8 ; i++)
303 Mix_Resume(i);
305 Mix_ResumeMusic();
308 void Audio::stopMusic()
310 if (!engine->useAudio)
311 return;
313 Mix_HaltMusic();
316 void Audio::fadeMusic()
318 if (!engine->useAudio)
319 return;
321 Mix_FadeOutMusic(3500);
324 void Audio::free()
326 for (int i = 0 ; i < MAX_SOUNDS - 3 ; i++)
328 if (sound[i] != NULL)
330 Mix_FreeChunk(sound[i]);
331 sound[i] = NULL;
335 if (music != NULL)
337 Mix_HaltMusic();
338 Mix_FreeMusic(music);
341 music = NULL;
343 if (quickSound != NULL)
344 Mix_FreeChunk(quickSound);
346 quickSound = NULL;
349 void Audio::destroy()
351 free();
353 for (int i = MAX_SOUNDS - 3 ; i < MAX_SOUNDS ; i++)
355 if (sound[i] != NULL)
357 Mix_FreeChunk(sound[i]);
358 sound[i] = NULL;