Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/orxonox/sound/SoundManager.cc @ 5881

Last change on this file since 5881 was 5881, checked in by rgrieder, 15 years ago

Fixed sound problem in Pong by correcting the assumptions that you will always have sound.

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
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 "SoundManager.h"
30
31#include <AL/alut.h>
32
33#include "util/Exception.h"
34#include "util/Math.h"
35#include "util/ScopeGuard.h"
36#include "core/GameMode.h"
37#include "core/ScopedSingletonManager.h"
38#include "CameraManager.h"
39#include "graphics/Camera.h"
40#include "SoundBase.h"
41
42namespace orxonox
43{
44    SoundManager* SoundManager::singletonPtr_s = NULL;
45    ManageScopedSingleton(SoundManager, ScopeID::Graphics, true);
46
47    SoundManager::SoundManager()
48    {
49        if (!alutInitWithoutContext(NULL,NULL))
50            ThrowException(InitialisationFailed, "OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
51        Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit);
52
53        COUT(3) << "OpenAL: Opening sound device..." << std::endl;
54        this->device_ = alcOpenDevice(NULL);
55        if (this->device_ == NULL)
56            ThrowException(InitialisationFailed, "OpenAL error: Could not open sound device.");
57        Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_);
58
59        COUT(3) << "OpenAL: Sound device opened" << std::endl;
60        this->context_ = alcCreateContext(this->device_, NULL);
61        if (this->context_ == NULL)
62            ThrowException(InitialisationFailed, "OpenAL error: Could not create sound context");
63        Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_);
64
65        if (alcMakeContextCurrent(this->context_) == AL_TRUE)
66            COUT(3) << "OpenAL: Context " << this->context_ << " loaded" << std::endl;
67
68        COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl;
69
70        const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER);
71        if (str == NULL)
72            COUT(2) << "OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl;
73        else
74            COUT(4) << "OpenAL ALUT supported MIME types: " << str << std::endl;
75
76        GameMode::setPlaysSound(true);
77        // Disarm guards
78        alutExitGuard.Dismiss();
79        closeDeviceGuard.Dismiss();
80        desroyContextGuard.Dismiss();
81    }
82
83    SoundManager::~SoundManager()
84    {
85        GameMode::setPlaysSound(false);
86        alcDestroyContext(this->context_);
87        alcCloseDevice(this->device_);
88        alutExit();
89    }
90
91    /**
92     * Add a SoundBase object to the list. Every SoundBase object should be in
93     * this list.
94     *
95     * @param sound Pointer to the SoundBase object to add
96     */
97    void SoundManager::addSound(SoundBase* sound)
98    {
99        this->soundlist_.push_back(sound);
100    }
101
102    /**
103     * Remove a SoundBase object from the list and destroy it.
104     */
105    void SoundManager::removeSound(SoundBase* sound)
106    {
107        std::list<SoundBase*>::iterator pos = this->soundlist_.end();
108        for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)
109        {
110            if((*i) == sound)
111                pos = i;
112        }
113
114        delete (*pos);
115        this->soundlist_.erase(pos);
116    }
117
118    /**
119     * Tick function, updates listener and registred SoundBase objects
120     *
121     * @param dt @see Orxonox::Tickable
122     */
123    void SoundManager::tick(float dt)
124    {
125        if (!CameraManager::getInstancePtr())
126            return;
127
128        // update listener position
129        Camera* camera = CameraManager::getInstance().getActiveCamera();
130        if(camera == NULL) return;
131        Vector3 pos = camera->getPosition();
132        alListener3f(AL_POSITION, pos.x, pos.y, pos.z);
133        ALenum error = alGetError();
134        if(error == AL_INVALID_VALUE)
135            COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl;
136
137        // update listener orientation
138        const Quaternion& orient = camera->getOrientation();
139        Vector3 up = orient.xAxis(); // just a wild guess
140        Vector3 at = orient.zAxis();
141
142        ALfloat orientation[6] = { at.x, at.y, at.z,
143                                 up.x, up.y, up.z };
144
145        alListenerfv(AL_POSITION, orientation);
146        error = alGetError();
147        if(error == AL_INVALID_VALUE)
148            COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
149
150        // update sounds
151        for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)
152            (*i)->update();
153    }
154
155    /**
156    * Check if sound is available
157    */
158    bool SoundManager::isSoundAvailable()
159    {
160        return this->soundavailable_;
161    }
162}
Note: See TracBrowser for help on using the repository browser.