Merge branch 'feature/c++11'
[supertux.git] / src / audio / sound_manager.cpp
index 9a6b6c8..9f2b36f 100644 (file)
 #include "audio/stream_sound_source.hpp"
 #include "util/log.hpp"
 
-#ifdef NDEBUG
-/** Older openal versions often miss this function and it isn't that vital for
- * supertux...
- */
-#ifdef alcGetString
-#undef alcGetString
-#endif
-#define alcGetString(x,y) ""
-#endif
-
-SoundManager* sound_manager = 0;
-
 SoundManager::SoundManager() :
   device(0), 
   context(0), 
@@ -66,12 +54,14 @@ SoundManager::SoundManager() :
     sound_enabled = true;
     music_enabled = true;
   } catch(std::exception& e) {
-    if(context != NULL)
+    if(context != NULL) {
       alcDestroyContext(context);
-    context = NULL;
-    if(device != NULL)
+      context = NULL; 
+    }
+    if(device != NULL) {
       alcCloseDevice(device);
-    device = NULL;
+      device = NULL;
+    }
     log_warning << "Couldn't initialize audio device: " << e.what() << std::endl;
     print_openal_version();
   }
@@ -92,9 +82,11 @@ SoundManager::~SoundManager()
 
   if(context != NULL) {
     alcDestroyContext(context);
+    context = NULL;
   }
   if(device != NULL) {
     alcCloseDevice(device);
+    device = NULL;
   }
 }
 
@@ -124,10 +116,9 @@ SoundManager::load_file_into_buffer(SoundFile* file)
 OpenALSoundSource*
 SoundManager::intern_create_sound_source(const std::string& filename)
 {
-  if(!sound_enabled)
-    throw std::runtime_error("sound disabled");
+  assert(sound_enabled);
 
-  std::auto_ptr<OpenALSoundSource> source (new OpenALSoundSource());
+  std::unique_ptr<OpenALSoundSource> source (new OpenALSoundSource());
 
   ALuint buffer;
 
@@ -137,7 +128,7 @@ SoundManager::intern_create_sound_source(const std::string& filename)
     buffer = i->second;
   } else {
     // Load sound file
-    std::auto_ptr<SoundFile> file (load_sound_file(filename));
+    std::unique_ptr<SoundFile> file (load_sound_file(filename));
 
     if(file->size < 100000) {
       buffer = load_file_into_buffer(file.get());
@@ -180,7 +171,7 @@ SoundManager::preload(const std::string& filename)
   if(i != buffers.end())
     return;
   try {
-    std::auto_ptr<SoundFile> file (load_sound_file(filename));
+    std::unique_ptr<SoundFile> file (load_sound_file(filename));
     // only keep small files
     if(file->size >= 100000)
       return;
@@ -199,11 +190,11 @@ SoundManager::play(const std::string& filename, const Vector& pos)
     return;
 
   try {
-    std::auto_ptr<OpenALSoundSource> source
+    std::unique_ptr<OpenALSoundSource> source
       (intern_create_sound_source(filename));
 
-    if(pos == Vector(-1, -1)) {
-      source->set_rollof_factor(0);
+    if(pos.x < 0 || pos.y < 0) {
+      source->set_relative(true);
     } else {
       source->set_position(pos);
     }
@@ -267,7 +258,7 @@ SoundManager::enable_music(bool enable)
   } else {
     if(music_source) {
       delete music_source;
-      music_source = 0;
+      music_source = NULL;
     }
   }
 }
@@ -302,10 +293,10 @@ SoundManager::play_music(const std::string& filename, bool fade)
   }
 
   try {
-    std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
-    alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
+    std::unique_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
     newmusic->set_sound_file(load_sound_file(filename));
     newmusic->set_looping(true);
+    newmusic->set_relative(true);
     if(fade)
       newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
     newmusic->play();
@@ -314,6 +305,8 @@ SoundManager::play_music(const std::string& filename, bool fade)
     music_source = newmusic.release();
   } catch(std::exception& e) {
     log_warning << "Couldn't play music file '" << filename << "': " << e.what() << std::endl;
+    // When this happens, previous music continued playing, stop it, just in case.
+    stop_music(0);
   }
 }