Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/sound/WorldSound.cc @ 7922

Last change on this file since 7922 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 4.5 KB
RevLine 
[3060]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:
[5896]23 *      Erwin 'vaiursch' Herrsche
24 *      Reto Grieder
[3060]25 *   Co-authors:
26 *      ...
27 *
28 */
[3196]29
[5896]30#include "WorldSound.h"
[3196]31
[3060]32#include <AL/alut.h>
[3196]33#include "util/Math.h"
[5914]34#include "core/CoreIncludes.h"
[5896]35#include "core/EventIncludes.h"
36#include "core/XMLPort.h"
[6417]37#include "Scene.h"
38#include "SoundManager.h"
[3060]39
[5738]40namespace orxonox
[3060]41{
[5896]42    CreateFactory(WorldSound);
43
44    WorldSound::WorldSound(BaseObject* creator)
45        : StaticEntity(creator)
[3060]46    {
[5896]47        RegisterObject(WorldSound);
[6417]48        // WorldSound buffers should be pooled when they're not used anymore
49        this->bPooling_ = true;
50        this->registerVariables();
[5896]51    }
[3060]52
[6417]53    void WorldSound::registerVariables()
[5896]54    {
[6417]55        registerVariable(volume_,   ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::volumeChanged));
56        registerVariable(source_,   ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::sourceChanged));
57        registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::loopingChanged));
58        registerVariable(pitch_,    ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::pitchChanged));
[7163]59        registerVariable((uint8_t&)(BaseSound::state_), ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::stateChanged));
[3060]60    }
61
[5896]62    void WorldSound::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[3060]63    {
[5896]64        SUPER(WorldSound, XMLPort, xmlelement, mode);
[6417]65        BaseSound::XMLPortExtern(xmlelement, mode);
[3060]66    }
67
[5896]68    void WorldSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
[3060]69    {
[5896]70        SUPER(WorldSound, XMLEventPort, xmlelement, mode);
71        XMLPortEventState(WorldSound, BaseObject, "play", play, xmlelement, mode);
[3060]72    }
73
[6417]74    void WorldSound::initialiseSource()
75    {
76        BaseSound::initialiseSource();
77        if (this->getScene())
78        {
79            float refDist = this->getScene()->getSoundReferenceDistance();
80            alSourcef(this->audioSource_, AL_REFERENCE_DISTANCE, refDist);
81            // TODO: 500 is very magical here. Derive something better
82            alSourcef(this->audioSource_, AL_MAX_DISTANCE, refDist * 500);
83        }
84        this->tick(0); // update position, orientation and velocity
85    }
86
[5896]87    void WorldSound::tick(float dt)
88    {
[5899]89        if (alIsSource(this->audioSource_))
[5896]90        {
91            const Vector3& pos = this->getWorldPosition();
[5899]92            alSource3f(this->audioSource_, AL_POSITION, pos.x, pos.y, pos.z);
[3060]93            ALenum error = alGetError();
[5896]94            if (error == AL_INVALID_VALUE)
[3060]95                COUT(2) << "Sound: OpenAL: Invalid sound position" << std::endl;
96
[5896]97            const Vector3& vel = this->getVelocity();
[5899]98            alSource3f(this->audioSource_, AL_VELOCITY, vel.x, vel.y, vel.z);
[3060]99            error = alGetError();
[5896]100            if (error == AL_INVALID_VALUE)
[3060]101                COUT(2) << "Sound: OpenAL: Invalid sound velocity" << std::endl;
102
[6417]103            const Vector3& direction = -this->getWorldOrientation().zAxis();
104            alSource3f(this->audioSource_, AL_DIRECTION, direction.x, direction.y, direction.z);
[3060]105            error = alGetError();
[5896]106            if (error == AL_INVALID_VALUE)
[3060]107                COUT(2) << "Sound: OpenAL: Invalid sound direction" << std::endl;
108        }
109    }
110
[6417]111    void WorldSound::changedActivity()
112    {
113        SUPER(WorldSound, changedActivity);
114        if (this->isActive())
115            this->play();
116        else
117            this->stop();
118    }
119
120    float WorldSound::getRealVolume()
121    {
122        assert(GameMode::playsSound());
123        return SoundManager::getInstance().getRealVolume(SoundType::Effects);
124    }
[5896]125}
Note: See TracBrowser for help on using the repository browser.