1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Erwin 'vaiursch' Herrsche |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "orxonox/objects/worldentities/WorldEntity.h" |
---|
30 | #include "util/Math.h" |
---|
31 | #include "SoundManager.h" |
---|
32 | #include "SoundBase.h" |
---|
33 | |
---|
34 | namespace orxonox |
---|
35 | { |
---|
36 | SoundBase::SoundBase() |
---|
37 | { |
---|
38 | this->source_ = 0; |
---|
39 | this->buffer_ = 0; |
---|
40 | this->entity_ = NULL; |
---|
41 | } |
---|
42 | SoundBase::SoundBase(WorldEntity* entity) |
---|
43 | { |
---|
44 | this->source_ = 0; |
---|
45 | this->buffer_ = 0; |
---|
46 | this->entity_ = entity; |
---|
47 | } |
---|
48 | |
---|
49 | void SoundBase::attachToEntity(WorldEntity* entity) |
---|
50 | { |
---|
51 | this->entity_ = entity; |
---|
52 | this->update(); |
---|
53 | } |
---|
54 | |
---|
55 | void SoundBase::update() { |
---|
56 | if(this->entity_ != NULL && alIsSource(this->source_)) { |
---|
57 | Vector3 pos = this->entity_->getPosition(); |
---|
58 | alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z); |
---|
59 | ALenum error = alGetError(); |
---|
60 | if(error == AL_INVALID_VALUE) |
---|
61 | COUT(2) << "OpenAL: Invalid sound position" << std::endl; |
---|
62 | |
---|
63 | Vector3 vel = this->entity_->getVelocity(); |
---|
64 | alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z); |
---|
65 | error = alGetError(); |
---|
66 | if(error == AL_INVALID_VALUE) |
---|
67 | COUT(2) << "OpenAL: Invalid sound position" << std::endl; |
---|
68 | |
---|
69 | Quaternion orient = this->entity_->getOrientation(); |
---|
70 | Vector3 at = orient.zAxis(); |
---|
71 | alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z); |
---|
72 | error = alGetError(); |
---|
73 | if(error == AL_INVALID_VALUE) |
---|
74 | COUT(2) << "OpenAL: Invalid sound position" << std::endl; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | void SoundBase::play(bool loop) { |
---|
79 | if(alIsSource(this->source_)) { |
---|
80 | if(loop) |
---|
81 | alSourcei(this->source_, AL_LOOPING, AL_TRUE); |
---|
82 | else |
---|
83 | alSourcei(this->source_, AL_LOOPING, AL_FALSE); |
---|
84 | alSourcePlay(this->source_); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | void SoundBase::stop() { |
---|
89 | if(alIsSource(this->source_)) { |
---|
90 | alSourceStop(this->source_); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | void SoundBase::pause() { |
---|
95 | if(alIsSource(this->source_)) { |
---|
96 | alSourcePause(this->source_); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | bool SoundBase::isPlaying() { |
---|
101 | if(alIsSource(this->source_)) { |
---|
102 | return getSourceState() == AL_PLAYING; |
---|
103 | } |
---|
104 | return false; |
---|
105 | } |
---|
106 | |
---|
107 | bool SoundBase::isPaused() { |
---|
108 | if(alIsSource(this->source_)) { |
---|
109 | return getSourceState() == AL_PAUSED; |
---|
110 | } |
---|
111 | return true; |
---|
112 | } |
---|
113 | |
---|
114 | bool SoundBase::isStopped() { |
---|
115 | if(alIsSource(this->source_)) { |
---|
116 | return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED; |
---|
117 | } |
---|
118 | return true; |
---|
119 | } |
---|
120 | |
---|
121 | bool SoundBase::loadFile(std::string filename) { |
---|
122 | COUT(3) << "OpenAL ALUT: loading file " << filename << std::endl; |
---|
123 | this->buffer_ = alutCreateBufferFromFile(filename.c_str()); |
---|
124 | if(this->buffer_ == AL_NONE) { |
---|
125 | COUT(2) << "OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
126 | return false; |
---|
127 | } |
---|
128 | |
---|
129 | alGenSources(1, &this->source_); |
---|
130 | alSourcei(this->source_, AL_BUFFER, this->buffer_); |
---|
131 | if(alGetError() != AL_NO_ERROR) { |
---|
132 | COUT(2) << "OpenAL: Error loading sample file" << std::endl; |
---|
133 | return false; |
---|
134 | } |
---|
135 | return true; |
---|
136 | } |
---|
137 | |
---|
138 | ALint SoundBase::getSourceState() { |
---|
139 | ALint state; |
---|
140 | alGetSourcei(this->source_, AL_SOURCE_STATE, &state); |
---|
141 | return state; |
---|
142 | } |
---|
143 | } // namespace: orxonox |
---|