4 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.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.
21 #include "music_manager.h"
26 MusicManager::MusicManager()
27 : current_music(0), music_enabled(true)
30 MusicManager::~MusicManager()
37 MusicManager::load_music(const std::string& file)
42 if(!exists_music(file))
43 st_abort("Couldn't load musicfile ", file.c_str());
45 std::map<std::string, MusicResource>::iterator i = musics.find(file);
46 assert(i != musics.end());
47 return MusicRef(& (i->second));
51 MusicManager::exists_music(const std::string& file)
56 // song already loaded?
57 std::map<std::string, MusicResource>::iterator i = musics.find(file);
58 if(i != musics.end()) {
62 Mix_Music* song = Mix_LoadMUS(file.c_str());
66 // insert into music list
67 std::pair<std::map<std::string, MusicResource>::iterator, bool> result =
69 std::make_pair<std::string, MusicResource> (file, MusicResource()));
70 MusicResource& resource = result.first->second;
71 resource.manager = this;
72 resource.music = song;
78 MusicManager::free_music(MusicResource* )
80 // TODO free music, currently we can't do this since SDL_mixer seems to have
81 // some bugs if you load/free alot of mod files.
85 MusicManager::play_music(const MusicRef& musicref)
90 if(musicref.music == 0 || current_music == musicref.music)
94 current_music->refcount--;
96 current_music = musicref.music;
97 current_music->refcount++;
100 Mix_PlayMusic(current_music->music, -1);
104 MusicManager::halt_music()
112 current_music->refcount--;
113 if(current_music->refcount == 0)
114 free_music(current_music);
120 MusicManager::enable_music(bool enable)
125 if(enable == music_enabled)
128 music_enabled = enable;
129 if(music_enabled == false) {
132 Mix_PlayMusic(current_music->music, -1);
136 MusicManager::MusicResource::~MusicResource()
138 // buggy SDL_mixer :-/
139 // Mix_FreeMusic(music);