]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CAudio.cpp
Use UNIX line endings everywhere.
[quix0rs-blobwars.git] / src / CAudio.cpp
1 /*
2 Copyright (C) 2004 Parallel Realities
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20
21 #include "headers.h"
22
23 Audio::Audio()
24 {
25         output = 2;
26         useSound = true;
27         useMusic = true;
28
29         for (int i = 0 ; i < MAX_SOUNDS ; i++)
30         {
31                 sound[i] = NULL;
32         }
33
34         music = NULL;
35         quickSound = NULL;
36         
37         levelMusicName[0] = 0;
38 }
39
40 void Audio::setSoundVolume(int soundVolume)
41 {
42         this->soundVolume = soundVolume;
43         if (engine->useAudio)
44                 Mix_Volume(-1, soundVolume);
45 }
46
47 void Audio::setMusicVolume(int musicVolume)
48 {
49         this->musicVolume = musicVolume;
50         if (engine->useAudio)
51         {
52                 Mix_VolumeMusic(musicVolume);
53         }
54 }
55
56 void Audio::registerEngine(Engine *engine)
57 {
58         this->engine = engine;
59 }
60
61 bool Audio::loadSound(int i, const char *filename)
62 {
63         if (!engine->useAudio)
64         {
65                 return true;
66         }
67                 
68         if (i >= MAX_SOUNDS)
69         {
70                 printf("ERROR: SOUND INDEX IS HIGHER THAN MAXIMUM ALLOWED %d >= %d\n", i, MAX_SOUNDS);
71                 exit(1);
72         }
73
74         if (sound[i] != NULL)
75         {
76                 Mix_FreeChunk(sound[i]);
77                 sound[i] = NULL;
78         }
79
80         #if USEPAK
81                 engine->unpack(filename, PAK_SOUND);
82                 sound[i] = Mix_LoadWAV_RW(engine->sdlrw, 1);
83         #else
84                 sound[i] = Mix_LoadWAV(filename);
85         #endif
86
87         if (!sound[i])
88         {
89                 debug(("WARNING - Failed to load %s\n", filename));
90                 return false;
91         }
92         
93         return true;
94 }
95
96 bool Audio::loadMusic(const char *filename)
97 {
98         char tempPath[PATH_MAX];
99         
100         snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
101         
102         if (!engine->useAudio)
103         {
104                 return true;
105         }
106
107         remove(tempPath);
108         
109         SDL_Delay(250); // wait a bit, just to be sure!
110
111         if (music != NULL)
112         {
113                 Mix_HaltMusic();
114                 SDL_Delay(5);
115                 Mix_FreeMusic(music);
116                 music = NULL;
117         }
118
119         #if USEPAK
120                 engine->unpack(filename, PAK_MUSIC);
121                 music = Mix_LoadMUS(tempPath);
122         #else
123                 music = Mix_LoadMUS(filename);
124         #endif
125
126         if (!music)
127         {
128                 debug(("WARNING - Failed to load %s\n", filename));
129                 return false;
130         }
131                 
132         strncpy(levelMusicName, filename, sizeof levelMusicName);
133
134         return true;
135 }
136
137 void Audio::playSound(int snd, int channel)
138 {
139         if ((!engine->useAudio) || (soundVolume == 0))
140                 return;
141         
142         if (!output)
143         {
144                 return;
145         }
146
147         Mix_Volume(channel, soundVolume);
148
149         Mix_PlayChannel(channel, sound[snd], 0);
150 }
151
152 void Audio::playMusic()
153 {
154         if (!engine->useAudio)
155                 return;
156         
157         if (!output)
158         {
159                 return;
160         }
161
162         Mix_PlayMusic(music, -1);
163
164         Mix_VolumeMusic(musicVolume);
165 }
166
167 void Audio::playMusicOnce()
168 {
169         if (!engine->useAudio)
170                 return;
171         
172         if (!output)
173         {
174                 return;
175         }
176
177         Mix_PlayMusic(music, 0);
178
179         Mix_VolumeMusic(musicVolume);
180 }
181
182 bool Audio::loadGameOverMusic()
183 {
184         char tempPath[PATH_MAX];
185         
186         snprintf(tempPath, sizeof tempPath, "%smusic.mod", engine->userHomeDirectory);
187         
188         if (!engine->useAudio)
189         {
190                 return true;
191         }
192
193         remove(tempPath);
194         SDL_Delay(250); // wait a bit, just to be sure!
195
196         if (music != NULL)
197         {
198                 Mix_HaltMusic();
199                 SDL_Delay(5);
200                 Mix_FreeMusic(music);
201                 music = NULL;
202         }
203
204         #if USEPAK
205                 engine->unpack("music/friendDied.mod", PAK_MUSIC);
206                 music = Mix_LoadMUS(tempPath);
207         #else
208                 music = Mix_LoadMUS("music/friendDied.mod");
209         #endif
210
211         if (!music)
212         {
213                 return false;
214         }
215
216         return true;
217 }
218
219 bool Audio::reloadLevelMusic()
220 {
221         // remove the Game Over music first...
222
223         if (music != NULL)
224         {
225                 Mix_HaltMusic();
226                 SDL_Delay(5);
227                 Mix_FreeMusic(music);
228                 music = NULL;
229         }
230
231         return loadMusic(levelMusicName);
232 }
233
234 void Audio::playAmbiance()
235 {
236         if ((!engine->useAudio) || (soundVolume == 0))
237         {
238                 return;
239         }
240         
241         if (!output)
242         {
243                 return;
244         }
245
246         Mix_PlayChannel(CH_AMBIANCE, sound[SND_AMBIANCE], -1);
247 }
248
249 void Audio::stopAmbiance()
250 {
251         if ((!engine->useAudio) || (soundVolume == 0))
252                 return;
253
254         Mix_HaltChannel(CH_AMBIANCE);
255 }
256
257 int Audio::playMenuSound(int sound)
258 {
259         if ((!engine->useAudio) || (soundVolume == 0))
260                 return sound;
261
262         if ((sound == 0) || (sound == 3))
263                 return sound;
264
265         if (sound == 1)
266                 playSound(SND_HIGHLIGHT, CH_ANY);
267
268         if (sound == 2)
269                 playSound(SND_SELECT, CH_ANY);
270                 
271         return sound;
272 }
273
274 void Audio::pause()
275 {
276         if (!engine->useAudio)
277                 return;
278
279         for (int i = 0 ; i < 8 ; i++)
280                 Mix_Pause(i);
281
282         Mix_PauseMusic();
283 }
284
285 void Audio::resume()
286 {
287         if (!engine->useAudio)
288                 return;
289         
290         if (!output)
291         {
292                 return;
293         }
294
295         for (int i = 0 ; i < 8 ; i++)
296                 Mix_Resume(i);
297
298         Mix_ResumeMusic();
299 }
300
301 void Audio::stopMusic()
302 {
303         if (!engine->useAudio)
304                 return;
305
306         Mix_HaltMusic();
307 }
308
309 void Audio::fadeMusic()
310 {
311         if (!engine->useAudio)
312                 return;
313
314         Mix_FadeOutMusic(3500);
315 }
316
317 void Audio::free()
318 {
319         for (int i = 0 ; i < MAX_SOUNDS - 3 ; i++)
320         {
321                 if (sound[i] != NULL)
322                 {
323                         Mix_FreeChunk(sound[i]);
324                         sound[i] = NULL;
325                 }
326         }
327
328         if (music != NULL)
329         {
330                 Mix_HaltMusic();
331                 SDL_Delay(5);
332                 Mix_FreeMusic(music);
333         }
334
335         music = NULL;
336
337         if (quickSound != NULL)
338                 Mix_FreeChunk(quickSound);
339
340         quickSound = NULL;
341 }
342
343 void Audio::destroy()
344 {
345         free();
346
347         for (int i = MAX_SOUNDS - 3 ; i < MAX_SOUNDS ; i++)
348         {
349                 if (sound[i] != NULL)
350                 {
351                         Mix_FreeChunk(sound[i]);
352                         sound[i] = NULL;
353                 }
354         }
355 }