[Super-tux-commit] supertux/lib/audio sound_manager.cpp,1.3,1.4 sound_manager.h,1.5,1.6 sound.cpp,1.
Brought to you by:
wkendrick
From: Tobias G. <to...@us...> - 2004-07-25 19:04:21
|
Update of /cvsroot/super-tux/supertux/lib/audio In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31225/lib/audio Modified Files: sound_manager.cpp sound_manager.h Removed Files: sound.cpp sound.h Log Message: - Major changes in Setup-API. - Moved all sound handling into SoundManager, which became a singleton. Index: sound_manager.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/audio/sound_manager.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- sound_manager.cpp 22 Jul 2004 19:07:50 -0000 1.3 +++ sound_manager.cpp 25 Jul 2004 19:03:34 -0000 1.4 @@ -22,15 +22,16 @@ #include "../audio/sound_manager.h" #include "../audio/musicref.h" -#include "../audio/sound.h" #include "../app/globals.h" #include "../app/setup.h" #include "../special/moving_object.h" using namespace SuperTux; +SoundManager* SoundManager::instance_ = 0; + SoundManager::SoundManager() - : current_music(0), music_enabled(true) + : current_music(0), m_music_enabled(true) , m_sound_enabled(true) , audio_device(true) { } @@ -38,12 +39,15 @@ { if(audio_device) Mix_HaltMusic(); + +sounds.clear(); +destroy_instance(); } void SoundManager::play_sound(Mix_Chunk* sound) { - if(!audio_device || !use_sound) + if(!audio_device || !m_sound_enabled) return; Mix_PlayChannel(-1, sound, 0); @@ -60,7 +64,7 @@ void SoundManager::play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2) { - if(!audio_device || !use_sound) + if(!audio_device || !m_sound_enabled) return; // TODO make sure this formula is good @@ -89,7 +93,7 @@ return MusicRef(0); if(!exists_music(file)) - st_abort("Couldn't load musicfile ", file.c_str()); + Termination::abort("Couldn't load musicfile ", file.c_str()); std::map<std::string, MusicResource>::iterator i = musics.find(file); assert(i != musics.end()); @@ -145,7 +149,7 @@ current_music = musicref.music; current_music->refcount++; - if(music_enabled) + if(m_music_enabled) Mix_PlayMusic(current_music->music, loops); } @@ -171,20 +175,79 @@ if(!audio_device) return; - if(enable == music_enabled) + if(enable == m_music_enabled) return; - music_enabled = enable; - if(music_enabled == false) { + m_music_enabled = enable; + if(m_music_enabled == false) { Mix_HaltMusic(); } else { Mix_PlayMusic(current_music->music, -1); } } +void +SoundManager::enable_sound(bool enable) +{ + if(!audio_device) + return; + + m_sound_enabled = enable; +} + SoundManager::MusicResource::~MusicResource() { // don't free music buggy SDL_Mixer crashs for some mod files // Mix_FreeMusic(music); } +/* --- LOAD A SOUND --- */ + +Mix_Chunk* SoundManager::load_sound(const std::string& file) +{ + if(!audio_device) + return 0; + + Mix_Chunk* snd = Mix_LoadWAV(file.c_str()); + + /*if (snd == 0) + Termination::abort("Can't load", file);*/ + + return(snd); +} + +void SoundManager::free_chunk(Mix_Chunk *chunk) +{ + Mix_FreeChunk( chunk ); +} + + +/* --- OPEN THE AUDIO DEVICE --- */ + +int SoundManager::open_audio (int frequency, Uint16 format, int channels, int chunksize) +{ + if (Mix_OpenAudio( frequency, format, channels, chunksize ) < 0) + return -1; + + // allocate 16 channels for mixing + if (Mix_AllocateChannels(8) != 8) + return -2; + + return 0; +} + + +/* --- CLOSE THE AUDIO DEVICE --- */ + +void SoundManager::close_audio( void ) +{ + if (audio_device) { + Mix_CloseAudio(); + } +} + +Mix_Chunk* SuperTux::IDToSound(int id) +{ + return SoundManager::get()->sounds[id]; +} + --- sound.h DELETED --- Index: sound_manager.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/audio/sound_manager.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- sound_manager.h 22 Jul 2004 19:07:50 -0000 1.5 +++ sound_manager.h 25 Jul 2004 19:03:34 -0000 1.6 @@ -21,6 +21,7 @@ #define SUPERTUX_SOUND_MANAGER_H #include <string> +#include <vector> #include <map> #include "SDL_mixer.h" @@ -32,14 +33,20 @@ class MusicRef; class MovingObject; + /// enum of different internal music types + enum Music_Type { + NO_MUSIC, + LEVEL_MUSIC, + HURRYUP_MUSIC, + HERRING_MUSIC + }; + /// Sound manager /** This class handles all sounds that are played */ class SoundManager { public: - SoundManager(); - ~SoundManager(); /// Play sound. void play_sound(Mix_Chunk* sound); @@ -47,10 +54,14 @@ void play_sound(Mix_Chunk* sound, const Vector& pos, const Vector& pos2); /// Play sound relative to a MovingObject and a Vector. void play_sound(Mix_Chunk* sound, const MovingObject* object, const Vector& pos); - + /// Load music. /** Is used to load the music for a MusicRef. */ MusicRef load_music(const std::string& file); + + /// Load sound. + Mix_Chunk * load_sound(const std::string& file); + /// Test if a certain music file exists. bool exists_music(const std::string& filename); @@ -64,9 +75,68 @@ /// Enable/Disable music. void enable_music(bool enable); + /// Is music enabled? + bool music_enabled() + { + return m_music_enabled; + } + + /// Enable/Disable sound. + void enable_sound(bool enable); + + /// Is sound enabled? + bool sound_enabled() + { + return m_sound_enabled; + } + + /* functions handling the sound and music */ + int open_audio(int frequency, Uint16 format, int channels, int chunksize); + void close_audio( void ); + + /// Is audio available? + bool audio_device_available() + { + return audio_device; + } + + void set_audio_device_available(bool available) + { + audio_device = available; + } + + static SoundManager* get() + { + return instance_ ? instance_ : instance_ = new SoundManager(); + } + static void destroy_instance() + { + delete instance_; + instance_ = 0; + } + + void add_sound(Mix_Chunk* sound, int id) + { + sounds[id] = sound; + } + + Mix_Chunk* get_sound(int id) + { + return sounds[id]; + } + private: + SoundManager(); + ~SoundManager(); + // music part friend class MusicRef; + friend class Setup; + friend Mix_Chunk* IDToSound(int id); + + static SoundManager* instance_ ; + + void free_chunk(Mix_Chunk* chunk); /// Resource for music. /** Contains the raw music data and @@ -84,12 +154,18 @@ void free_music(MusicResource* music); + /*variables for stocking the sound and music*/ + std::map<int, Mix_Chunk*> sounds; std::map<std::string, MusicResource> musics; MusicResource* current_music; - bool music_enabled; + bool m_music_enabled; + bool m_sound_enabled; + bool audio_device; /* != 0: available and initialized */ + }; + Mix_Chunk* IDToSound(int id); + } // namespace SuperTux - #endif /*SUPERTUX_SOUND_MANAGER_H*/ --- sound.cpp DELETED --- |