Generated SuperTux libtool library containing more general source, that could prove...
[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 "audio/sound.h"
26 #include "app/globals.h"
27 #include "app/setup.h"
28 #include "special/moving_object.h"
29
30 SoundManager::SoundManager()
31   : current_music(0), music_enabled(true)
32 {
33 }
34
35 SoundManager::~SoundManager()
36 {
37   if(audio_device)
38     Mix_HaltMusic();
39 }
40
41 void
42 SoundManager::play_sound(Mix_Chunk* sound)
43 {
44   if(!audio_device || !use_sound)
45     return;
46
47   Mix_PlayChannel(-1, sound, 0);  
48 }
49
50 void
51 SoundManager::play_sound(Mix_Chunk* sound, const MovingObject* object, const Vector& pos)
52 {
53   // TODO keep track of the object later and move the sound along with the
54   // object.
55   play_sound(sound, object->get_pos(), pos);
56 }
57
58 void
59 SoundManager::play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2)
60 {
61   if(!audio_device || !use_sound)
62     return;
63
64   // TODO make sure this formula is good
65   float distance 
66     = pos2.x- pos.x;
67   int loud = int(255.0/float(screen->w*2) * fabsf(distance));
68   if(loud > 255)
69     return;
70
71   int chan = Mix_PlayChannel(-1, sound, 0);
72   if(chan < 0)
73     return;                                  
74   Mix_SetDistance(chan, loud);
75
76   // very bad way to do this...
77   if(distance > 100)
78     Mix_SetPanning(chan, 230, 24);
79   else if(distance < -100)
80     Mix_SetPanning(chan, 24, 230);
81 }
82
83 MusicRef
84 SoundManager::load_music(const std::string& file)
85 {
86   if(!audio_device)
87     return MusicRef(0);
88
89   if(!exists_music(file))
90     st_abort("Couldn't load musicfile ", file.c_str());
91
92   std::map<std::string, MusicResource>::iterator i = musics.find(file);
93   assert(i != musics.end());
94   return MusicRef(& (i->second));
95 }
96
97 bool
98 SoundManager::exists_music(const std::string& file)
99 {
100   if(!audio_device)
101     return true;
102   
103   // song already loaded?
104   std::map<std::string, MusicResource>::iterator i = musics.find(file);
105   if(i != musics.end()) {
106     return true;                                      
107   }
108   
109   Mix_Music* song = Mix_LoadMUS(file.c_str());
110   if(song == 0)
111     return false;
112
113   // insert into music list
114   std::pair<std::map<std::string, MusicResource>::iterator, bool> result = 
115     musics.insert(
116         std::make_pair<std::string, MusicResource> (file, MusicResource()));
117   MusicResource& resource = result.first->second;
118   resource.manager = this;
119   resource.music = song;
120
121   return true;
122 }
123
124 void
125 SoundManager::free_music(MusicResource* )
126 {
127   // TODO free music, currently we can't do this since SDL_mixer seems to have
128   // some bugs if you load/free alot of mod files.  
129 }
130
131 void
132 SoundManager::play_music(const MusicRef& musicref, int loops)
133 {
134   if(!audio_device)
135     return;
136
137   if(musicref.music == 0 || current_music == musicref.music)
138     return;
139
140   if(current_music)
141     current_music->refcount--;
142   
143   current_music = musicref.music;
144   current_music->refcount++;
145   
146   if(music_enabled)
147     Mix_PlayMusic(current_music->music, loops);
148 }
149
150 void
151 SoundManager::halt_music()
152 {
153   if(!audio_device)
154     return;
155   
156   Mix_HaltMusic();
157   
158   if(current_music) {
159     current_music->refcount--;
160     if(current_music->refcount == 0)
161       free_music(current_music);
162     current_music = 0;
163   }
164 }
165
166 void
167 SoundManager::enable_music(bool enable)
168 {
169   if(!audio_device)
170     return;
171
172   if(enable == music_enabled)
173     return;
174   
175   music_enabled = enable;
176   if(music_enabled == false) {
177     Mix_HaltMusic();
178   } else {
179     Mix_PlayMusic(current_music->music, -1);
180   }
181 }
182
183 SoundManager::MusicResource::~MusicResource()
184 {
185   // don't free music buggy SDL_Mixer crashs for some mod files
186   // Mix_FreeMusic(music);
187 }
188