- Cleanup and rewrite some mainloop code
[supertux.git] / src / audio / sound_manager.cpp
index cc9dc40..fd3320e 100644 (file)
@@ -25,6 +25,7 @@
 #include <sstream>
 #include <memory>
 #include <assert.h>
+#include <SDL.h>
 
 #include "sound_file.hpp"
 #include "sound_source.hpp"
 #include "log.hpp"
 #include "timer.hpp"
 
+#ifndef DEBUG
+  /** 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()
@@ -117,6 +128,14 @@ SoundManager::create_sound_source(const std::string& filename)
   if(!sound_enabled)
     return create_dummy_sound_source();
 
+  std::auto_ptr<OpenALSoundSource> 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();
+  }
+
   ALuint buffer;
 
   // reuse an existing static sound buffer
@@ -142,9 +161,8 @@ SoundManager::create_sound_source(const std::string& filename)
     }
   }
 
-  OpenALSoundSource* source = new OpenALSoundSource();
   alSourcei(source->source, AL_BUFFER, buffer);
-  return source;
+  return source.release();
 }
 
 void
@@ -295,7 +313,7 @@ SoundManager::play_music(const std::string& filename, bool fade)
 void
 SoundManager::set_listener_position(const Vector& pos)
 {
-  static Uint32 lastticks = 0;
+  static Uint32 lastticks = SDL_GetTicks();
 
   Uint32 current_ticks = SDL_GetTicks();
   if(current_ticks - lastticks < 300)
@@ -314,11 +332,12 @@ SoundManager::set_listener_velocity(const Vector& vel)
 void
 SoundManager::update()
 {
-  static float lasttime = real_time;
+  static Uint32 lasttime = SDL_GetTicks();
+  Uint32 now = SDL_GetTicks();
 
-  if(real_time - lasttime < 0.3)
+  if(now - lasttime < 300)
     return;
-  lasttime = real_time;
+  lasttime = now;
 
   // update and check for finished sound sources
   for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {