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