Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Sound: Added building things, corrected some typos

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