/*********************************************************\
		 __      __           __  __
		/ /     / / _____    / / / / ______   __     __
	   / /     / / /___  \  / / / / / ___  | / /    / /
	  / /_____/ / _____| / / / / / / /__/ / / /    / /
	 / ______  / / ___  / / / / / / _____/ / /    / /
	/ /     / / / /__/ / / / / / / |____  / |____/ /
   /_/     /_/ |______/ /_/ /_/  \_____/  \_____  /
										 ______/ /
			High Level Game Framework   /_______/

  ------------------------------------------------------

  Copyright (c) 2008 - Rodrigo Braz Monteiro.
  This file is subject to the terms of license.txt.

\*********************************************************/

#include "audio_music.h"
#include "SDL_mixer.h"
#include "resource_locator.h"
#include "exception.h"

using namespace Halley;

AudioMusic::AudioMusic(String filename)
	: mus(nullptr)
{
	auto data = ResourceLocator::get(filename);
	//mus = Mix_LoadMUS_RW(data->getRWOps());
	mus = Mix_LoadMUS(data->getPath().c_str());
	if (!mus) {
		throw Exception("Error loading music \""+filename+"\": "+Mix_GetError());
	}
}

AudioMusic::~AudioMusic()
{
	if (mus) {
		Mix_HaltMusic();
		Mix_FreeMusic(mus);
		mus = nullptr;
	}
}

void AudioMusic::play(int loops)
{
	if (Mix_PlayMusic(mus, loops) == -1) {
		std::cout << "Warning: failed trying to play music.h" << std::endl;
	}
}
