Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / audio / openal_sound_source.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "audio/openal_sound_source.hpp"
18 #include "audio/sound_manager.hpp"
19
20 OpenALSoundSource::OpenALSoundSource() :
21   source()
22 {
23   alGenSources(1, &source);
24   SoundManager::check_al_error("Couldn't create audio source: ");
25   set_reference_distance(128);
26 }
27
28 OpenALSoundSource::~OpenALSoundSource()
29 {
30   stop();
31   alDeleteSources(1, &source);
32 }
33
34 void
35 OpenALSoundSource::stop()
36 {
37   alSourceStop(source);
38   alSourcei(source, AL_BUFFER, AL_NONE);
39   SoundManager::check_al_error("Problem stopping audio source: ");
40 }
41
42 void
43 OpenALSoundSource::play()
44 {
45   alSourcePlay(source);
46   SoundManager::check_al_error("Couldn't start audio source: ");
47 }
48
49 bool
50 OpenALSoundSource::playing()
51 {
52   ALint state = AL_PLAYING;
53   alGetSourcei(source, AL_SOURCE_STATE, &state);
54   return state != AL_STOPPED;
55 }
56
57 void
58 OpenALSoundSource::update()
59 {
60 }
61
62 void
63 OpenALSoundSource::set_looping(bool looping)
64 {
65   alSourcei(source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
66 }
67
68 void
69 OpenALSoundSource::set_position(const Vector& position)
70 {
71   alSource3f(source, AL_POSITION, position.x, position.y, 0);
72 }
73
74 void
75 OpenALSoundSource::set_velocity(const Vector& velocity)
76 {
77   alSource3f(source, AL_VELOCITY, velocity.x, velocity.y, 0);
78 }
79
80 void
81 OpenALSoundSource::set_gain(float gain)
82 {
83   alSourcef(source, AL_GAIN, gain);
84 }
85
86 void
87 OpenALSoundSource::set_pitch(float pitch)
88 {
89   alSourcef(source, AL_PITCH, pitch);
90 }
91
92 void
93 OpenALSoundSource::set_reference_distance(float distance)
94 {
95   alSourcef(source, AL_REFERENCE_DISTANCE, distance);
96 }
97
98 void
99 OpenALSoundSource::set_rollof_factor(float factor)
100 {
101   alSourcef(source, AL_ROLLOFF_FACTOR, factor);
102 }
103
104 /* EOF */