source: GTP/trunk/App/Games/Jungle_Rumble/src/SoundNode.cpp @ 1378

Revision 1378, 2.9 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - integrate into GTP SVN structure

Line 
1#include "dxstdafx.h"
2#include ".\SoundNode.h"
3#include "Scene.h"
4#include "Camera.h"
5#include "GameManager.h"
6#include "ResourceManager.h"
7
8SoundNode::SoundNode(void) : Node() {
9        this->soundSample = NULL;
10        this->channel = -1;
11        this->kill = true;
12        this->nodeType |= Scene::NODE_SOUND;
13        this->playing = false;
14        this->vol = 1;
15        this->timeToPlaySample = -1;
16        D3DXMatrixIdentity(&this->myWorldMatrix);
17}
18
19SoundNode::~SoundNode(void) {
20}
21
22bool SoundNode::loadFile(std::string filename, bool loop) {
23        this->loop = loop;
24        this->soundSample = this->myScene->manager->resManager.loadSound(filename, this->loop);
25        if(this->soundSample!=NULL) {
26                FSOUND_Sample_SetMinMaxDistance(this->soundSample, 20.0f, 500.0f);
27        }
28        return (this->soundSample!=NULL);
29}
30
31void SoundNode::play() {
32        this->channel = FSOUND_PlaySound(FSOUND_FREE, this->soundSample);
33        this->timeToPlaySample = (float)FSOUND_Sample_GetLength(this->soundSample)/FSOUND_GetFrequency(this->channel);
34        if(this->channel!=-1) {
35                FSOUND_SetVolume(this->channel, (int)(255*this->vol));
36        }
37        this->playing = true;
38}
39
40void SoundNode::stop() {
41        if(this->channel!=-1) {
42                FSOUND_StopSound(this->channel);
43                this->playing = false;
44                this->timeToPlaySample = -1;
45        }
46        this->channel = -1;
47}
48
49FSOUND_SAMPLE* SoundNode::getSample() {
50        return this->soundSample;
51}
52
53void SoundNode::update(float dt) {
54        if(this->playing)
55                this->timeToPlaySample-=dt;
56        Vector origin;
57        origin = this->getAbsolutePosition();
58        this->vel = origin - this->oldPos;
59        this->oldPos = origin;
60        this->vel = this->vel*dt;
61               
62        if(this->channel!=-1) {
63                float velF[3];
64                float posF[3];
65
66                velF[0] = this->vel.x; velF[1] = this->vel.y; velF[2] = this->vel.z;
67                posF[0] = origin.x; posF[1] = origin.y; posF[2] = origin.z;
68
69                FSOUND_3D_SetAttributes(this->channel,  &posF[0], &velF[0]);
70
71                if(this->kill && !this->loop && this->timeToPlaySample<0) {
72                        this->killMe();
73                } else if(timeToPlaySample<=0) {
74                        this->playing = false;
75                        this->timeToPlaySample = -1;
76                }
77        }
78        Node::update(dt);
79}
80
81void SoundNode::setKillSoundNodeAfterPlayed(bool _kill)
82{
83        this->kill = _kill;
84}
85
86int SoundNode::getChannel() {
87        return this->channel;
88}
89
90float SoundNode::getTimeToPlay()
91{
92        return this->timeToPlaySample;
93}
94
95Node* SoundNode::clone() {
96        return NULL;
97}
98
99void SoundNode::setVolume(float _vol) {
100        this->vol = _vol;
101        if(this->channel!=-1) {
102                FSOUND_SetVolume(this->channel, (int)(255*this->vol));
103        }       
104}
105
106void SoundNode::setSceneAlpha(float _alpha)
107{
108        if(this->channel!=-1) {
109                FSOUND_SetVolume(this->channel, (int)(255*this->vol*_alpha));
110        }       
111}
112
113void SoundNode::killMe()
114{
115        this->stop();
116        Node::killMe();
117}
118
119void SoundNode::setPosition(Vector &_pos)
120{
121        Node::setPosition(_pos);
122        float velF[3];
123        float posF[3];
124
125        velF[0] = 0; velF[1] = 0; velF[2] = 0;
126        posF[0] = _pos.x; posF[1] = _pos.y; posF[2] = _pos.z;
127
128        FSOUND_3D_SetAttributes(this->channel,  &posF[0], &velF[0]);
129}
Note: See TracBrowser for help on using the repository browser.