////////////////////////////////////////////////////////////////////////////////
//    Scorched3D (c) 2000-2004
//
//    This file is part of Scorched3D.
//
//    Scorched3D 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.
//
//    Scorched3D 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 Scorched3D; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

#include <sound/VirtualSoundSource.h>
#include <sound/PlayingSoundSource.h>
#include <sound/SoundSource.h>
#include <sound/Sound.h>

VirtualSoundSource::VirtualSoundSource(
	unsigned int priority, bool looping, bool managed) : 
	playingSource_(0),
	priority_(priority),
	gain_(1.0f), refDist_(75.0f), rolloff_(1.0f),
	buffer_(0),
	relative_(false), 
	managed_(managed),
	looping_(looping)
{
	DIALOG_ASSERT(!(managed_ && looping_));
	if (managed_) Sound::instance()->addManaged(this);
}

VirtualSoundSource::~VirtualSoundSource()
{
	stop();
}

void VirtualSoundSource::play(SoundBuffer *buffer)
{
	stop();

	buffer_ = buffer;
	Sound::instance()->addPlaying(this);
}

void VirtualSoundSource::actualPlay()
{
	playingSource_->actualSource->setGain(gain_);
	playingSource_->actualSource->setRolloff(rolloff_);
	playingSource_->actualSource->setReferenceDistance(refDist_);
	playingSource_->actualSource->setRelative(relative_);
	playingSource_->actualSource->setPosition(position_);
	playingSource_->actualSource->setVelocity(velocity_);
	playingSource_->actualSource->play(buffer_, looping_);
}

void VirtualSoundSource::stop()
{
	if (playingSource_)
	{
		Sound::instance()->removePlaying(this);
	}
	playingSource_ = 0;
}

void VirtualSoundSource::setPlayingSource(PlayingSoundSource *s)
{ 
	playingSource_ = s; 
}

bool VirtualSoundSource::getPlaying()
{
	if (playingSource_ && playingSource_->actualSource)
	{
		return playingSource_->actualSource->getPlaying();
	}
	return false;
}

void VirtualSoundSource::setRelative()
{
	relative_ = true;
	if (playingSource_ && playingSource_->actualSource)
	{
		playingSource_->actualSource->setRelative(true);
	}
}

void VirtualSoundSource::setPosition(Vector &position)
{
	position_ = position;
	if (playingSource_ && playingSource_->actualSource)
	{
		playingSource_->actualSource->setPosition(position);
	}
}

void VirtualSoundSource::setVelocity(Vector &velocity)
{
	velocity_ = velocity;
	if (playingSource_ && playingSource_->actualSource)
	{
		playingSource_->actualSource->setVelocity(velocity);
	}
}

void VirtualSoundSource::setGain(float gain)
{
	gain_ = gain;
	if (playingSource_ && playingSource_->actualSource)
	{
		playingSource_->actualSource->setGain(gain);
	}
}

void VirtualSoundSource::setReferenceDistance(float refDist)
{
	refDist_ = refDist;
	if (playingSource_ && playingSource_->actualSource)
	{
		playingSource_->actualSource->setReferenceDistance(refDist);
	}
}

void VirtualSoundSource::setRolloff(float rolloff)
{
	rolloff_ = rolloff;
	if (playingSource_ && playingSource_->actualSource)
	{
		playingSource_->actualSource->setRolloff(rolloff);
	}
}

void VirtualSoundSource::updateDistance(Vector &listener)
{
	Vector pos = getPosition();
	if (!getRelative())
	{
		pos -= listener;
	}
	distance_ = pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2];
}
