separate nolok sprite
[supertux.git] / src / audio / sound_manager.cpp
index 933ddcf..656caac 100644 (file)
@@ -9,9 +9,11 @@
 #include "sound_source.hpp"
 #include "stream_sound_source.hpp"
 
+SoundManager* sound_manager = 0;
+
 SoundManager::SoundManager()
   : device(0), context(0), sound_enabled(false), music_source(0),
-    next_music_source(0)
+    music_enabled(true)
 {
   try {
     device = alcOpenDevice(0);
@@ -39,7 +41,6 @@ SoundManager::SoundManager()
 SoundManager::~SoundManager()
 {
   delete music_source;
-  delete next_music_source;
 
   for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
     delete *i;
@@ -51,7 +52,6 @@ SoundManager::~SoundManager()
   }
 
   if(context != 0) {
-    alcMakeContextCurrent(0);
     alcDestroyContext(context);
   }
   if(device != 0) {
@@ -151,31 +151,44 @@ SoundManager::enable_music(bool enable)
 }
 
 void
+SoundManager::stop_music(float fadetime)
+{
+  if(fadetime > 0) {
+    if(music_source
+        && music_source->get_fade_state() != StreamSoundSource::FadingOff)
+      music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
+  } else {
+    delete music_source;
+    music_source = 0;
+  }
+  current_music = "";
+}
+
+void
 SoundManager::play_music(const std::string& filename, bool fade)
 {
-  if(filename == current_music)
+  if(filename == current_music && music_source != NULL)
     return;
   current_music = filename;
   if(!music_enabled)
     return;
 
-  try {
-    StreamSoundSource* newmusic 
-      = new StreamSoundSource(load_sound_file(filename));
+  if(filename == "") {
+    delete music_source;
+    music_source = 0;
+    return;
+  }
 
+  try {
+    std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
     alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
-    if(fade) {
-      if(music_source)
-        music_source->setFading(StreamSoundSource::FadingOff, .25f);
-      delete next_music_source;
-      next_music_source = newmusic;
-    } else {
-      delete music_source;
-      music_source = newmusic;
-      music_source->play();
-      next_music_source = 0;
-    }
+    newmusic->set_sound_file(load_sound_file(filename));
+    if(fade)
+      newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
+    newmusic->play();
+
+    delete music_source;
+    music_source = newmusic.release();
   } catch(std::exception& e) {
     std::cerr << "Couldn't play music file '" << filename << "': "
       << e.what() << "\n";
@@ -183,13 +196,20 @@ SoundManager::play_music(const std::string& filename, bool fade)
 }
 
 void
-SoundManager::set_listener_position(Vector pos)
+SoundManager::set_listener_position(const Vector& pos)
 {
+  static Uint32 lastticks = 0;
+
+  Uint32 current_ticks = SDL_GetTicks();
+  if(current_ticks - lastticks < 300)
+    return;
+  lastticks = current_ticks;                 
+
   alListener3f(AL_POSITION, pos.x, pos.y, 0);
 }
 
 void
-SoundManager::set_listener_velocity(Vector vel)
+SoundManager::set_listener_velocity(const Vector& vel)
 {
   alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
 }
@@ -197,6 +217,13 @@ SoundManager::set_listener_velocity(Vector vel)
 void
 SoundManager::update()
 {
+  static Uint32 lastticks = 0;
+
+  Uint32 current_ticks = SDL_GetTicks();
+  if(current_ticks - lastticks < 300)
+    return;
+  lastticks = current_ticks;
+
   // check for finished sound sources
   for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
     SoundSource* source = *i;
@@ -212,14 +239,6 @@ SoundManager::update()
     music_source->update();
   }
   
-  if(next_music_source && !music_source || !music_source->playing()) {
-    delete music_source;
-    music_source = next_music_source;
-    //music_source->setFading(StreamSoundSource::FadingOn, 1.0f);
-    music_source->play();
-    next_music_source = 0;
-  }
-  
   alcProcessContext(context);
   check_alc_error("Error while processing audio context: ");
 }
@@ -254,7 +273,7 @@ SoundManager::print_openal_version()
   std::cout << "OpenAL Vendor: " << alGetString(AL_VENDOR) << "\n"
             << "OpenAL Version: " << alGetString(AL_VERSION) << "\n" 
             << "OpenAL Renderer: " << alGetString(AL_RENDERER) << "\n"
-            << "OpenAl Extensions: " << alGetString(AL_RENDERER) << "\n";
+            << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << "\n";
 }
 
 void