#include "dxstdafx.h" #include ".\SoundNode.h" #include "Scene.h" #include "Camera.h" #include "GameManager.h" #include "ResourceManager.h" SoundNode::SoundNode(void) : Node() { this->soundSample = NULL; this->channel = -1; this->kill = true; this->nodeType |= Scene::NODE_SOUND; this->playing = false; this->vol = 1; this->timeToPlaySample = -1; D3DXMatrixIdentity(&this->myWorldMatrix); } SoundNode::~SoundNode(void) { } bool SoundNode::loadFile(std::string filename, bool loop) { this->loop = loop; this->soundSample = this->myScene->manager->resManager.loadSound(filename, this->loop); if(this->soundSample!=NULL) { FSOUND_Sample_SetMinMaxDistance(this->soundSample, 20.0f, 500.0f); } return (this->soundSample!=NULL); } void SoundNode::play() { this->channel = FSOUND_PlaySound(FSOUND_FREE, this->soundSample); this->timeToPlaySample = (float)FSOUND_Sample_GetLength(this->soundSample)/FSOUND_GetFrequency(this->channel); if(this->channel!=-1) { FSOUND_SetVolume(this->channel, (int)(255*this->vol)); } this->playing = true; } void SoundNode::stop() { if(this->channel!=-1) { FSOUND_StopSound(this->channel); this->playing = false; this->timeToPlaySample = -1; } this->channel = -1; } FSOUND_SAMPLE* SoundNode::getSample() { return this->soundSample; } void SoundNode::update(float dt) { if(this->playing) this->timeToPlaySample-=dt; Vector origin; origin = this->getAbsolutePosition(); this->vel = origin - this->oldPos; this->oldPos = origin; this->vel = this->vel*dt; if(this->channel!=-1) { float velF[3]; float posF[3]; velF[0] = this->vel.x; velF[1] = this->vel.y; velF[2] = this->vel.z; posF[0] = origin.x; posF[1] = origin.y; posF[2] = origin.z; FSOUND_3D_SetAttributes(this->channel, &posF[0], &velF[0]); if(this->kill && !this->loop && this->timeToPlaySample<0) { this->killMe(); } else if(timeToPlaySample<=0) { this->playing = false; this->timeToPlaySample = -1; } } Node::update(dt); } void SoundNode::setKillSoundNodeAfterPlayed(bool _kill) { this->kill = _kill; } int SoundNode::getChannel() { return this->channel; } float SoundNode::getTimeToPlay() { return this->timeToPlaySample; } Node* SoundNode::clone() { return NULL; } void SoundNode::setVolume(float _vol) { this->vol = _vol; if(this->channel!=-1) { FSOUND_SetVolume(this->channel, (int)(255*this->vol)); } } void SoundNode::setSceneAlpha(float _alpha) { if(this->channel!=-1) { FSOUND_SetVolume(this->channel, (int)(255*this->vol*_alpha)); } } void SoundNode::killMe() { this->stop(); Node::killMe(); } void SoundNode::setPosition(Vector &_pos) { Node::setPosition(_pos); float velF[3]; float posF[3]; velF[0] = 0; velF[1] = 0; velF[2] = 0; posF[0] = _pos.x; posF[1] = _pos.y; posF[2] = _pos.z; FSOUND_3D_SetAttributes(this->channel, &posF[0], &velF[0]); }