From: Matthias Braun Date: Tue, 23 Jan 2007 12:28:17 +0000 (+0000) Subject: correctly fix the case when openal fails to create new sources (this time it's really... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=666b1ac7e7f7825ef77930a4a9f87e1bbc4a10ce;p=supertux.git correctly fix the case when openal fails to create new sources (this time it's really correct ;-) SVN-Revision: 4659 --- diff --git a/src/audio/sound_manager.cpp b/src/audio/sound_manager.cpp index fd3320e0e..2e83630e6 100644 --- a/src/audio/sound_manager.cpp +++ b/src/audio/sound_manager.cpp @@ -122,19 +122,13 @@ SoundManager::load_file_into_buffer(SoundFile* file) return buffer; } -SoundSource* -SoundManager::create_sound_source(const std::string& filename) +OpenALSoundSource* +SoundManager::intern_create_sound_source(const std::string& filename) { if(!sound_enabled) - return create_dummy_sound_source(); + throw new std::exception("sound disabled"); - std::auto_ptr source; - try { - source.reset(new OpenALSoundSource()); - } catch(std::exception& e) { - log_warning << "Couldn't create audio source: " << e.what() << std::endl; - return create_dummy_sound_source(); - } + std::auto_ptr source (new OpenALSoundSource()); ALuint buffer; @@ -143,21 +137,16 @@ SoundManager::create_sound_source(const std::string& filename) if(i != buffers.end()) { buffer = i->second; } else { - try { - // Load sound file - std::auto_ptr file (load_sound_file(filename)); - - if(file->size < 100000) { - buffer = load_file_into_buffer(file.get()); - buffers.insert(std::make_pair(filename, buffer)); - } else { - StreamSoundSource* source = new StreamSoundSource(); - source->set_sound_file(file.release()); - return source; - } - } catch(std::exception& e) { - log_warning << "Couldn't load soundfile '" << filename << "': " << e.what() << std::endl; - return create_dummy_sound_source(); + // Load sound file + std::auto_ptr file (load_sound_file(filename)); + + if(file->size < 100000) { + buffer = load_file_into_buffer(file.get()); + buffers.insert(std::make_pair(filename, buffer)); + } else { + StreamSoundSource* source = new StreamSoundSource(); + source->set_sound_file(file.release()); + return source; } } @@ -165,6 +154,20 @@ SoundManager::create_sound_source(const std::string& filename) return source.release(); } +SoundSource* +SoundManager::create_sound_source(const std::string& filename) +{ + if(!sound_enabled) + return create_dummy_sound_source(); + + try { + return intern_create_sound_source(filename); + } catch(std::exception &e) { + log_warning << "Couldn't create audio source: " << e.what() << std::endl; + return create_dummy_sound_source(); + } +} + void SoundManager::preload(const std::string& filename) { @@ -193,7 +196,7 @@ SoundManager::play(const std::string& filename, const Vector& pos) try { std::auto_ptr source - (static_cast (create_sound_source(filename))); + (intern_create_sound_source(filename)); if(pos == Vector(-1, -1)) { source->set_rollof_factor(0); diff --git a/src/audio/sound_manager.hpp b/src/audio/sound_manager.hpp index edf929011..356c1988e 100644 --- a/src/audio/sound_manager.hpp +++ b/src/audio/sound_manager.hpp @@ -43,8 +43,7 @@ public: * Creates a new sound source object which plays the specified soundfile. * You are responsible for deleting the sound source later (this will stop the * sound). - * This function might throw exceptions. It returns 0 if no audio device is - * available. + * This function never throws exceptions, but might return a DummySoundSource */ SoundSource* create_sound_source(const std::string& filename); /** @@ -88,6 +87,8 @@ private: friend class OpenALSoundSource; friend class StreamSoundSource; + /** creates a new sound source, might throw exceptions, never returns NULL */ + OpenALSoundSource* intern_create_sound_source(const std::string& filename); static ALuint load_file_into_buffer(SoundFile* file); static ALenum get_sample_format(SoundFile* file);