[cppcheck] Part 1: Performance
[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::update()
60 {
61 }
62
63 void
64 OpenALSoundSource::set_looping(bool looping)
65 {
66   alSourcei(source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
67 }
68
69 void
70 OpenALSoundSource::set_relative(bool relative)
71 {
72   alSourcei(source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
73 }
74
75 void
76 OpenALSoundSource::set_position(const Vector& position)
77 {
78   alSource3f(source, AL_POSITION, position.x, position.y, 0);
79 }
80
81 void
82 OpenALSoundSource::set_velocity(const Vector& velocity)
83 {
84   alSource3f(source, AL_VELOCITY, velocity.x, velocity.y, 0);
85 }
86
87 void
88 OpenALSoundSource::set_gain(float gain)
89 {
90   alSourcef(source, AL_GAIN, gain);
91 }
92
93 void
94 OpenALSoundSource::set_pitch(float pitch)
95 {
96   alSourcef(source, AL_PITCH, pitch);
97 }
98
99 void
100 OpenALSoundSource::set_reference_distance(float distance)
101 {
102   alSourcef(source, AL_REFERENCE_DISTANCE, distance);
103 }
104
105 /* EOF */