AmbientSound scripting patch from tuxdev
authorChristoph Sommer <mail@christoph-sommer.de>
Wed, 5 Jul 2006 02:27:11 +0000 (02:27 +0000)
committerChristoph Sommer <mail@christoph-sommer.de>
Wed, 5 Jul 2006 02:27:11 +0000 (02:27 +0000)
SVN-Revision: 3889

src/object/ambient_sound.cpp
src/object/ambient_sound.hpp
src/scripting/wrapper.cpp
src/scripting/wrapper.hpp
src/scripting/wrapper.interface.hpp

index 6893054..5ac3b0f 100644 (file)
 #include "audio/sound_manager.hpp"
 #include "audio/sound_source.hpp"
 #include "log.hpp"
+#include "scripting/ambient_sound.hpp"
+#include "scripting/squirrel_util.hpp"
 
 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
 {
+  name="";
   position.x = 0;
   position.y = 0;
 
@@ -49,6 +52,7 @@ AmbientSound::AmbientSound(const lisp::Lisp& lisp)
     log_warning << "No Position in ambient_sound" << std::endl;
   }
 
+  lisp.get("name" , name);
   lisp.get("width" , dimension.x);
   lisp.get("height", dimension.y);
 
@@ -204,4 +208,19 @@ AmbientSound::draw(DrawingContext &)
 {
 }
 
+void
+AmbientSound::expose(HSQUIRRELVM vm, SQInteger table_idx)
+{
+  if (name == "") return;
+  Scripting::AmbientSound* interface = new Scripting::AmbientSound(this);
+  expose_object(vm, table_idx, interface, name, true);
+}
+
+void
+AmbientSound::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
+{
+  if (name == "") return;
+  Scripting::unexpose_object(vm, table_idx, name);
+}
+
 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");
index 0b2bd1d..74d9c9d 100644 (file)
 #include "game_object.hpp"
 #include "resources.hpp"
 #include "player.hpp"
+#include "script_interface.hpp"
 
 class SoundSource;
 
-class AmbientSound : public GameObject
+class AmbientSound : public GameObject, public ScriptInterface
 {
 public:
   AmbientSound(const lisp::Lisp& lisp);
   AmbientSound(Vector pos, float factor, float bias, float vol, std::string file);
   ~AmbientSound();
+
+  void set_pos(Vector newpos)
+  {
+    position=newpos;
+  }
+  const Vector get_pos() const
+  {
+    return position;
+  }
 protected:
   virtual void hit(Player& player);
   virtual void update(float time);
   virtual void draw(DrawingContext&);
   virtual void start_playing();
   virtual void stop_playing();
+  virtual void expose(HSQUIRRELVM vm, SQInteger table_idx);
+  virtual void unexpose(HSQUIRRELVM vm, SQInteger table_idx);
 private:
+  std::string name; /**< user-defined name for use in scripts or empty string if not scriptable */
   Vector position;
   Vector dimension;
 
index be281de..529d889 100644 (file)
@@ -2001,6 +2001,97 @@ static SQInteger Wind_stop_wrapper(HSQUIRRELVM vm)
   
 }
 
+static SQInteger AmbientSound_release_hook(SQUserPointer ptr, SQInteger )
+{
+  Scripting::AmbientSound* _this = reinterpret_cast<Scripting::AmbientSound*> (ptr);
+  delete _this;
+  return 0;
+}
+
+static SQInteger AmbientSound_set_pos_wrapper(HSQUIRRELVM vm)
+{
+  SQUserPointer data;
+  if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {
+    sq_throwerror(vm, _SC("'set_pos' called without instance"));
+    return SQ_ERROR;
+  }
+  Scripting::AmbientSound* _this = reinterpret_cast<Scripting::AmbientSound*> (data);
+  SQFloat arg0;
+  if(SQ_FAILED(sq_getfloat(vm, 2, &arg0))) {
+    sq_throwerror(vm, _SC("Argument 1 not a float"));
+    return SQ_ERROR;
+  }
+  SQFloat arg1;
+  if(SQ_FAILED(sq_getfloat(vm, 3, &arg1))) {
+    sq_throwerror(vm, _SC("Argument 2 not a float"));
+    return SQ_ERROR;
+  }
+  
+  try {
+    _this->set_pos(static_cast<float> (arg0), static_cast<float> (arg1));
+  
+    return 0;
+  
+  } catch(std::exception& e) {
+    sq_throwerror(vm, e.what());
+    return SQ_ERROR;
+  } catch(...) {
+    sq_throwerror(vm, _SC("Unexpected exception while executing function 'set_pos'"));
+    return SQ_ERROR;
+  }
+  
+}
+
+static SQInteger AmbientSound_get_pos_x_wrapper(HSQUIRRELVM vm)
+{
+  SQUserPointer data;
+  if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {
+    sq_throwerror(vm, _SC("'get_pos_x' called without instance"));
+    return SQ_ERROR;
+  }
+  Scripting::AmbientSound* _this = reinterpret_cast<Scripting::AmbientSound*> (data);
+  
+  try {
+    float return_value = _this->get_pos_x();
+  
+    sq_pushfloat(vm, return_value);
+    return 1;
+  
+  } catch(std::exception& e) {
+    sq_throwerror(vm, e.what());
+    return SQ_ERROR;
+  } catch(...) {
+    sq_throwerror(vm, _SC("Unexpected exception while executing function 'get_pos_x'"));
+    return SQ_ERROR;
+  }
+  
+}
+
+static SQInteger AmbientSound_get_pos_y_wrapper(HSQUIRRELVM vm)
+{
+  SQUserPointer data;
+  if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {
+    sq_throwerror(vm, _SC("'get_pos_y' called without instance"));
+    return SQ_ERROR;
+  }
+  Scripting::AmbientSound* _this = reinterpret_cast<Scripting::AmbientSound*> (data);
+  
+  try {
+    float return_value = _this->get_pos_y();
+  
+    sq_pushfloat(vm, return_value);
+    return 1;
+  
+  } catch(std::exception& e) {
+    sq_throwerror(vm, e.what());
+    return SQ_ERROR;
+  } catch(...) {
+    sq_throwerror(vm, _SC("Unexpected exception while executing function 'get_pos_y'"));
+    return SQ_ERROR;
+  }
+  
+}
+
 static SQInteger display_wrapper(HSQUIRRELVM vm)
 {
   return Scripting::display(vm);
@@ -2851,6 +2942,32 @@ void create_squirrel_instance(HSQUIRRELVM v, Scripting::Wind* object, bool setup
   sq_remove(v, -2); // remove root table
 }
 
+void create_squirrel_instance(HSQUIRRELVM v, Scripting::AmbientSound* object, bool setup_releasehook)
+{
+  using namespace Wrapper;
+
+  sq_pushroottable(v);
+  sq_pushstring(v, "AmbientSound", -1);
+  if(SQ_FAILED(sq_get(v, -2))) {
+    std::ostringstream msg;
+    msg << "Couldn't resolved squirrel type 'AmbientSound'";
+    throw SquirrelError(v, msg.str());
+  }
+
+  if(SQ_FAILED(sq_createinstance(v, -1)) || SQ_FAILED(sq_setinstanceup(v, -1, object))) {
+    std::ostringstream msg;
+    msg << "Couldn't setup squirrel instance for object of type 'AmbientSound'";
+    throw SquirrelError(v, msg.str());
+  }
+  sq_remove(v, -2); // remove object name
+
+  if(setup_releasehook) {
+    sq_setreleasehook(v, -1, AmbientSound_release_hook);
+  }
+
+  sq_remove(v, -2); // remove root table
+}
+
 void register_supertux_wrapper(HSQUIRRELVM v)
 {
   using namespace Wrapper;
@@ -3607,6 +3724,35 @@ void register_supertux_wrapper(HSQUIRRELVM v)
     throw SquirrelError(v, "Couldn't register class 'Wind'");
   }
 
+  // Register class AmbientSound
+  sq_pushstring(v, "AmbientSound", -1);
+  if(sq_newclass(v, SQFalse) < 0) {
+    std::ostringstream msg;
+    msg << "Couldn't create new class 'AmbientSound'";
+    throw SquirrelError(v, msg.str());
+  }
+  sq_pushstring(v, "set_pos", -1);
+  sq_newclosure(v, &AmbientSound_set_pos_wrapper, 0);
+  if(SQ_FAILED(sq_createslot(v, -3))) {
+    throw SquirrelError(v, "Couldn't register function 'set_pos'");
+  }
+
+  sq_pushstring(v, "get_pos_x", -1);
+  sq_newclosure(v, &AmbientSound_get_pos_x_wrapper, 0);
+  if(SQ_FAILED(sq_createslot(v, -3))) {
+    throw SquirrelError(v, "Couldn't register function 'get_pos_x'");
+  }
+
+  sq_pushstring(v, "get_pos_y", -1);
+  sq_newclosure(v, &AmbientSound_get_pos_y_wrapper, 0);
+  if(SQ_FAILED(sq_createslot(v, -3))) {
+    throw SquirrelError(v, "Couldn't register function 'get_pos_y'");
+  }
+
+  if(SQ_FAILED(sq_createslot(v, -3))) {
+    throw SquirrelError(v, "Couldn't register class 'AmbientSound'");
+  }
+
 }
 
 } // end of namespace Scripting
index 4d0af7c..b4c9bd9 100644 (file)
@@ -24,6 +24,7 @@ void create_squirrel_instance(HSQUIRRELVM v, Scripting::FloatingImage* object, b
 void create_squirrel_instance(HSQUIRRELVM v, Scripting::Platform* object, bool setup_releasehook = false);
 void create_squirrel_instance(HSQUIRRELVM v, Scripting::Candle* object, bool setup_releasehook = false);
 void create_squirrel_instance(HSQUIRRELVM v, Scripting::Wind* object, bool setup_releasehook = false);
+void create_squirrel_instance(HSQUIRRELVM v, Scripting::AmbientSound* object, bool setup_releasehook = false);
 
 }
 
index 51c4c86..d7ae920 100644 (file)
@@ -11,4 +11,5 @@
 #include "platform.hpp"
 #include "candle.hpp"
 #include "wind.hpp"
+#include "ambient_sound.hpp"