Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound/src/sound/SoundManager.cc @ 2931

Last change on this file since 2931 was 2931, checked in by erwin, 15 years ago

Sound: SoundBase complete

File size: 4.0 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 <OgreSceneNode.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    /**
40     * Static function to get the singleton instance of SoundManager.
41     *
42     * @return The singleton instance
43     */
44    SoundManager* SoundManager::instance()
45    {
46        if(SoundManager::singleton_ == NULL)
47        {
48            SoundManager::singleton_ = new SoundManager();
49        }
50
51        return SoundManager::singleton_;
52    }
53
54    /**
55     * Default constructor
56     */
57    SoundManager::SoundManager()
58    {
59        // OpenAL device and context creation
60        this->device_ = alcOpenDevice(NULL); // we operate on the default sound device
61        if(this->device_)
62            COUT(2) << "OpenAL: Could not create sound device, there will be no sound!" << std::endl;
63
64        this->context_ = alcCreateContext(this->device_, NULL);
65        alcMakeContextCurrent(this->context_);
66        ALenum error = alcGetError(this->device_);
67        if(error != ALC_NO_ERROR)
68            COUT(2) << "OpenAL: Could not create sound context." << std::endl;
69
70    }
71
72    /**
73     * Add a SoundBase object to the list. Every SoundBase object should be in
74     * this list.
75     *
76     * @param sound Pointer to the SoundBase object to add
77     */
78    void SoundManager::addSound(SoundBase* sound)
79    {
80        this->soundlist_.push_back(sound);
81    }
82
83    /**
84     * Remove a SoundBase object from the list and destroy it.
85     */
86    void SoundManager::removeSound(SoundBase* sound)
87    {
88        std::list<SoundBase*>::iterator pos = this->soundlist_.end();
89        for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)
90        {
91            if((*i) == sound)
92                pos = i;
93        }
94       
95        delete (*pos);
96        this->soundlist_.erase(pos);
97    }
98
99    /**
100     * Tick function, updates listener and registred SoundBase objects
101     *
102     * @param dt @see Orxonox::Tickable
103     */
104    void SoundManager::tick(float dt)
105    {
106        // update listener position
107        Camera* camera = CameraManager::getInstance().getActiveCamera();
108        Vector3 pos = camera->getPosition();
109        alListener3f(AL_POSITION, pos.x, pos.y, pos.z);
110        ALenum error = alGetError();
111        if(error == AL_INVALID_VALUE)
112            COUT(2) << "OpenAL: Invalid listener position" << std::endl;
113
114        // update listener orientation
115        Quaternion orient = camera->getOrientation();
116        Vector3 up = orient.xAxis(); // just a wild guess
117        Vector3 at = orient.zAxis();
118
119        ALfloat orientation[6] = { at.x, at.y, at.z, 
120                                 up.x, up.y, up.z };
121
122        alListenerfv(AL_POSITION, orientation);
123        error = alGetError();
124        if(error == AL_INVALID_VALUE)
125            COUT(2) << "OpenAL: Invalid listener orientation" << std::endl;
126
127        // update sounds
128        for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)
129            (*i)->update();
130    }
131
132} 
Note: See TracBrowser for help on using the repository browser.