Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2980 was 2980, checked in by erwin, 16 years ago

added config ability to sound, few other changes

  • 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: 5.0 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#include <AL/alut.h>
29#include <vorbis/vorbisfile.h>
30
31#include "orxonox/objects/worldentities/WorldEntity.h"
32#include "util/Math.h"
33#include "SoundBase.h"
34#include "SoundManager.h"
35
36namespace orxonox
37{
38    SoundManager* SoundBase::soundmanager_s = NULL;
39
40    SoundBase::SoundBase(WorldEntity* entity)
41    {
42        if(SoundBase::soundmanager_s == NULL)
43        {
44            SoundBase::soundmanager_s = new SoundManager();
45        }
46
47        this->source_ = 0;
48        this->buffer_ = 0;
49        this->entity_ = entity;
50
51        SoundBase::soundmanager_s->addSound(this);
52    }
53
54    SoundBase::~SoundBase()
55    {
56        alSourcei(this->source_, AL_BUFFER, 0);
57        alDeleteSources(1, &this->source_);
58        alDeleteBuffers(1, &this->buffer_);
59    }
60
61    void SoundBase::attachToEntity(WorldEntity* entity)
62    {
63        this->entity_ = entity;
64        this->update();
65    }
66
67    void SoundBase::update() {
68        if(this->entity_ != NULL && alIsSource(this->source_)) {
69            Vector3 pos = this->entity_->getPosition();
70            alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z);
71            ALenum error = alGetError();
72            if(error == AL_INVALID_VALUE)
73                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
74
75            Vector3 vel = this->entity_->getVelocity();
76            alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z);
77            error = alGetError();
78            if(error == AL_INVALID_VALUE)
79                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
80
81            Quaternion orient = this->entity_->getOrientation();
82            Vector3 at = orient.zAxis();
83            alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z);
84            error = alGetError();
85            if(error == AL_INVALID_VALUE)
86                COUT(2) << "OpenAL: Invalid sound position" << std::endl;
87        }
88    }
89
90    void SoundBase::play(bool loop) {
91        if(alIsSource(this->source_)) {
92            if(loop)
93                alSourcei(this->source_, AL_LOOPING, AL_TRUE);
94            else
95                alSourcei(this->source_, AL_LOOPING, AL_FALSE);
96            alSourcePlay(this->source_);
97        }
98    }
99
100    void SoundBase::stop() {
101        if(alIsSource(this->source_)) {
102            alSourceStop(this->source_);
103        }
104    }
105
106    void SoundBase::pause() {
107        if(alIsSource(this->source_)) {
108            alSourcePause(this->source_);
109        }
110    }
111
112    bool SoundBase::isPlaying() {
113        if(alIsSource(this->source_)) {
114            return getSourceState() == AL_PLAYING;
115        }
116        return false;
117    }
118
119    bool SoundBase::isPaused() {
120        if(alIsSource(this->source_)) {
121            return getSourceState() == AL_PAUSED;
122        }
123        return true;
124    }
125
126    bool SoundBase::isStopped() {
127        if(alIsSource(this->source_)) {
128            return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
129        }
130        return true;
131    }
132
133    bool SoundBase::loadFile(std::string filename) {
134        filename = Core::getMediaPathString() + "/audio/" + filename;
135        COUT(3) << "OpenAL ALUT: loading file " << filename << std::endl;
136        this->buffer_ = alutCreateBufferFromFile(filename.c_str());
137        if(this->buffer_ == AL_NONE) {
138            COUT(2) << "OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
139            if(filename.find("ogg", 0) != std::string::npos)
140            {
141                this->buffer_ = loadOggFile(filename);
142            }
143
144            if(this->buffer_ == AL_NONE) 
145                return false;
146        }
147
148        alGenSources(1, &this->source_);
149        alSourcei(this->source_, AL_BUFFER, this->buffer_);
150        if(alGetError() != AL_NO_ERROR) {
151            COUT(2) << "OpenAL: Error loading sample file" << std::endl;
152            return false;
153        }
154        return true;
155    }
156
157    ALint SoundBase::getSourceState() {
158        ALint state;
159        alGetSourcei(this->source_, AL_SOURCE_STATE, &state);
160        return state;
161    }
162   
163    ALuint SoundBase::loadOggFile(std::string filename)
164    {
165        // just a dummy
166        return AL_NONE;
167    }
168} // namespace: orxonox
Note: See TracBrowser for help on using the repository browser.