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 "physfs/physfs.hpp"
33 for (int i
= 0 ; i
< MAX_SOUNDS
; i
++)
41 strcpy(levelMusicName
, "");
44 void Audio::setSoundVolume(int soundVolume
)
46 this->soundVolume
= soundVolume
;
48 Mix_Volume(-1, soundVolume
);
51 void Audio::setMusicVolume(int musicVolume
)
53 this->musicVolume
= musicVolume
;
56 Mix_VolumeMusic(musicVolume
);
60 void Audio::registerEngine(Engine
*engine
)
62 this->engine
= engine
;
67 * @param i: the id to load the sound to
68 * @param filename: the file to load (relative to datadir)
71 bool Audio::loadSound(int i
, const char *filename
)
73 if (!engine
->useAudio
)
80 error
<< "Sound index is higher than maximum allowed (requested " << i
<< ", maximum is " << MAX_SOUNDS
<< ")" << std::endl
81 << "Ignoring, but this is an internal error, so report it!" << std::endl
;
87 Mix_FreeChunk(sound
[i
]);
93 SDL_RWops
* buffer
= PhysFS::instance
->RWopsFile(filename
);
94 sound
[i
] = Mix_LoadWAV_RW(buffer
, 1);
98 Exception::SdlMixerError
e(AT
);
99 e
.AddInfo("Mix_GetError()", Mix_GetError());
100 e
.AddInfo("File tried to load", filename
);
104 catch (const Exception::Base
& e
)
106 error
<< e
.GetDesc() << std::endl
;
109 catch (const std::exception
& e
)
111 error
<< "Failed to load '" << filename
<< "'" << std::endl
<< "The error was: " << e
.what() << std::endl
;
116 error
<< "Failed to load '" << filename
<< "'" << std::endl
<< "Unknown error while loading." << std::endl
;
123 bool Audio::loadMusic(const char *filename
)
125 if (!engine
->useAudio
)
134 Mix_FreeMusic(music
);
136 SDL_FreeRW(music_rwops
);
142 music_rwops
= PhysFS::instance
->RWopsFile(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
;
169 strcpy(levelMusicName
, filename
);
174 void Audio::playSound(int snd
, int channel
)
176 if ((!engine
->useAudio
) || (soundVolume
== 0))
184 Mix_Volume(channel
, soundVolume
);
186 Mix_PlayChannel(channel
, sound
[snd
], 0);
189 void Audio::playMusic()
191 if (!engine
->useAudio
)
199 Mix_PlayMusic(music
, -1);
201 Mix_VolumeMusic(musicVolume
);
204 void Audio::playMusicOnce()
206 if (!engine
->useAudio
)
214 Mix_PlayMusic(music
, 0);
216 Mix_VolumeMusic(musicVolume
);
219 bool Audio::loadGameOverMusic()
225 return loadMusic("music/friendDied.mod");
228 bool Audio::reloadLevelMusic()
230 // 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
]);
339 Mix_FreeMusic(music
);
344 if (quickSound
!= NULL
)
345 Mix_FreeChunk(quickSound
);
350 void Audio::destroy()
354 for (int i
= MAX_SOUNDS
- 3 ; i
< MAX_SOUNDS
; i
++)
356 if (sound
[i
] != NULL
)
358 Mix_FreeChunk(sound
[i
]);