00001 // SuperTux 00002 // Copyright (C) 2006 Matthias Braun <matze@braunis.de> 00003 // 00004 // This program is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 #ifndef HEADER_SUPERTUX_AUDIO_SOUND_MANAGER_HPP 00018 #define HEADER_SUPERTUX_AUDIO_SOUND_MANAGER_HPP 00019 00020 #include <map> 00021 #include <string> 00022 #include <vector> 00023 00024 #include <al.h> 00025 #include <alc.h> 00026 00027 #include "math/vector.hpp" 00028 00029 class SoundFile; 00030 class SoundSource; 00031 class StreamSoundSource; 00032 class OpenALSoundSource; 00033 00034 class SoundManager 00035 { 00036 public: 00037 SoundManager(); 00038 virtual ~SoundManager(); 00039 00040 void enable_sound(bool sound_enabled); 00047 SoundSource* create_sound_source(const std::string& filename); 00051 void play(const std::string& name, const Vector& pos = Vector(-1, -1)); 00056 void manage_source(SoundSource* source); 00058 void preload(const std::string& name); 00059 00060 void set_listener_position(const Vector& position); 00061 void set_listener_velocity(const Vector& velocity); 00062 00063 void enable_music(bool music_enabled); 00064 void play_music(const std::string& filename, bool fade = false); 00065 void stop_music(float fadetime = 0); 00066 00067 bool is_music_enabled() { return music_enabled; } 00068 bool is_sound_enabled() { return sound_enabled; } 00069 00070 bool is_audio_enabled() { 00071 return device != 0 && context != 0; 00072 } 00073 00074 void update(); 00075 00076 /* 00077 * Tell soundmanager to call update() for stream_sound_source. 00078 */ 00079 void register_for_update( StreamSoundSource* sss ); 00080 /* 00081 * Unsubscribe from updates for stream_sound_source. 00082 */ 00083 void remove_from_update( StreamSoundSource* sss ); 00084 00085 private: 00086 friend class OpenALSoundSource; 00087 friend class StreamSoundSource; 00088 00090 OpenALSoundSource* intern_create_sound_source(const std::string& filename); 00091 static ALuint load_file_into_buffer(SoundFile* file); 00092 static ALenum get_sample_format(SoundFile* file); 00093 00094 void print_openal_version(); 00095 void check_alc_error(const char* message); 00096 static void check_al_error(const char* message); 00097 00098 ALCdevice* device; 00099 ALCcontext* context; 00100 bool sound_enabled; 00101 00102 typedef std::map<std::string, ALuint> SoundBuffers; 00103 SoundBuffers buffers; 00104 typedef std::vector<OpenALSoundSource*> SoundSources; 00105 SoundSources sources; 00106 00107 typedef std::vector<StreamSoundSource*> StreamSoundSources; 00108 StreamSoundSources update_list; 00109 00110 StreamSoundSource* music_source; 00111 00112 bool music_enabled; 00113 std::string current_music; 00114 00115 private: 00116 SoundManager(const SoundManager&); 00117 SoundManager& operator=(const SoundManager&); 00118 }; 00119 00120 #endif 00121 00122 /* EOF */