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