added some comments and the framework for platforms that travel back and forth on...
[supertux.git] / src / scripting / script_interpreter.cpp
index b027fbf..91e3263 100644 (file)
@@ -1,6 +1,6 @@
 #include <config.h>
 
-#include "script_interpreter.h"
+#include "script_interpreter.hpp"
 
 #include <stdarg.h>
 #include <stdexcept>
 #include <sqstdmath.h>
 #include <sqstdstring.h>
 
-#include "wrapper.h"
-#include "wrapper_util.h"
-#include "sector.h"
-#include "file_system.h"
-#include "game_session.h"
-#include "resources.h"
-#include "physfs/physfs_stream.h"
-#include "object/text_object.h"
-#include "object/scripted_object.h"
-#include "object/display_effect.h"
-#include "scripting/sound.h"
-#include "scripting/scripted_object.h"
-#include "scripting/display_effect.h"
+#include "wrapper.hpp"
+#include "wrapper_util.hpp"
+#include "sector.hpp"
+#include "file_system.hpp"
+#include "game_session.hpp"
+#include "resources.hpp"
+#include "physfs/physfs_stream.hpp"
+#include "object/text_object.hpp"
+#include "object/scripted_object.hpp"
+#include "object/display_effect.hpp"
+#include "scripting/sound.hpp"
+#include "scripting/scripted_object.hpp"
+#include "scripting/display_effect.hpp"
 
 static void printfunc(HSQUIRRELVM, const char* str, ...)
 {
@@ -63,15 +63,14 @@ ScriptInterpreter::ScriptInterpreter(const std::string& new_working_directory)
   sq_setprintfunc(v, printfunc);
   
   // register supertux API
-  register_functions(v, supertux_global_functions);
-  register_classes(v, supertux_classes);
+  SquirrelWrapper::register_supertux_wrapper(v);
 
   // expose some "global" objects
   sound = new Scripting::Sound();
-  expose_object(sound, "Sound", "Sound");  
+  expose_object(sound, "Sound");
   
   level = new Scripting::Level();
-  expose_object(level, "Level", "Level");
+  expose_object(level, "Level");
 }
 
 void
@@ -86,20 +85,19 @@ ScriptInterpreter::register_sector(Sector* sector)
     if(!scripted_object)
       continue;
     
-    expose_object(scripted_object, scripted_object->get_name(), 
-        "ScriptedObject");
+    expose_object(scripted_object, scripted_object->get_name());
   }
   
   TextObject* text_object = new TextObject();
   sector->add_object(text_object);
   Scripting::Text* text = static_cast<Scripting::Text*> (text_object);
-  expose_object(text, "Text", "Text");
+  expose_object(text, "Text");
   
   DisplayEffect* display_effect = new DisplayEffect();
   sector->add_object(display_effect);
   Scripting::DisplayEffect* display_effect_api
     = static_cast<Scripting::DisplayEffect*> (display_effect);
-  expose_object(display_effect_api, "DisplayEffect", "DisplayEffect");
+  expose_object(display_effect_api, "DisplayEffect");
 }
 
 ScriptInterpreter::~ScriptInterpreter()
@@ -145,39 +143,6 @@ ScriptInterpreter::run_script(std::istream& in, const std::string& sourcename,
 }
 
 void
-ScriptInterpreter::expose_object(void* object, const std::string& name,
-                                 const std::string& type)
-{
-  // part1 of registration of the instance in the root table
-  sq_pushroottable(v);
-  sq_pushstring(v, name.c_str(), -1);
-
-  // resolve class name
-  sq_pushroottable(v);
-  sq_pushstring(v, type.c_str(), -1);
-  if(sq_get(v, -2) < 0) {
-    std::ostringstream msg;
-    msg << "Couldn't resolve squirrel type '" << type << "'.";
-    throw std::runtime_error(msg.str());
-  }
-  sq_remove(v, -2); // remove roottable
-  
-  // create an instance and set pointer to c++ object
-  if(sq_createinstance(v, -1) < 0 || sq_setinstanceup(v, -1, object)) {
-    std::ostringstream msg;
-    msg << "Couldn't setup squirrel instance for object '"
-        << name << "' of type '" << type << "'.";
-    throw SquirrelError(v, msg.str());
-  }
-  
-  sq_remove(v, -2); // remove class from stack
-  
-  // part2 of registration of the instance in the root table
-  if(sq_createslot(v, -3) < 0)
-    throw SquirrelError(v, "Couldn't register object in squirrel root table");    sq_pop(v, 1);
-}
-
-void
 ScriptInterpreter::set_wakeup_time(float seconds)
 {
   wakeup_timer.start(seconds);