- add a COLGROUP_MOVING_STATIC (in the future we should try to reduce the number...
[supertux.git] / src / audio / stream_sound_source.cpp
index d278563..958ef6f 100644 (file)
@@ -25,6 +25,7 @@
 #include "stream_sound_source.hpp"
 #include "sound_manager.hpp"
 #include "sound_file.hpp"
+#include "timer.hpp"
 #include "log.hpp"
 
 StreamSoundSource::StreamSoundSource()
@@ -32,10 +33,14 @@ StreamSoundSource::StreamSoundSource()
 {
   alGenBuffers(STREAMFRAGMENTS, buffers);
   SoundManager::check_al_error("Couldn't allocate audio buffers: ");
+  //add me to update list
+  sound_manager->register_for_update( this );
 }
 
 StreamSoundSource::~StreamSoundSource()
 {
+  //don't update me any longer
+  sound_manager->remove_from_update( this );
   delete file;
   stop();
   alDeleteBuffers(STREAMFRAGMENTS, buffers);
@@ -73,15 +78,14 @@ StreamSoundSource::update()
   if(!playing()) {
     if(processed == 0 || !looping)
       return;
-    
-    // we might have to restart the source if we had a buffer underrun  
+
+    // we might have to restart the source if we had a buffer underrun
     log_info << "Restarting audio source because of buffer underrun" << std::endl;
     play();
   }
 
   if(fade_state == FadingOn) {
-    Uint32 ticks = SDL_GetTicks();
-    float time = (ticks - fade_start_ticks) / 1000.0;
+    float time = real_time - fade_start_time;
     if(time >= fade_time) {
       set_gain(1.0);
       fade_state = NoFading;
@@ -89,8 +93,7 @@ StreamSoundSource::update()
       set_gain(time / fade_time);
     }
   } else if(fade_state == FadingOff) {
-    Uint32 ticks = SDL_GetTicks();
-    float time = (ticks - fade_start_ticks) / 1000.0;
+    float time = real_time - fade_start_time;
     if(time >= fade_time) {
       stop();
       fade_state = NoFading;
@@ -105,7 +108,7 @@ StreamSoundSource::set_fading(FadeState state, float fade_time)
 {
   this->fade_state = state;
   this->fade_time = fade_time;
-  this->fade_start_ticks = SDL_GetTicks();
+  this->fade_start_time = real_time;
 }
 
 bool
@@ -139,4 +142,3 @@ StreamSoundSource::fillBufferAndQueue(ALuint buffer)
   // return false if there aren't more buffers to fill
   return bytesread >= STREAMFRAGMENTSIZE;
 }
-