//
// music.cpp
//
// music manager
//
//
// Author: Tomi Belan <tomi.belan@gmail.com>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <string>
#include <SDL_mixer.h>

#include "music.h"
#include "main.h"

static Mix_Music *m1, *m2, *m3;
static int currentMusic;

namespace Music {
  bool load()
  {
    if(!haveSound)
      return true;
    
    m1 = Mix_LoadMUS((programDataPath + "music1.mp3").c_str());
    if(!m1) return error("$Can't load music1.mp3", ERR_ERROR);
    m2 = Mix_LoadMUS((programDataPath + "music2.mp3").c_str());
    if(!m2) return error("$Can't load music2.mp3", ERR_ERROR);
    m3 = Mix_LoadMUS((programDataPath + "music3.mp3").c_str());
    if(!m3) return error("$Can't load music3.mp3", ERR_ERROR);
  }
  
  void unload()
  {
    if(!haveSound)
      return;
    
    if(m1) Mix_FreeMusic(m1);
    if(m2) Mix_FreeMusic(m2);
    if(m3) Mix_FreeMusic(m3);
  }
  
  void play(int n)
  {
    if(!haveSound)
      return;
    if(n == currentMusic)
      return;
    if(Mix_PlayingMusic())
      Mix_HaltMusic();
    currentMusic = n;
    Mix_Music *m;
    switch(n) {
      case 1: Mix_PlayMusic(m1, -1); break;
      case 2: Mix_PlayMusic(m2, -1); break;
      case 3: Mix_PlayMusic(m3, -1); break;
      default: currentMusic = 0; break;
    }
  }
  
  int current()
  {
    if(!haveSound)
      return 0;
    return currentMusic;
  }
};
