fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / audio / sound_manager.cpp
index 0c77b99..cc9dc40 100644 (file)
@@ -24,6 +24,7 @@
 #include <iostream>
 #include <sstream>
 #include <memory>
+#include <assert.h>
 
 #include "sound_file.hpp"
 #include "sound_source.hpp"
@@ -117,8 +118,8 @@ SoundManager::create_sound_source(const std::string& filename)
     return create_dummy_sound_source();
 
   ALuint buffer;
-  
-  // reuse an existing static sound buffer            
+
+  // reuse an existing static sound buffer
   SoundBuffers::iterator i = buffers.find(filename);
   if(i != buffers.end()) {
     buffer = i->second;
@@ -140,10 +141,10 @@ SoundManager::create_sound_source(const std::string& filename)
       return create_dummy_sound_source();
     }
   }
-  
+
   OpenALSoundSource* source = new OpenALSoundSource();
   alSourcei(source->source, AL_BUFFER, buffer);
-  return source;  
+  return source;
 }
 
 void
@@ -151,7 +152,7 @@ SoundManager::preload(const std::string& filename)
 {
   if(!sound_enabled)
     return;
-  
+
   SoundBuffers::iterator i = buffers.find(filename);
   // already loaded?
   if(i != buffers.end())
@@ -171,18 +172,18 @@ SoundManager::play(const std::string& filename, const Vector& pos)
 {
   if(!sound_enabled)
     return;
-  
+
   try {
-    OpenALSoundSource* source 
-      = static_cast<OpenALSoundSource*> (create_sound_source(filename));
-    
+    std::auto_ptr<OpenALSoundSource> source
+      (static_cast<OpenALSoundSource*> (create_sound_source(filename)));
+
     if(pos == Vector(-1, -1)) {
       source->set_rollof_factor(0);
     } else {
       source->set_position(pos);
     }
     source->play();
-    sources.push_back(source);
+    sources.push_back(source.release());
   } catch(std::exception& e) {
     log_warning << "Couldn't play sound " << filename << ": " << e.what() << std::endl;
   }
@@ -192,7 +193,7 @@ void
 SoundManager::manage_source(SoundSource* source)
 {
   assert(source != NULL);
-  
+
   OpenALSoundSource* openal_source = dynamic_cast<OpenALSoundSource*> (source);
   if(openal_source != NULL) {
     sources.push_back(openal_source);
@@ -200,6 +201,27 @@ SoundManager::manage_source(SoundSource* source)
 }
 
 void
+SoundManager::register_for_update( StreamSoundSource* sss ){
+  if( sss != NULL ){
+    update_list.push_back( sss );
+  }
+}
+
+void
+SoundManager::remove_from_update( StreamSoundSource* sss  ){
+  if( sss != NULL ){
+    StreamSoundSources::iterator i = update_list.begin();
+       while( i != update_list.end() ){
+      if( *i == sss ){
+        i = update_list.erase(i);
+      } else {
+        i++;
+      }
+    }
+  }
+}
+
+void
 SoundManager::enable_sound(bool enable)
 {
   if(device == NULL)
@@ -278,7 +300,7 @@ SoundManager::set_listener_position(const Vector& pos)
   Uint32 current_ticks = SDL_GetTicks();
   if(current_ticks - lastticks < 300)
     return;
-  lastticks = current_ticks;                 
+  lastticks = current_ticks;
 
   alListener3f(AL_POSITION, pos.x, pos.y, 0);
 }
@@ -303,7 +325,7 @@ SoundManager::update()
     OpenALSoundSource* source = *i;
 
     source->update();
-    
+
     if(!source->playing()) {
       delete source;
       i = sources.erase(i);
@@ -321,6 +343,13 @@ SoundManager::update()
     alcProcessContext(context);
     check_alc_error("Error while processing audio context: ");
   }
+
+  //run update() for stream_sound_source
+  StreamSoundSources::iterator s = update_list.begin();
+  while( s != update_list.end() ){
+    (*s)->update();
+    s++;
+  }
 }
 
 ALenum
@@ -343,7 +372,7 @@ SoundManager::get_sample_format(SoundFile* file)
       throw std::runtime_error("Only 16 and 8 bit samples supported");
     }
   }
-  
+
   throw std::runtime_error("Only 1 and 2 channel samples supported");
 }
 
@@ -364,7 +393,7 @@ SoundManager::check_alc_error(const char* message)
     std::stringstream msg;
     msg << message << alcGetString(device, err);
     throw std::runtime_error(msg.str());
-  }                
+  }
 }
 
 void
@@ -375,6 +404,5 @@ SoundManager::check_al_error(const char* message)
     std::stringstream msg;
     msg << message << alGetString(err);
     throw std::runtime_error(msg.str());
-  }  
+  }
 }
-