[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: |
---|
| 23 | * Erwin 'vaiursch' Herrsche |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[3196] | 29 | #include "SoundManager.h" |
---|
| 30 | |
---|
[3060] | 31 | #include <AL/alut.h> |
---|
| 32 | |
---|
[5929] | 33 | #include "util/Exception.h" |
---|
[3196] | 34 | #include "util/Math.h" |
---|
[5929] | 35 | #include "util/ScopeGuard.h" |
---|
| 36 | #include "core/GameMode.h" |
---|
| 37 | #include "core/ScopedSingletonManager.h" |
---|
[3060] | 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
[3370] | 41 | SoundManager* SoundManager::singletonPtr_s = NULL; |
---|
[5929] | 42 | ManageScopedSingleton(SoundManager, ScopeID::Graphics, true); |
---|
[3060] | 43 | |
---|
| 44 | SoundManager::SoundManager() |
---|
| 45 | { |
---|
[5929] | 46 | if (!alutInitWithoutContext(NULL,NULL)) |
---|
| 47 | ThrowException(InitialisationFailed, "OpenAL ALUT error: " << alutGetErrorString(alutGetError())); |
---|
| 48 | Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); |
---|
| 49 | |
---|
| 50 | COUT(3) << "OpenAL: Opening sound device..." << std::endl; |
---|
| 51 | this->device_ = alcOpenDevice(NULL); |
---|
| 52 | if (this->device_ == NULL) |
---|
[3060] | 53 | { |
---|
[5929] | 54 | COUT(0) << "OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl; |
---|
| 55 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 56 | COUT(0) << "Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; |
---|
| 57 | #endif |
---|
| 58 | ThrowException(InitialisationFailed, "OpenAL error: Could not open sound device."); |
---|
[3060] | 59 | } |
---|
[5929] | 60 | Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); |
---|
| 61 | |
---|
| 62 | COUT(3) << "OpenAL: Sound device opened" << std::endl; |
---|
| 63 | this->context_ = alcCreateContext(this->device_, NULL); |
---|
| 64 | if (this->context_ == NULL) |
---|
| 65 | ThrowException(InitialisationFailed, "OpenAL error: Could not create sound context"); |
---|
| 66 | Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_); |
---|
| 67 | |
---|
| 68 | if (alcMakeContextCurrent(this->context_) == AL_TRUE) |
---|
| 69 | COUT(3) << "OpenAL: Context " << this->context_ << " loaded" << std::endl; |
---|
| 70 | |
---|
| 71 | COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl; |
---|
| 72 | |
---|
| 73 | const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER); |
---|
| 74 | if (str == NULL) |
---|
| 75 | COUT(2) << "OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl; |
---|
[3060] | 76 | else |
---|
[5929] | 77 | COUT(4) << "OpenAL ALUT supported MIME types: " << str << std::endl; |
---|
[3060] | 78 | |
---|
[5929] | 79 | GameMode::setPlaysSound(true); |
---|
| 80 | // Disarm guards |
---|
| 81 | alutExitGuard.Dismiss(); |
---|
| 82 | closeDeviceGuard.Dismiss(); |
---|
| 83 | desroyContextGuard.Dismiss(); |
---|
[3060] | 84 | } |
---|
| 85 | |
---|
| 86 | SoundManager::~SoundManager() |
---|
| 87 | { |
---|
[5929] | 88 | GameMode::setPlaysSound(false); |
---|
[3060] | 89 | alcDestroyContext(this->context_); |
---|
[3280] | 90 | alcCloseDevice(this->device_); |
---|
[3060] | 91 | alutExit(); |
---|
| 92 | } |
---|
| 93 | |
---|
[5929] | 94 | void SoundManager::setListenerPosition(const Vector3& position) |
---|
[3060] | 95 | { |
---|
[5929] | 96 | alListener3f(AL_POSITION, position.x, position.y, position.z); |
---|
| 97 | ALenum error = alGetError(); |
---|
| 98 | if (error == AL_INVALID_VALUE) |
---|
| 99 | COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl; |
---|
[3060] | 100 | } |
---|
| 101 | |
---|
[5929] | 102 | void SoundManager::setListenerOrientation(const Quaternion& orientation) |
---|
[3060] | 103 | { |
---|
[5929] | 104 | // update listener orientation |
---|
| 105 | Vector3 up = orientation.xAxis(); // just a wild guess |
---|
| 106 | Vector3 at = orientation.zAxis(); |
---|
[3060] | 107 | |
---|
[5929] | 108 | ALfloat orient[6] = { at.x, at.y, at.z, |
---|
| 109 | up.x, up.y, up.z }; |
---|
[3060] | 110 | |
---|
[5929] | 111 | alListenerfv(AL_POSITION, orient); |
---|
[3060] | 112 | ALenum error = alGetError(); |
---|
[5929] | 113 | if (error == AL_INVALID_VALUE) |
---|
[3060] | 114 | COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; |
---|
| 115 | } |
---|
| 116 | } |
---|