Renamed code variables, as well.
[supertux.git] / lib / audio / sound_manager.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
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.
19
20 #include <cmath>
21 #include <cassert>
22
23 #include "../audio/sound_manager.h"
24 #include "../audio/musicref.h"
25 #include "../app/globals.h"
26 #include "../app/setup.h"
27 #include "../special/moving_object.h"
28
29 using namespace SuperTux;
30
31 SoundManager* SoundManager::instance_ = 0;
32
33 SoundManager::SoundManager()
34   : current_music(0), m_music_enabled(true) , m_sound_enabled(true) , audio_device(true)
35 {
36 }
37
38 SoundManager::~SoundManager()
39 {
40   if(audio_device)
41     Mix_HaltMusic();
42
43 sounds.clear();
44 destroy_instance();
45 }
46
47 void
48 SoundManager::play_sound(Mix_Chunk* sound)
49 {
50   if(!audio_device || !m_sound_enabled)
51     return;
52
53   Mix_PlayChannel(-1, sound, 0);  
54 }
55
56 void
57 SoundManager::play_sound(Mix_Chunk* sound, const MovingObject* object, const Vector& pos)
58 {
59   // TODO keep track of the object later and move the sound along with the
60   // object.
61   play_sound(sound, object->get_pos(), pos);
62 }
63
64 void
65 SoundManager::play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2)
66 {
67   if(!audio_device || !m_sound_enabled)
68     return;
69
70   // TODO make sure this formula is good
71   float distance 
72     = pos2.x- pos.x;
73   int loud = int(255.0/float(screen->w*2) * fabsf(distance));
74   if(loud > 255)
75     return;
76
77   int chan = Mix_PlayChannel(-1, sound, 0);
78   if(chan < 0)
79     return;                                  
80   Mix_SetDistance(chan, loud);
81
82   // very bad way to do this...
83   if(distance > 100)
84     Mix_SetPanning(chan, 230, 24);
85   else if(distance < -100)
86     Mix_SetPanning(chan, 24, 230);
87 }
88
89 MusicRef
90 SoundManager::load_music(const std::string& file)
91 {
92   if(!audio_device)
93     return MusicRef(0);
94
95   if(!exists_music(file))
96     Termination::abort("Couldn't load musicfile ", file.c_str());
97
98   std::map<std::string, MusicResource>::iterator i = musics.find(file);
99   assert(i != musics.end());
100   return MusicRef(& (i->second));
101 }
102
103 bool
104 SoundManager::exists_music(const std::string& file)
105 {
106   if(!audio_device)
107     return true;
108   
109   // song already loaded?
110   std::map<std::string, MusicResource>::iterator i = musics.find(file);
111   if(i != musics.end()) {
112     return true;                                      
113   }
114   
115   Mix_Music* song = Mix_LoadMUS(file.c_str());
116   if(song == 0)
117     return false;
118
119   // insert into music list
120   std::pair<std::map<std::string, MusicResource>::iterator, bool> result = 
121     musics.insert(
122         std::make_pair<std::string, MusicResource> (file, MusicResource()));
123   MusicResource& resource = result.first->second;
124   resource.manager = this;
125   resource.music = song;
126
127   return true;
128 }
129
130 void
131 SoundManager::free_music(MusicResource* )
132 {
133   // TODO free music, currently we can't do this since SDL_mixer seems to have
134   // some bugs if you load/free alot of mod files.  
135 }
136
137 void
138 SoundManager::play_music(const MusicRef& musicref, int loops)
139 {
140   if(!audio_device)
141     return;
142
143   if(musicref.music == 0 || current_music == musicref.music)
144     return;
145
146   if(current_music)
147     current_music->refcount--;
148   
149   current_music = musicref.music;
150   current_music->refcount++;
151   
152   if(m_music_enabled)
153     Mix_PlayMusic(current_music->music, loops);
154 }
155
156 void
157 SoundManager::halt_music()
158 {
159   if(!audio_device)
160     return;
161   
162   Mix_HaltMusic();
163   
164   if(current_music) {
165     current_music->refcount--;
166     if(current_music->refcount == 0)
167       free_music(current_music);
168     current_music = 0;
169   }
170 }
171
172 void
173 SoundManager::enable_music(bool enable)
174 {
175   if(!audio_device)
176     return;
177
178   if(enable == m_music_enabled)
179     return;
180   
181   m_music_enabled = enable;
182   if(m_music_enabled == false) {
183     Mix_HaltMusic();
184   } else {
185     Mix_PlayMusic(current_music->music, -1);
186   }
187 }
188
189 void
190 SoundManager::enable_sound(bool enable)
191 {
192   if(!audio_device)
193     return;
194   
195   m_sound_enabled = enable;
196 }
197
198 SoundManager::MusicResource::~MusicResource()
199 {
200   // don't free music buggy SDL_Mixer crashs for some mod files
201   // Mix_FreeMusic(music);
202 }
203
204 /* --- LOAD A SOUND --- */
205
206 Mix_Chunk* SoundManager::load_sound(const std::string& file)
207 {
208   if(!audio_device)
209     return 0;
210   
211   Mix_Chunk* snd = Mix_LoadWAV(file.c_str());
212
213   /*if (snd == 0)
214     Termination::abort("Can't load", file);*/
215
216   return(snd);
217 }
218
219 void SoundManager::free_chunk(Mix_Chunk *chunk)
220 {
221   Mix_FreeChunk( chunk );
222 }
223
224
225 /* --- OPEN THE AUDIO DEVICE --- */
226
227 int SoundManager::open_audio (int frequency, Uint16 format, int channels, int chunksize)
228 {
229   if (Mix_OpenAudio( frequency, format, channels, chunksize ) < 0)
230     return -1;
231
232   // allocate 16 channels for mixing
233   if (Mix_AllocateChannels(8)  != 8)
234     return -2;
235   
236   return 0;
237 }
238
239
240 /* --- CLOSE THE AUDIO DEVICE --- */
241
242 void SoundManager::close_audio( void )
243 {
244   if (audio_device) {
245     Mix_CloseAudio();
246   }
247 }
248
249 Mix_Chunk* SuperTux::IDToSound(int id)
250 {
251   return SoundManager::get()->sounds[id];
252 }
253