252cc6ee5ff79f9930eec31bc0c30341e49e1d88
[supertux.git] / src / 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
24 OpenALSoundSource::OpenALSoundSource()
25 {
26   alGenSources(1, &source);
27   SoundManager::check_al_error("Couldn't create audio source: ");
28   set_reference_distance(128);
29 }
30
31 OpenALSoundSource::~OpenALSoundSource()
32 {
33   stop();
34   alDeleteSources(1, &source);
35 }
36
37 void
38 OpenALSoundSource::stop()
39 {
40   alSourceStop(source);
41   alSourcei(source, AL_BUFFER, AL_NONE);
42   SoundManager::check_al_error("Problem stopping audio source: ");
43 }
44
45 void
46 OpenALSoundSource::play()
47 {
48   alSourcePlay(source);
49   SoundManager::check_al_error("Couldn't start audio source: ");
50 }
51
52 bool
53 OpenALSoundSource::playing()
54 {
55   ALint state = AL_PLAYING;
56   alGetSourcei(source, AL_SOURCE_STATE, &state);
57   return state != AL_STOPPED;
58 }
59
60 void
61 OpenALSoundSource::update()
62 {
63 }
64
65 void
66 OpenALSoundSource::set_looping(bool looping)
67 {
68   alSourcei(source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
69 }
70
71 void
72 OpenALSoundSource::set_position(const Vector& position)
73 {
74   alSource3f(source, AL_POSITION, position.x, position.y, 0);
75 }
76
77 void
78 OpenALSoundSource::set_velocity(const Vector& velocity)
79 {
80   alSource3f(source, AL_VELOCITY, velocity.x, velocity.y, 0);
81 }
82
83 void
84 OpenALSoundSource::set_gain(float gain)
85 {
86   alSourcef(source, AL_GAIN, gain);
87 }
88
89 void
90 OpenALSoundSource::set_pitch(float pitch)
91 {
92   alSourcef(source, AL_PITCH, pitch);
93 }
94
95 void
96 OpenALSoundSource::set_reference_distance(float distance)
97 {
98   alSourcef(source, AL_REFERENCE_DISTANCE, distance);
99 }
100
101 void
102 OpenALSoundSource::set_rollof_factor(float factor)
103 {
104   alSourcef(source, AL_ROLLOFF_FACTOR, factor);
105 }