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