Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged resource branch back to the trunk. Changes:

  • Automated graphics loading by evaluating whether a GameState requires it
  • Using native Tcl library (x3n)

Windows users: Update your dependency package!

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