Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Sound: implemented sample file loading via alut

File size: 3.6 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    /**
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        if(!alutInit(NULL,NULL)) {
60            COUT(2) << "OpenAL ALUT: " << alutGetErrorString(alutGetError());
61        }
62    }
63
64    /**
65     * Add a SoundBase object to the list. Every SoundBase object should be in
66     * this list.
67     *
68     * @param sound Pointer to the SoundBase object to add
69     */
70    void SoundManager::addSound(SoundBase* sound)
71    {
72        this->soundlist_.push_back(sound);
73    }
74
75    /**
76     * Remove a SoundBase object from the list and destroy it.
77     */
78    void SoundManager::removeSound(SoundBase* sound)
79    {
80        std::list<SoundBase*>::iterator pos = this->soundlist_.end();
81        for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)
82        {
83            if((*i) == sound)
84                pos = i;
85        }
86       
87        delete (*pos);
88        this->soundlist_.erase(pos);
89    }
90
91    /**
92     * Tick function, updates listener and registred SoundBase objects
93     *
94     * @param dt @see Orxonox::Tickable
95     */
96    void SoundManager::tick(float dt)
97    {
98        // update listener position
99        Camera* camera = CameraManager::getInstance().getActiveCamera();
100        Vector3 pos = camera->getPosition();
101        alListener3f(AL_POSITION, pos.x, pos.y, pos.z);
102        ALenum error = alGetError();
103        if(error == AL_INVALID_VALUE)
104            COUT(2) << "OpenAL: Invalid listener position" << std::endl;
105
106        // update listener orientation
107        Quaternion orient = camera->getOrientation();
108        Vector3 up = orient.xAxis(); // just a wild guess
109        Vector3 at = orient.zAxis();
110
111        ALfloat orientation[6] = { at.x, at.y, at.z, 
112                                 up.x, up.y, up.z };
113
114        alListenerfv(AL_POSITION, orientation);
115        error = alGetError();
116        if(error == AL_INVALID_VALUE)
117            COUT(2) << "OpenAL: Invalid listener orientation" << std::endl;
118
119        // update sounds
120        for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)
121            (*i)->update();
122    }
123
124} 
Note: See TracBrowser for help on using the repository browser.