Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/orxonox/sound/SoundManager.cc @ 5878

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

Added GameMode::playsSound(). This function checks whether sound is available. You MUST NEVER assume that the SoundManager exists and ALWAYS check it (even if graphics is all loaded).
Also cleaned SoundManager c'tor to use Exceptions.

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