Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound/src/sound/SoundBase.cc @ 2931

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

Sound: SoundBase complete

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 "orxonox/objects/worldentities/WorldEntity.h"
30#include "util/Math.h"
31#include "SoundManager.h"
32#include "SoundBase.h"
33
34namespace orxonox
35{
36    SoundBase::SoundBase(WorldEntity* entity)
37    {
38        this->source_ = 0;
39        this->buffer_ = 0;
40        this->entity_ = entity;
41
42        if(SoundBase::soundmanager_s == NULL)
43            SoundBase::soundmanager_s = SoundManager::instance();
44    }
45
46    void SoundBase::attachToEntity(WorldEntity* entity)
47    {
48        this->entity_ = entity;
49        this->update();
50    }
51
52    void SoundBase::update() {
53        if(alIsSource(this->source_)) {
54            Vector3 pos = this->entity_->getPosition();
55            alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z);
56            ALenum error = alGetError();
57            if(error == AL_INVALID_VALUE)
58                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
59
60            Vector3 vel = this->entity_->getVelocity();
61            alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z);
62            error = alGetError();
63            if(error == AL_INVALID_VALUE)
64                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
65
66            Quaternion orient = this->entity_->getOrientation();
67            Vector3 at = orient.zAxis();
68            alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z);
69            error = alGetError();
70            if(error == AL_INVALID_VALUE)
71                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
72        }
73    }
74
75    void SoundBase::play(bool loop) {
76        if(alIsSource(this->source_)) {
77            if(loop)
78                alSourcei(this->source_, AL_LOOPING, AL_TRUE);
79            else
80                alSourcei(this->source_, AL_LOOPING, AL_FALSE);
81            alSourcePlay(this->source_);
82        }
83    }
84
85    void SoundBase::stop() {
86        if(alIsSource(this->source_)) {
87            alSourceStop(this->source_);
88        }
89    }
90
91    void SoundBase::pause() {
92        if(alIsSource(this->source_)) {
93            alSourcePause(this->source_);
94        }
95    }
96
97    bool SoundBase::isPlaying() {
98        if(alIsSource(this->source_)) {
99            return getSourceState() == AL_PLAYING;
100        }
101    }
102
103    bool SoundBase::isPaused() {
104        if(alIsSource(this->source_)) {
105            return getSourceState() == AL_PAUSED;
106        }
107    }
108
109    bool SoundBase::isStopped() {
110        if(alIsSource(this->source_)) {
111            return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
112        }
113    }
114
115    ALint SoundBase::getSourceState() {
116        ALint state;
117        alGetSourcei(this->source_, AL_SOURCE_STATE, &state);
118        return state;
119    }
120} // namespace: orxonox
Note: See TracBrowser for help on using the repository browser.