1 //  $Id: music_manager.cpp 827 2004-04-29 00:15:11Z grumbel $
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.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 <assert.h>
21 #include "music_manager.h"
22 #include "musicref.h"
23 #include "sound.h"
24 #include "setup.h"
25 
MusicManager()26 MusicManager::MusicManager()
27   : current_music(0), music_enabled(true)
28 { }
29 
~MusicManager()30 MusicManager::~MusicManager()
31 {
32   if(audio_device)
33     Mix_HaltMusic();
34 }
35 
36 MusicRef
load_music(const std::string & file)37 MusicManager::load_music(const std::string& file)
38 {
39   if(!audio_device)
40     return MusicRef(0);
41 
42   if(!exists_music(file))
43     st_abort("Couldn't load musicfile ", file.c_str());
44 
45   std::map<std::string, MusicResource>::iterator i = musics.find(file);
46   assert(i != musics.end());
47   return MusicRef(& (i->second));
48 }
49 
50 bool
exists_music(const std::string & file)51 MusicManager::exists_music(const std::string& file)
52 {
53   if(!audio_device)
54     return true;
55 
56   // song already loaded?
57   std::map<std::string, MusicResource>::iterator i = musics.find(file);
58   if(i != musics.end()) {
59     return true;
60   }
61 
62   Mix_Music* song = Mix_LoadMUS(file.c_str());
63   if(song == 0)
64     return false;
65 
66   // insert into music list
67   std::pair<std::map<std::string, MusicResource>::iterator, bool> result =
68     musics.insert(
69         std::make_pair<std::string, MusicResource> (std::string(file), MusicResource()));
70   MusicResource& resource = result.first->second;
71   resource.manager = this;
72   resource.music = song;
73 
74   return true;
75 }
76 
77 void
free_music(MusicResource *)78 MusicManager::free_music(MusicResource* )
79 {
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.
82 }
83 
84 void
play_music(const MusicRef & musicref,int loops)85 MusicManager::play_music(const MusicRef& musicref, int loops)
86 {
87   if(!audio_device)
88     return;
89 
90   if(musicref.music == 0 || current_music == musicref.music)
91     return;
92 
93   if(current_music)
94     current_music->refcount--;
95 
96   current_music = musicref.music;
97   current_music->refcount++;
98 
99   if(music_enabled)
100     Mix_PlayMusic(current_music->music, loops);
101 }
102 
103 void
halt_music()104 MusicManager::halt_music()
105 {
106   if(!audio_device)
107     return;
108 
109   Mix_HaltMusic();
110 
111   if(current_music) {
112     current_music->refcount--;
113     if(current_music->refcount == 0)
114       free_music(current_music);
115     current_music = 0;
116   }
117 }
118 
119 void
enable_music(bool enable)120 MusicManager::enable_music(bool enable)
121 {
122   if(!audio_device)
123     return;
124 
125   if(enable == music_enabled)
126     return;
127 
128   music_enabled = enable;
129   if(music_enabled == false) {
130     Mix_HaltMusic();
131   } else {
132     Mix_PlayMusic(current_music->music, -1);
133   }
134 }
135 
~MusicResource()136 MusicManager::MusicResource::~MusicResource()
137 {
138   // buggy SDL_mixer :-/
139   // Mix_FreeMusic(music);
140 }
141 
142