[3060] | 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: |
---|
[5896] | 23 | * Erwin 'vaiursch' Herrsche |
---|
| 24 | * Reto Grieder |
---|
[3060] | 25 | * Co-authors: |
---|
| 26 | * ... |
---|
| 27 | * |
---|
| 28 | */ |
---|
[3196] | 29 | |
---|
[5896] | 30 | #include "WorldSound.h" |
---|
[3196] | 31 | |
---|
[3060] | 32 | #include <AL/alut.h> |
---|
[3196] | 33 | #include "util/Math.h" |
---|
[5914] | 34 | #include "core/CoreIncludes.h" |
---|
[5896] | 35 | #include "core/EventIncludes.h" |
---|
| 36 | #include "core/XMLPort.h" |
---|
[6186] | 37 | #include "SoundManager.h" |
---|
[6307] | 38 | #include <core/ConsoleCommandCompilation.h> |
---|
[3060] | 39 | |
---|
[5738] | 40 | namespace orxonox |
---|
[3060] | 41 | { |
---|
[5896] | 42 | CreateFactory(WorldSound); |
---|
| 43 | |
---|
| 44 | WorldSound::WorldSound(BaseObject* creator) |
---|
| 45 | : StaticEntity(creator) |
---|
[3060] | 46 | { |
---|
[5896] | 47 | RegisterObject(WorldSound); |
---|
[6254] | 48 | // WorldSound buffers should be pooled when they're not used anymore |
---|
| 49 | this->bPooling_ = true; |
---|
[6307] | 50 | this->registerVariables(); |
---|
[5896] | 51 | } |
---|
[3060] | 52 | |
---|
[5896] | 53 | WorldSound::~WorldSound() |
---|
| 54 | { |
---|
[3060] | 55 | } |
---|
[6307] | 56 | |
---|
| 57 | void WorldSound::registerVariables() |
---|
| 58 | { |
---|
| 59 | registerVariable(volume_, ObjectDirection::ToClient, new NetworkCallback<BaseSound>(static_cast<BaseSound*>(this), &BaseSound::volumeChanged)); |
---|
| 60 | registerVariable(source_, ObjectDirection::ToClient, new NetworkCallback<BaseSound>(static_cast<BaseSound*>(this), &BaseSound::sourceChanged)); |
---|
| 61 | registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback<BaseSound>(static_cast<BaseSound*>(this), &BaseSound::loopingChanged)); |
---|
[6320] | 62 | registerVariable((int&)(BaseSound::state_), ObjectDirection::ToClient, new NetworkCallback<BaseSound>(static_cast<BaseSound*>(this), &BaseSound::stateChanged)); |
---|
[6307] | 63 | registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback<BaseSound>(static_cast<BaseSound*>(this), &BaseSound::pitchChanged)); |
---|
| 64 | } |
---|
[3060] | 65 | |
---|
[5896] | 66 | void WorldSound::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[3060] | 67 | { |
---|
[5896] | 68 | SUPER(WorldSound, XMLPort, xmlelement, mode); |
---|
[6117] | 69 | BaseSound::XMLPortExtern(xmlelement, mode); |
---|
[3060] | 70 | } |
---|
| 71 | |
---|
[5896] | 72 | void WorldSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[3060] | 73 | { |
---|
[5896] | 74 | SUPER(WorldSound, XMLEventPort, xmlelement, mode); |
---|
| 75 | XMLPortEventState(WorldSound, BaseObject, "play", play, xmlelement, mode); |
---|
[3060] | 76 | } |
---|
| 77 | |
---|
[6322] | 78 | void WorldSound::initialiseSource() |
---|
| 79 | { |
---|
| 80 | BaseSound::initialiseSource(); |
---|
| 81 | this->tick(0); // update position, orientation and velocity |
---|
| 82 | } |
---|
| 83 | |
---|
[5896] | 84 | void WorldSound::tick(float dt) |
---|
| 85 | { |
---|
[5899] | 86 | if (alIsSource(this->audioSource_)) |
---|
[5896] | 87 | { |
---|
| 88 | const Vector3& pos = this->getWorldPosition(); |
---|
[5899] | 89 | alSource3f(this->audioSource_, AL_POSITION, pos.x, pos.y, pos.z); |
---|
[3060] | 90 | ALenum error = alGetError(); |
---|
[5896] | 91 | if (error == AL_INVALID_VALUE) |
---|
[3060] | 92 | COUT(2) << "Sound: OpenAL: Invalid sound position" << std::endl; |
---|
| 93 | |
---|
[5896] | 94 | const Vector3& vel = this->getVelocity(); |
---|
[5899] | 95 | alSource3f(this->audioSource_, AL_VELOCITY, vel.x, vel.y, vel.z); |
---|
[3060] | 96 | error = alGetError(); |
---|
[5896] | 97 | if (error == AL_INVALID_VALUE) |
---|
[3060] | 98 | COUT(2) << "Sound: OpenAL: Invalid sound velocity" << std::endl; |
---|
| 99 | |
---|
[6269] | 100 | const Vector3& direction = -this->getWorldOrientation().zAxis(); |
---|
| 101 | alSource3f(this->audioSource_, AL_DIRECTION, direction.x, direction.y, direction.z); |
---|
[3060] | 102 | error = alGetError(); |
---|
[5896] | 103 | if (error == AL_INVALID_VALUE) |
---|
[3060] | 104 | COUT(2) << "Sound: OpenAL: Invalid sound direction" << std::endl; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
[6117] | 108 | void WorldSound::changedActivity() |
---|
| 109 | { |
---|
| 110 | SUPER(WorldSound, changedActivity); |
---|
| 111 | if (this->isActive()) |
---|
| 112 | this->play(); |
---|
| 113 | else |
---|
| 114 | this->stop(); |
---|
| 115 | } |
---|
[6204] | 116 | |
---|
[6186] | 117 | float WorldSound::getVolumeGain() |
---|
| 118 | { |
---|
| 119 | return SoundManager::getInstance().getVolume(SoundType::effects); |
---|
| 120 | } |
---|
[5896] | 121 | } |
---|