Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/sound/SoundManager.cc @ 3203

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

Merged pch branch back to trunk.

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