Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/sound/SoundManager.cc @ 3133

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

Reduced header file dependencies in the sound classes.

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