#include <assert.h>
#include "music_manager.h"
#include "musicref.h"
#include "sound.h"
#include "setup.h"
MusicManager::MusicManager()
#ifndef NO_SDLMIXER
: current_music(0), music_enabled(true)
#endif
{ }
MusicManager::~MusicManager()
{
#ifndef NO_SDLMIXER
if(audio_device)
Mix_HaltMusic();
#endif
}
MusicRef
MusicManager::load_music(const std::string& file)
{
#ifndef NO_SDLMIXER
if(!audio_device)
return MusicRef(0);
if(!exists_music(file))
st_abort("Couldn't load musicfile ", file.c_str());
std::map<std::string, MusicResource>::iterator i = musics.find(file);
assert(i != musics.end());
return MusicRef(& (i->second));
#else
return MusicRef(0);
#endif
}
bool
MusicManager::exists_music(const std::string& file)
{
#ifndef NO_SDLMIXER
if(!audio_device)
return true;
std::map<std::string, MusicResource>::iterator i = musics.find(file);
if(i != musics.end()) {
return true;
}
Mix_Music* song = Mix_LoadMUS(file.c_str());
if(song == 0)
return false;
std::pair<std::map<std::string, MusicResource>::iterator, bool> result =
musics.insert(
std::make_pair<std::string, MusicResource> (file, MusicResource()));
MusicResource& resource = result.first->second;
resource.manager = this;
resource.music = song;
#endif
return true;
}
void
MusicManager::free_music(MusicResource* )
{
}
void
MusicManager::play_music(const MusicRef& musicref, int loops)
{
#ifndef NO_SDLMIXER
if(!audio_device)
return;
if(musicref.music == 0 || current_music == musicref.music)
return;
if(current_music)
current_music->refcount--;
current_music = musicref.music;
current_music->refcount++;
if(music_enabled)
Mix_PlayMusic(current_music->music, loops);
#endif
}
void
MusicManager::halt_music()
{
#ifndef NO_SDLMIXER
if(!audio_device)
return;
Mix_HaltMusic();
if(current_music) {
current_music->refcount--;
if(current_music->refcount == 0)
free_music(current_music);
current_music = 0;
}
#endif
}
void
MusicManager::enable_music(bool enable)
{
#ifndef NO_SDLMIXER
if(!audio_device)
return;
if(enable == music_enabled)
return;
music_enabled = enable;
if(music_enabled == false) {
Mix_HaltMusic();
} else {
Mix_PlayMusic(current_music->music, -1);
}
#endif
}
MusicManager::MusicResource::~MusicResource()
{
#ifndef NO_SDLMIXER
#endif
}