VirtualSoundSource.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <sound/VirtualSoundSource.h>
00022 #include <sound/PlayingSoundSource.h>
00023 #include <sound/SoundSource.h>
00024 #include <sound/Sound.h>
00025 
00026 VirtualSoundSource::VirtualSoundSource(
00027         unsigned int priority, bool looping, bool managed) : 
00028         playingSource_(0),
00029         priority_(priority),
00030         gain_(1.0f), refDist_(75.0f), rolloff_(1.0f),
00031         buffer_(0),
00032         relative_(false), 
00033         managed_(managed),
00034         looping_(looping)
00035 {
00036         if (managed_) Sound::instance()->addManaged(this);
00037 }
00038 
00039 VirtualSoundSource::~VirtualSoundSource()
00040 {
00041         stop();
00042         playingSource_ = 0;
00043 }
00044 
00045 void VirtualSoundSource::play(SoundBuffer *buffer)
00046 {
00047         stop();
00048 
00049         buffer_ = buffer;
00050         Sound::instance()->addPlaying(this);
00051 }
00052 
00053 void VirtualSoundSource::actualPlay()
00054 {
00055         playingSource_->getActualSource()->setGain(gain_);
00056         playingSource_->getActualSource()->setRolloff(rolloff_);
00057         playingSource_->getActualSource()->setReferenceDistance(refDist_);
00058         playingSource_->getActualSource()->setRelative(relative_);
00059         playingSource_->getActualSource()->setPosition(position_);
00060         playingSource_->getActualSource()->setVelocity(velocity_);
00061         playingSource_->getActualSource()->play(buffer_, looping_);
00062 }
00063 
00064 void VirtualSoundSource::stop()
00065 {
00066         if (playingSource_)
00067         {
00068                 Sound::instance()->removePlaying(this);
00069         }
00070 }
00071 
00072 void VirtualSoundSource::setPlayingSource(PlayingSoundSource *s)
00073 { 
00074         playingSource_ = s; 
00075 }
00076 
00077 bool VirtualSoundSource::getPlaying()
00078 {
00079         if (playingSource_ && playingSource_->getActualSource())
00080         {
00081                 return playingSource_->getActualSource()->getPlaying();
00082         }
00083         return false;
00084 }
00085 
00086 void VirtualSoundSource::setRelative()
00087 {
00088         relative_ = true;
00089         if (playingSource_ && playingSource_->getActualSource())
00090         {
00091                 playingSource_->getActualSource()->setRelative(true);
00092         }
00093 }
00094 
00095 void VirtualSoundSource::setPosition(Vector &position)
00096 {
00097         position_ = position;
00098         if (playingSource_ && playingSource_->getActualSource())
00099         {
00100                 playingSource_->getActualSource()->setPosition(position);
00101         }
00102 }
00103 
00104 void VirtualSoundSource::setVelocity(Vector &velocity)
00105 {
00106         velocity_ = velocity;
00107         if (playingSource_ && playingSource_->getActualSource())
00108         {
00109                 playingSource_->getActualSource()->setVelocity(velocity);
00110         }
00111 }
00112 
00113 void VirtualSoundSource::setGain(float gain)
00114 {
00115         gain_ = gain;
00116         if (playingSource_ && playingSource_->getActualSource())
00117         {
00118                 playingSource_->getActualSource()->setGain(gain);
00119         }
00120 }
00121 
00122 void VirtualSoundSource::setReferenceDistance(float refDist)
00123 {
00124         refDist_ = refDist;
00125         if (playingSource_ && playingSource_->getActualSource())
00126         {
00127                 playingSource_->getActualSource()->setReferenceDistance(refDist);
00128         }
00129 }
00130 
00131 void VirtualSoundSource::setRolloff(float rolloff)
00132 {
00133         rolloff_ = rolloff;
00134         if (playingSource_ && playingSource_->getActualSource())
00135         {
00136                 playingSource_->getActualSource()->setRolloff(rolloff);
00137         }
00138 }
00139 
00140 void VirtualSoundSource::updateDistance(Vector &listener)
00141 {
00142         Vector pos = getPosition();
00143         if (!getRelative())
00144         {
00145                 pos -= listener;
00146         }
00147         distance_ = pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2];
00148 }

Generated on Mon Feb 16 15:14:47 2009 for Scorched3D by  doxygen 1.5.3