3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 Matthias Braun <matze@braunis.de
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.
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.
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.
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"
29 using namespace SuperTux;
31 SoundManager* SoundManager::instance_ = 0;
33 SoundManager::SoundManager()
34 : current_music(0), m_music_enabled(true) , m_sound_enabled(true) , audio_device(true)
38 SoundManager::~SoundManager()
48 SoundManager::play_sound(Mix_Chunk* sound)
50 if(!audio_device || !m_sound_enabled)
53 Mix_PlayChannel(-1, sound, 0);
57 SoundManager::play_sound(Mix_Chunk* sound, const MovingObject* object, const Vector& pos)
59 // TODO keep track of the object later and move the sound along with the
61 play_sound(sound, object->get_pos(), pos);
65 SoundManager::play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2)
67 if(!audio_device || !m_sound_enabled)
70 // TODO make sure this formula is good
73 int loud = int(255.0/float(screen->w*2) * fabsf(distance));
77 int chan = Mix_PlayChannel(-1, sound, 0);
80 Mix_SetDistance(chan, loud);
82 // very bad way to do this...
84 Mix_SetPanning(chan, 230, 24);
85 else if(distance < -100)
86 Mix_SetPanning(chan, 24, 230);
90 SoundManager::load_music(const std::string& file)
95 if(!exists_music(file))
96 Termination::abort("Couldn't load musicfile ", file.c_str());
98 std::map<std::string, MusicResource>::iterator i = musics.find(file);
99 assert(i != musics.end());
100 return MusicRef(& (i->second));
104 SoundManager::exists_music(const std::string& file)
109 // song already loaded?
110 std::map<std::string, MusicResource>::iterator i = musics.find(file);
111 if(i != musics.end()) {
115 Mix_Music* song = Mix_LoadMUS(file.c_str());
119 // insert into music list
120 std::pair<std::map<std::string, MusicResource>::iterator, bool> result =
122 std::make_pair<std::string, MusicResource> (file, MusicResource()));
123 MusicResource& resource = result.first->second;
124 resource.manager = this;
125 resource.music = song;
131 SoundManager::free_music(MusicResource* )
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.
138 SoundManager::play_music(const MusicRef& musicref, int loops)
143 if(musicref.music == 0 || current_music == musicref.music)
147 current_music->refcount--;
149 current_music = musicref.music;
150 current_music->refcount++;
153 Mix_PlayMusic(current_music->music, loops);
157 SoundManager::halt_music()
165 current_music->refcount--;
166 if(current_music->refcount == 0)
167 free_music(current_music);
173 SoundManager::enable_music(bool enable)
178 if(enable == m_music_enabled)
181 m_music_enabled = enable;
182 if(m_music_enabled == false) {
185 Mix_PlayMusic(current_music->music, -1);
190 SoundManager::enable_sound(bool enable)
195 m_sound_enabled = enable;
198 SoundManager::MusicResource::~MusicResource()
200 // don't free music buggy SDL_Mixer crashs for some mod files
201 // Mix_FreeMusic(music);
204 /* --- LOAD A SOUND --- */
206 Mix_Chunk* SoundManager::load_sound(const std::string& file)
211 Mix_Chunk* snd = Mix_LoadWAV(file.c_str());
214 Termination::abort("Can't load", file);*/
219 void SoundManager::free_chunk(Mix_Chunk *chunk)
221 Mix_FreeChunk( chunk );
225 /* --- OPEN THE AUDIO DEVICE --- */
227 int SoundManager::open_audio (int frequency, Uint16 format, int channels, int chunksize)
229 if (Mix_OpenAudio( frequency, format, channels, chunksize ) < 0)
232 // allocate 16 channels for mixing
233 if (Mix_AllocateChannels(8) != 8)
240 /* --- CLOSE THE AUDIO DEVICE --- */
242 void SoundManager::close_audio( void )
249 Mix_Chunk* SuperTux::IDToSound(int id)
251 return SoundManager::get()->sounds[id];