Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound2/src/orxonox/sound/SoundManager.cc @ 3050

Last change on this file since 3050 was 3013, checked in by bknecht, 16 years ago

merged sound changes into sound2 use sound2 from now on

  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/buildsystem/src/sound/SoundManager.cc1874-2276,​2278-2400
    /code/branches/buildsystem2/src/sound/SoundManager.cc2506-2658
    /code/branches/buildsystem3/src/sound/SoundManager.cc2662-2708
    /code/branches/ceguilua/src/sound/SoundManager.cc1802-1808
    /code/branches/core3/src/sound/SoundManager.cc1572-1739
    /code/branches/gcc43/src/sound/SoundManager.cc1580
    /code/branches/gui/src/sound/SoundManager.cc1635-1723
    /code/branches/input/src/sound/SoundManager.cc1629-1636
    /code/branches/lodfinal/src/sound/SoundManager.cc2372-2411
    /code/branches/miniprojects/src/sound/SoundManager.cc2754-2824
    /code/branches/network/src/sound/SoundManager.cc2356
    /code/branches/network64/src/sound/SoundManager.cc2210-2355
    /code/branches/objecthierarchy/src/sound/SoundManager.cc1911-2085,​2100,​2110-2169
    /code/branches/objecthierarchy2/src/sound/SoundManager.cc2171-2479
    /code/branches/overlay/src/sound/SoundManager.cc2117-2385
    /code/branches/physics/src/sound/SoundManager.cc1912-2055,​2107-2439
    /code/branches/physics_merge/src/sound/SoundManager.cc2436-2457
    /code/branches/pickups/src/sound/SoundManager.cc1926-2086,​2127
    /code/branches/pickups2/src/sound/SoundManager.cc2107-2497
    /code/branches/presentation/src/sound/SoundManager.cc2369-2652,​2654-2660
    /code/branches/questsystem/src/sound/SoundManager.cc1894-2088
    /code/branches/questsystem2/src/sound/SoundManager.cc2107-2259
    /code/branches/script_trigger/src/sound/SoundManager.cc1295-1953,​1955
    /code/branches/weapon/src/sound/SoundManager.cc1925-2094
    /code/branches/weapon2/src/sound/SoundManager.cc2107-2488
File size: 5.1 KB
RevLine 
[2899]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
[2932]29#include <AL/alut.h>
[2899]30
31#include "orxonox/CameraManager.h"
[2930]32#include "orxonox/objects/worldentities/Camera.h"
33#include "util/Math.h"
[2931]34#include "SoundBase.h"
[2899]35#include "SoundManager.h"
36
37namespace orxonox
38{
[2966]39    ALCdevice* SoundManager::device_s = NULL;
40
[2899]41    /**
42     * Default constructor
43     */
44    SoundManager::SoundManager()
45    {
[2998]46        this->soundavailable_ = true;
[2966]47        if(!alutInitWithoutContext(NULL,NULL))
48        {
[2984]49            COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
[2998]50            this->soundavailable_ = false;
[2932]51        }
[2966]52        else
53        {
[2984]54            COUT(4) << "Sound: OpenAL ALUT version:" << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl;
55            COUT(4) << "Sound: OpenAL ALUT supported MIME types:" << alutGetMIMETypes(ALUT_LOADER_BUFFER) << std::endl;
[2966]56            if(SoundManager::device_s == NULL)
57            {
[2984]58                COUT(3) << "Sound: OpenAL: Open sound device..." << std::endl;
[2966]59                SoundManager::device_s = alcOpenDevice(NULL);
60            }
[2950]61
[2966]62            if(SoundManager::device_s == NULL)
63            {
[2984]64                COUT(2) << "Sound: OpenAL: Could not open sound device" << std::endl;
[2998]65                this->soundavailable_ = false;
[2966]66            }
67            else
68            {
[2984]69                COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl;
[2966]70                this->context_ = alcCreateContext(SoundManager::device_s, NULL);
71                if(this->context_ == NULL)
72                {
[2984]73                    COUT(2) << "Sound: OpenAL: Could not create sound context" << std::endl;
[2998]74                    this->soundavailable_ = false;
[2966]75                }
76                else
77                {
[2982]78                    if(alcMakeContextCurrent(this->context_) == AL_TRUE)
[2984]79                        COUT(3) << "Sound: OpenAL: Context " << this->context_ << "loaded" << std::endl;
[2966]80                }
81            }
82        }
[2899]83    }
84
[2966]85    SoundManager::~SoundManager()
86    {
87        alcDestroyContext(this->context_);
88        alcCloseDevice(SoundManager::device_s);
89        alutExit();
90    }
91
[2899]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        }
[2966]114
[2899]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        // update listener position
127        Camera* camera = CameraManager::getInstance().getActiveCamera();
[2950]128        if(camera == NULL) return;
[2899]129        Vector3 pos = camera->getPosition();
130        alListener3f(AL_POSITION, pos.x, pos.y, pos.z);
131        ALenum error = alGetError();
[2931]132        if(error == AL_INVALID_VALUE)
[2984]133            COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl;
[2899]134
135        // update listener orientation
136        Quaternion orient = camera->getOrientation();
137        Vector3 up = orient.xAxis(); // just a wild guess
[2930]138        Vector3 at = orient.zAxis();
[2899]139
[2931]140        ALfloat orientation[6] = { at.x, at.y, at.z, 
[2930]141                                 up.x, up.y, up.z };
[2899]142
143        alListenerfv(AL_POSITION, orientation);
144        error = alGetError();
145        if(error == AL_INVALID_VALUE)
[2984]146            COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
[2899]147
148        // update sounds
149        for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)
150            (*i)->update();
151    }
[2998]152   
153    /**
154    * Check if sound is available
155    */
156    bool SoundManager::isSoundAvailable()
157    {
158        return this->soundavailable_;
159    }
[2966]160}
Note: See TracBrowser for help on using the repository browser.