Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

SoundManager.cc: alutGetMIMETypes might return NULL instead of const char* —> Display error and don't feed NULL-string or all hell might break loose.

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