panthema / 2006 / SDIOS06 / sdios06 / src / supertux / music_manager.cpp (Download File)
//  $Id: music_manager.cpp 827 2004-04-29 00:15:11Z grumbel $
//
//  SuperTux
//  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#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;
  
  // song already loaded?
  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;

  // insert into music list
  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* )
{
  // TODO free music, currently we can't do this since SDL_mixer seems to have
  // some bugs if you load/free alot of mod files.  
}

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
  // buggy SDL_mixer :-/
  // Mix_FreeMusic(music);
#endif
}

RSS 2.0 Weblog Feed Atom 1.0 Weblog Feed Valid XHTML 1.1 Valid CSS (2.1)
Copyright 2005-2014 Timo Bingmann - Impressum