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.
25 #include "console/console.hpp"
26 #include "physfs/rwops.hpp"
34 for (int i
= 0 ; i
< MAX_SOUNDS
; i
++)
42 strcpy(levelMusicName
, "");
45 void Audio::setSoundVolume(int soundVolume
)
47 this->soundVolume
= soundVolume
;
49 Mix_Volume(-1, soundVolume
);
52 void Audio::setMusicVolume(int musicVolume
)
54 this->musicVolume
= musicVolume
;
57 Mix_VolumeMusic(musicVolume
);
61 void Audio::registerEngine(Engine
*engine
)
63 this->engine
= engine
;
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
)
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
;
88 Mix_FreeChunk(sound
[i
]);
94 SDL_RWops
* buffer
= PhysFS::RWops(filename
);
95 sound
[i
] = Mix_LoadWAV_RW(buffer
, 1);
99 Exception::SdlMixerError
e(AT
);
100 e
.AddInfo("Mix_GetError()", Mix_GetError());
101 e
.AddInfo("File tried to load", filename
);
105 catch (const Exception::Base
& e
)
107 error
<< e
.GetDesc() << std::endl
;
110 catch (const std::exception
& e
)
112 error
<< "Failed to load '" << filename
<< "'" << std::endl
<< "The error was: " << e
.what() << std::endl
;
117 error
<< "Failed to load '" << filename
<< "'" << std::endl
<< "Unknown error while loading." << std::endl
;
124 bool Audio::loadMusic(const char *filename
, const bool save
)
126 if (!engine
->useAudio
)
134 Mix_FreeMusic(music
);
136 SDL_FreeRW(music_rwops
);
142 music_rwops
= PhysFS::RWops(filename
);
143 music
= Mix_LoadMUS_RW(music_rwops
);
147 Exception::SdlMixerError
e(AT
);
148 e
.AddInfo("Mix_GetError()", Mix_GetError());
149 e
.AddInfo("File tried to load", filename
);
153 catch (const Exception::Base
& e
)
155 error
<< e
.GetDesc() << std::endl
;
158 catch (const std::exception
& e
)
160 error
<< "Failed to load '" << filename
<< "'" << std::endl
<< "The error was: " << e
.what() << std::endl
;
165 error
<< "Failed to load '" << filename
<< "'" << std::endl
<< "Unknown error while loading." << std::endl
;
170 strcpy(levelMusicName
, filename
);
175 void Audio::playSound(int snd
, int channel
)
177 if ((!engine
->useAudio
) || (soundVolume
== 0))
185 Mix_Volume(channel
, soundVolume
);
187 Mix_PlayChannel(channel
, sound
[snd
], 0);
190 void Audio::playMusic()
192 if (!engine
->useAudio
)
200 Mix_PlayMusic(music
, -1);
202 Mix_VolumeMusic(musicVolume
);
205 void Audio::playMusicOnce()
207 if (!engine
->useAudio
)
215 Mix_PlayMusic(music
, 0);
217 Mix_VolumeMusic(musicVolume
);
220 bool Audio::loadGameOverMusic()
226 return loadMusic("music/friendDied.mod", false);
229 bool Audio::reloadLevelMusic()
231 // remove the Game Over music first...
236 Mix_FreeMusic(music
);
240 return loadMusic(levelMusicName
);
243 void Audio::playAmbiance()
245 if ((!engine
->useAudio
) || (soundVolume
== 0))
253 Mix_PlayChannel(CH_AMBIANCE
, sound
[SND_AMBIANCE
], -1);
256 void Audio::stopAmbiance()
258 if ((!engine
->useAudio
) || (soundVolume
== 0))
261 Mix_HaltChannel(CH_AMBIANCE
);
264 int Audio::playMenuSound(int sound
)
266 if ((!engine
->useAudio
) || (soundVolume
== 0))
269 if ((sound
== 0) || (sound
== 3))
273 playSound(SND_HIGHLIGHT
, CH_ANY
);
276 playSound(SND_SELECT
, CH_ANY
);
283 if (!engine
->useAudio
)
286 for (int i
= 0 ; i
< 8 ; i
++)
294 if (!engine
->useAudio
)
302 for (int i
= 0 ; i
< 8 ; i
++)
308 void Audio::stopMusic()
310 if (!engine
->useAudio
)
316 void Audio::fadeMusic()
318 if (!engine
->useAudio
)
321 Mix_FadeOutMusic(3500);
326 for (int i
= 0 ; i
< MAX_SOUNDS
- 3 ; i
++)
328 if (sound
[i
] != NULL
)
330 Mix_FreeChunk(sound
[i
]);
338 Mix_FreeMusic(music
);
343 if (quickSound
!= NULL
)
344 Mix_FreeChunk(quickSound
);
349 void Audio::destroy()
353 for (int i
= MAX_SOUNDS
- 3 ; i
< MAX_SOUNDS
; i
++)
355 if (sound
[i
] != NULL
)
357 Mix_FreeChunk(sound
[i
]);