[2899] | 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 | |
---|
[2932] | 29 | #include <AL/alut.h> |
---|
[2899] | 30 | |
---|
| 31 | #include "orxonox/CameraManager.h" |
---|
[2930] | 32 | #include "orxonox/objects/worldentities/Camera.h" |
---|
| 33 | #include "util/Math.h" |
---|
[2931] | 34 | #include "SoundBase.h" |
---|
[2899] | 35 | #include "SoundManager.h" |
---|
| 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
[2966] | 39 | ALCdevice* SoundManager::device_s = NULL; |
---|
| 40 | |
---|
[2899] | 41 | /** |
---|
| 42 | * Default constructor |
---|
| 43 | */ |
---|
| 44 | SoundManager::SoundManager() |
---|
| 45 | { |
---|
[2966] | 46 | if(!alutInitWithoutContext(NULL,NULL)) |
---|
| 47 | { |
---|
[2984] | 48 | COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
[2932] | 49 | } |
---|
[2966] | 50 | else |
---|
| 51 | { |
---|
[2984] | 52 | COUT(4) << "Sound: OpenAL ALUT version:" << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl; |
---|
| 53 | COUT(4) << "Sound: OpenAL ALUT supported MIME types:" << alutGetMIMETypes(ALUT_LOADER_BUFFER) << std::endl; |
---|
[2966] | 54 | if(SoundManager::device_s == NULL) |
---|
| 55 | { |
---|
[2984] | 56 | COUT(3) << "Sound: OpenAL: Open sound device..." << std::endl; |
---|
[2966] | 57 | SoundManager::device_s = alcOpenDevice(NULL); |
---|
| 58 | } |
---|
[2950] | 59 | |
---|
[2966] | 60 | if(SoundManager::device_s == NULL) |
---|
| 61 | { |
---|
[2984] | 62 | COUT(2) << "Sound: OpenAL: Could not open sound device" << std::endl; |
---|
[2966] | 63 | } |
---|
| 64 | else |
---|
| 65 | { |
---|
[2984] | 66 | COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl; |
---|
[2966] | 67 | this->context_ = alcCreateContext(SoundManager::device_s, NULL); |
---|
| 68 | if(this->context_ == NULL) |
---|
| 69 | { |
---|
[2984] | 70 | COUT(2) << "Sound: OpenAL: Could not create sound context" << std::endl; |
---|
[2966] | 71 | } |
---|
| 72 | else |
---|
| 73 | { |
---|
[2982] | 74 | if(alcMakeContextCurrent(this->context_) == AL_TRUE) |
---|
[2984] | 75 | COUT(3) << "Sound: OpenAL: Context " << this->context_ << "loaded" << std::endl; |
---|
[2966] | 76 | } |
---|
| 77 | } |
---|
| 78 | } |
---|
[2899] | 79 | } |
---|
| 80 | |
---|
[2966] | 81 | SoundManager::~SoundManager() |
---|
| 82 | { |
---|
| 83 | alcDestroyContext(this->context_); |
---|
| 84 | alcCloseDevice(SoundManager::device_s); |
---|
| 85 | alutExit(); |
---|
| 86 | } |
---|
| 87 | |
---|
[2899] | 88 | /** |
---|
| 89 | * Add a SoundBase object to the list. Every SoundBase object should be in |
---|
| 90 | * this list. |
---|
| 91 | * |
---|
| 92 | * @param sound Pointer to the SoundBase object to add |
---|
| 93 | */ |
---|
| 94 | void SoundManager::addSound(SoundBase* sound) |
---|
| 95 | { |
---|
| 96 | this->soundlist_.push_back(sound); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Remove a SoundBase object from the list and destroy it. |
---|
| 101 | */ |
---|
| 102 | void SoundManager::removeSound(SoundBase* sound) |
---|
| 103 | { |
---|
| 104 | std::list<SoundBase*>::iterator pos = this->soundlist_.end(); |
---|
| 105 | for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++) |
---|
| 106 | { |
---|
| 107 | if((*i) == sound) |
---|
| 108 | pos = i; |
---|
| 109 | } |
---|
[2966] | 110 | |
---|
[2899] | 111 | delete (*pos); |
---|
| 112 | this->soundlist_.erase(pos); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * Tick function, updates listener and registred SoundBase objects |
---|
| 117 | * |
---|
| 118 | * @param dt @see Orxonox::Tickable |
---|
| 119 | */ |
---|
| 120 | void SoundManager::tick(float dt) |
---|
| 121 | { |
---|
| 122 | // update listener position |
---|
| 123 | Camera* camera = CameraManager::getInstance().getActiveCamera(); |
---|
[2950] | 124 | if(camera == NULL) return; |
---|
[2899] | 125 | Vector3 pos = camera->getPosition(); |
---|
| 126 | alListener3f(AL_POSITION, pos.x, pos.y, pos.z); |
---|
| 127 | ALenum error = alGetError(); |
---|
[2931] | 128 | if(error == AL_INVALID_VALUE) |
---|
[2984] | 129 | COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl; |
---|
[2899] | 130 | |
---|
| 131 | // update listener orientation |
---|
| 132 | Quaternion orient = camera->getOrientation(); |
---|
| 133 | Vector3 up = orient.xAxis(); // just a wild guess |
---|
[2930] | 134 | Vector3 at = orient.zAxis(); |
---|
[2899] | 135 | |
---|
[2931] | 136 | ALfloat orientation[6] = { at.x, at.y, at.z, |
---|
[2930] | 137 | up.x, up.y, up.z }; |
---|
[2899] | 138 | |
---|
| 139 | alListenerfv(AL_POSITION, orientation); |
---|
| 140 | error = alGetError(); |
---|
| 141 | if(error == AL_INVALID_VALUE) |
---|
[2984] | 142 | COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; |
---|
[2899] | 143 | |
---|
| 144 | // update sounds |
---|
| 145 | for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++) |
---|
| 146 | (*i)->update(); |
---|
| 147 | } |
---|
[2966] | 148 | } |
---|