Fixed a Segmentation Fault when mr_iceblock was kicked into a brick containing coins...
[supertux.git] / src / object / ambient_sound.cpp
index 05748fa..e7474e6 100644 (file)
 #include "supertux/sector.hpp"
 #include "util/reader.hpp"
 
-AmbientSound::AmbientSound(const Reader& lisp)
+AmbientSound::AmbientSound(const Reader& lisp) :
+  position(),
+  dimension(),
+  sample(),
+  sound_source(),
+  latency(),
+  distance_factor(),
+  distance_bias(),
+  silence_distance(),
+  maximumvolume(),
+  targetvolume(),
+  currentvolume(),
+  volume_ptr()
 {
-  name="";
   position.x = 0;
   position.y = 0;
 
@@ -38,7 +49,6 @@ AmbientSound::AmbientSound(const Reader& lisp)
   distance_factor = 0;
   distance_bias = 0;
   maximumvolume = 1;
-  sample = "";
   currentvolume = 0;
 
   if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
@@ -75,12 +85,24 @@ AmbientSound::AmbientSound(const Reader& lisp)
 
   lisp.get("silence_distance",silence_distance);
 
-  sound_source = 0; // not playing at the beginning
-  sound_manager->preload(sample);
+  sound_source.reset(); // not playing at the beginning
+  SoundManager::current()->preload(sample);
   latency=0;
 }
 
-AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file)
+AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file) :
+  position(),
+  dimension(),
+  sample(file),
+  sound_source(),
+  latency(),
+  distance_factor(),
+  distance_bias(),
+  silence_distance(),
+  maximumvolume(),
+  targetvolume(),
+  currentvolume(),
+  volume_ptr()
 {
   position.x=pos.x;
   position.y=pos.y;
@@ -91,7 +113,6 @@ AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std:
   distance_factor=factor*factor;
   distance_bias=bias*bias;
   maximumvolume=vol;
-  sample=file;
 
   // set default silence_distance
 
@@ -101,11 +122,12 @@ AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std:
     silence_distance = 1/distance_factor;
 
   sound_source = 0; // not playing at the beginning
-  sound_manager->preload(sample);
+  SoundManager::current()->preload(sample);
   latency=0;
 }
 
-AmbientSound::~AmbientSound() {
+AmbientSound::~AmbientSound()
+{
   stop_playing();
 }
 
@@ -115,16 +137,16 @@ AmbientSound::hit(Player& )
 }
 
 void
-AmbientSound::stop_playing() {
-  delete sound_source;
-  sound_source = 0;
+AmbientSound::stop_playing()
+{
+  sound_source.reset();
 }
 
 void
 AmbientSound::start_playing()
 {
   try {
-    sound_source = sound_manager->create_sound_source(sample);
+    sound_source = SoundManager::current()->create_sound_source(sample);
     if(!sound_source)
       throw std::runtime_error("file not found");
 
@@ -134,8 +156,7 @@ AmbientSound::start_playing()
     sound_source->play();
   } catch(std::exception& e) {
     log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl;
-    delete sound_source;
-    sound_source = 0;
+    sound_source.reset();
     remove_me();
   }
 }
@@ -207,14 +228,14 @@ AmbientSound::draw(DrawingContext &)
 void
 AmbientSound::expose(HSQUIRRELVM vm, SQInteger table_idx)
 {
-  Scripting::AmbientSound* interface = static_cast<Scripting::AmbientSound*> (this);
-  expose_object(vm, table_idx, interface, name, false);
+  scripting::AmbientSound* _this = static_cast<scripting::AmbientSound*> (this);
+  expose_object(vm, table_idx, _this, name, false);
 }
 
 void
 AmbientSound::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
 {
-  Scripting::unexpose_object(vm, table_idx, name);
+  scripting::unexpose_object(vm, table_idx, name);
 }
 
 void
@@ -236,6 +257,4 @@ AmbientSound::get_pos_y() const
   return position.y;
 }
 
-IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");
-
 /* EOF */