Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sound/src/orxonox/sound/SoundBase.cc @ 2966

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

moved files, modified to support multiple contexts

  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/buildsystem/src/sound/SoundBase.cc1874-2276,​2278-2400
    /code/branches/buildsystem2/src/sound/SoundBase.cc2506-2658
    /code/branches/buildsystem3/src/sound/SoundBase.cc2662-2708
    /code/branches/ceguilua/src/sound/SoundBase.cc1802-1808
    /code/branches/core3/src/sound/SoundBase.cc1572-1739
    /code/branches/gcc43/src/sound/SoundBase.cc1580
    /code/branches/gui/src/sound/SoundBase.cc1635-1723
    /code/branches/input/src/sound/SoundBase.cc1629-1636
    /code/branches/lodfinal/src/sound/SoundBase.cc2372-2411
    /code/branches/miniprojects/src/sound/SoundBase.cc2754-2824
    /code/branches/network/src/sound/SoundBase.cc2356
    /code/branches/network64/src/sound/SoundBase.cc2210-2355
    /code/branches/objecthierarchy/src/sound/SoundBase.cc1911-2085,​2100,​2110-2169
    /code/branches/objecthierarchy2/src/sound/SoundBase.cc2171-2479
    /code/branches/overlay/src/sound/SoundBase.cc2117-2385
    /code/branches/physics/src/sound/SoundBase.cc1912-2055,​2107-2439
    /code/branches/physics_merge/src/sound/SoundBase.cc2436-2457
    /code/branches/pickups/src/sound/SoundBase.cc1926-2086,​2127
    /code/branches/pickups2/src/sound/SoundBase.cc2107-2497
    /code/branches/presentation/src/sound/SoundBase.cc2369-2652,​2654-2660
    /code/branches/questsystem/src/sound/SoundBase.cc1894-2088
    /code/branches/questsystem2/src/sound/SoundBase.cc2107-2259
    /code/branches/script_trigger/src/sound/SoundBase.cc1295-1953,​1955
    /code/branches/weapon/src/sound/SoundBase.cc1925-2094
    /code/branches/weapon2/src/sound/SoundBase.cc2107-2488
File size: 4.5 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()
37    {
38        this->source_ = 0;
39        this->buffer_ = 0;
40        this->entity_ = NULL;
41    }
42
43    SoundBase::SoundBase(WorldEntity* entity)
44    {
45        this->source_ = 0;
46        this->buffer_ = 0;
47        this->entity_ = entity;
48    }
49
50    SoundBase::~SoundBase()
51    {
52        alSourcei(this->source_, AL_BUFFER, 0);
53        alDeleteSources(1, &this->source_);
54        alDeleteBuffers(1, &this->buffer_);
55    }
56
57    void SoundBase::attachToEntity(WorldEntity* entity)
58    {
59        this->entity_ = entity;
60        this->update();
61    }
62
63    void SoundBase::update() {
64        if(this->entity_ != NULL && alIsSource(this->source_)) {
65            Vector3 pos = this->entity_->getPosition();
66            alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z);
67            ALenum error = alGetError();
68            if(error == AL_INVALID_VALUE)
69                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
70
71            Vector3 vel = this->entity_->getVelocity();
72            alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z);
73            error = alGetError();
74            if(error == AL_INVALID_VALUE)
75                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
76
77            Quaternion orient = this->entity_->getOrientation();
78            Vector3 at = orient.zAxis();
79            alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z);
80            error = alGetError();
81            if(error == AL_INVALID_VALUE)
82                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
83        }
84    }
85
86    void SoundBase::play(bool loop) {
87        if(alIsSource(this->source_)) {
88            if(loop)
89                alSourcei(this->source_, AL_LOOPING, AL_TRUE);
90            else
91                alSourcei(this->source_, AL_LOOPING, AL_FALSE);
92            alSourcePlay(this->source_);
93        }
94    }
95
96    void SoundBase::stop() {
97        if(alIsSource(this->source_)) {
98            alSourceStop(this->source_);
99        }
100    }
101
102    void SoundBase::pause() {
103        if(alIsSource(this->source_)) {
104            alSourcePause(this->source_);
105        }
106    }
107
108    bool SoundBase::isPlaying() {
109        if(alIsSource(this->source_)) {
110            return getSourceState() == AL_PLAYING;
111        }
112        return false;
113    }
114
115    bool SoundBase::isPaused() {
116        if(alIsSource(this->source_)) {
117            return getSourceState() == AL_PAUSED;
118        }
119        return true;
120    }
121
122    bool SoundBase::isStopped() {
123        if(alIsSource(this->source_)) {
124            return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
125        }
126        return true;
127    }
128
129    bool SoundBase::loadFile(std::string filename) {
130        COUT(3) << "OpenAL ALUT: loading file " << filename << std::endl;
131        this->buffer_ = alutCreateBufferFromFile(filename.c_str());
132        if(this->buffer_ == AL_NONE) {
133            COUT(2) << "OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
134            return false;
135        }
136
137        alGenSources(1, &this->source_);
138        alSourcei(this->source_, AL_BUFFER, this->buffer_);
139        if(alGetError() != AL_NO_ERROR) {
140            COUT(2) << "OpenAL: Error loading sample file" << std::endl;
141            return false;
142        }
143        return true;
144    }
145
146    ALint SoundBase::getSourceState() {
147        ALint state;
148        alGetSourcei(this->source_, AL_SOURCE_STATE, &state);
149        return state;
150    }
151} // namespace: orxonox
Note: See TracBrowser for help on using the repository browser.