Fix music not properly fading in again
[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
19 #include "audio/sound_manager.hpp"
20
21 OpenALSoundSource::OpenALSoundSource() :
22   source()
23 {
24   alGenSources(1, &source);
25   SoundManager::check_al_error("Couldn't create audio source: ");
26   set_reference_distance(128);
27 }
28
29 OpenALSoundSource::~OpenALSoundSource()
30 {
31   stop();
32   alDeleteSources(1, &source);
33 }
34
35 void
36 OpenALSoundSource::stop()
37 {
38   alSourceStop(source);
39   alSourcei(source, AL_BUFFER, AL_NONE);
40   SoundManager::check_al_error("Problem stopping audio source: ");
41 }
42
43 void
44 OpenALSoundSource::play()
45 {
46   alSourcePlay(source);
47   SoundManager::check_al_error("Couldn't start audio source: ");
48 }
49
50 bool
51 OpenALSoundSource::playing()
52 {
53   ALint state = AL_PLAYING;
54   alGetSourcei(source, AL_SOURCE_STATE, &state);
55   return state != AL_STOPPED;
56 }
57
58 void
59 OpenALSoundSource::pause()
60 {
61   alSourcePause(source);
62   SoundManager::check_al_error("Couldn't pause audio source: ");
63 }
64
65 void
66 OpenALSoundSource::resume()
67 {
68   if( !this->paused() )
69   {
70     return;
71   }
72
73   this->play();
74 }
75
76 bool
77 OpenALSoundSource::paused()
78 {
79     ALint state = AL_PAUSED;
80     alGetSourcei(source, AL_SOURCE_STATE, &state);
81     return state == AL_PAUSED;
82 }
83
84 void
85 OpenALSoundSource::update()
86 {
87 }
88
89 void
90 OpenALSoundSource::set_looping(bool looping)
91 {
92   alSourcei(source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
93 }
94
95 void
96 OpenALSoundSource::set_relative(bool relative)
97 {
98   alSourcei(source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
99 }
100
101 void
102 OpenALSoundSource::set_position(const Vector& position)
103 {
104   alSource3f(source, AL_POSITION, position.x, position.y, 0);
105 }
106
107 void
108 OpenALSoundSource::set_velocity(const Vector& velocity)
109 {
110   alSource3f(source, AL_VELOCITY, velocity.x, velocity.y, 0);
111 }
112
113 void
114 OpenALSoundSource::set_gain(float gain)
115 {
116   alSourcef(source, AL_GAIN, gain);
117 }
118
119 void
120 OpenALSoundSource::set_pitch(float pitch)
121 {
122   alSourcef(source, AL_PITCH, pitch);
123 }
124
125 void
126 OpenALSoundSource::set_reference_distance(float distance)
127 {
128   alSourcef(source, AL_REFERENCE_DISTANCE, distance);
129 }
130
131 /* EOF */