/***************************************************************************
 *   Copyright (C) 2007 by Denis A. Birukov   *
 *   Denis A. Birukov@gmail.com   *
 *                                                                         *
 *   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 "config.h"
#include <SDL/SDL_mixer.h>
#include <SDL/SDL.h>
#include "music.h"
#include <iostream>
bool is_music_inited = false;
bool init_music()
{
	if ( SDL_InitSubSystem ( SDL_INIT_AUDIO ) != 0 ) {std::cerr<<"can't init audio subsistem"<<std::endl; is_music_inited = false; return false;}
	if ( Mix_OpenAudio ( 44100, AUDIO_S16, 6, 4096 ) < 0 ) {std::cerr<<"cant open audio!"<<std::endl; is_music_inited = false; return false;}
	is_music_inited = true;
	Mix_VolumeMusic ( 128 );
	return true;
}

bool Music::load ( std::string file )
{
	if ( !is_music_inited ) return false;
	_music_ = Mix_LoadMUS ( file.c_str() );
	if ( !_music_ )
	{
		std::cerr<<"Mix_LoadMUS"<<file<<" error:"<<Mix_GetError() <<std::endl;
	}
	return true;

}
void Music::play(int repeat)
{
	if ( !_music_ || !is_music_inited ) return;
	Mix_PlayMusic ( _music_, repeat );
}

void Music::stop()
{
	if ( !_music_ || !is_music_inited ) return;
	Mix_HaltMusic( );
}

void Music::pause()
{
	if ( !_music_ || !is_music_inited ) return;
	Mix_PauseMusic();
}

bool Effect::load ( std::string file )
{
	if ( !is_music_inited ) return false;
	_chunk_ = Mix_LoadWAV ( file.c_str() );
	if ( !_chunk_ )
	{
		std::cerr<<"Mix_LoadWAV:"<<file<<" error:"<<Mix_GetError() <<std::endl;
	}
	return true;
}
void Effect::play()
{
	if ( !_chunk_ || !is_music_inited ) return;
	_channel_ = Mix_PlayChannel ( -1, _chunk_, 0 );
}
void Effect::stop()
{
	if ( !_chunk_ || !is_music_inited ) return;
	Mix_HaltChannel ( _channel_ );
}
void Effect::pause()
{
	if ( !_chunk_ || !is_music_inited ) return;
//        Mix_PauseChannel( _channel_ );
}

Music::~ Music()
{
	if ( !_music_ || !is_music_inited ) return;
	Mix_FreeMusic ( _music_ );
	_music_ = 0;
}

Effect::~ Effect()
{
	if ( !_chunk_ || !is_music_inited ) return;
	Mix_FreeChunk ( _chunk_ );
	_chunk_ = 0;
}
