Made compiles on MacOS X a bit smoother, activated MacOS-specific code for CMake...
[supertux.git] / src / audio / sound_manager.hpp
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 #ifndef __SOUND_MANAGER_H__
20 #define __SOUND_MANAGER_H__
21
22 #include <string>
23 #include <vector>
24 #include <map>
25
26 #ifndef MACOSX
27 #include <AL/alc.h>
28 #include <AL/al.h>
29 #else
30 #include <OpenAL/alc.h>
31 #include <OpenAL/al.h>
32 #endif
33
34 #include "math/vector.hpp"
35
36 class SoundFile;
37 class SoundSource;
38 class StreamSoundSource;
39 class OpenALSoundSource;
40
41 class SoundManager
42 {
43 public:
44   SoundManager();
45   virtual ~SoundManager();
46
47   void enable_sound(bool sound_enabled);
48   /**
49    * Creates a new sound source object which plays the specified soundfile.
50    * You are responsible for deleting the sound source later (this will stop the
51    * sound).
52    * This function never throws exceptions, but might return a DummySoundSource
53    */
54   SoundSource* create_sound_source(const std::string& filename);
55   /**
56    * Convenience function to simply play a sound at a given position.
57    */
58   void play(const std::string& name, const Vector& pos = Vector(-1, -1));
59   /**
60    * Adds the source to the list of managed sources (= the source gets deleted
61    * when it finished playing)
62    */
63   void manage_source(SoundSource* source);
64   /// preloads a sound, so that you don't get a lag later when playing it
65   void preload(const std::string& name);
66
67   void set_listener_position(const Vector& position);
68   void set_listener_velocity(const Vector& velocity);
69
70   void enable_music(bool music_enabled);
71   void play_music(const std::string& filename, bool fade = false);
72   void stop_music(float fadetime = 0);
73
74   bool is_music_enabled() { return music_enabled; }
75   bool is_sound_enabled() { return sound_enabled; }
76
77   bool is_audio_enabled() {
78                         return device != 0 && context != 0;
79   }
80
81   void update();
82
83   /*
84    * Tell soundmanager to call update() for stream_sound_source.
85    */
86   void register_for_update( StreamSoundSource* sss );
87   /*
88    * Unsubscribe from updates for stream_sound_source.
89    */
90   void remove_from_update( StreamSoundSource* sss );
91
92 private:
93   friend class OpenALSoundSource;
94   friend class StreamSoundSource;
95
96   /** creates a new sound source, might throw exceptions, never returns NULL */
97   OpenALSoundSource* intern_create_sound_source(const std::string& filename);
98   static ALuint load_file_into_buffer(SoundFile* file);
99   static ALenum get_sample_format(SoundFile* file);
100
101   void print_openal_version();
102   void check_alc_error(const char* message);
103   static void check_al_error(const char* message);
104
105   ALCdevice* device;
106   ALCcontext* context;
107   bool sound_enabled;
108
109   typedef std::map<std::string, ALuint> SoundBuffers;
110   SoundBuffers buffers;
111   typedef std::vector<OpenALSoundSource*> SoundSources;
112   SoundSources sources;
113
114   typedef std::vector<StreamSoundSource*> StreamSoundSources;
115   StreamSoundSources update_list;
116
117   StreamSoundSource* music_source;
118
119   bool music_enabled;
120   std::string current_music;
121 };
122
123 extern SoundManager* sound_manager;
124
125 #endif