From: Matthias Braun Date: Sat, 2 Jul 2005 12:06:32 +0000 (+0000) Subject: use new exposing functions X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=3b3e6ad939a9ce78271597b980d63b44c3ca000c;p=supertux.git use new exposing functions SVN-Revision: 2683 --- diff --git a/src/scripting/script_interpreter.cpp b/src/scripting/script_interpreter.cpp index 72bbb1adc..91e326356 100644 --- a/src/scripting/script_interpreter.cpp +++ b/src/scripting/script_interpreter.cpp @@ -67,10 +67,10 @@ ScriptInterpreter::ScriptInterpreter(const std::string& new_working_directory) // 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 @@ -85,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 (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 (display_effect); - expose_object(display_effect_api, "DisplayEffect", "DisplayEffect"); + expose_object(display_effect_api, "DisplayEffect"); } ScriptInterpreter::~ScriptInterpreter() @@ -144,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); diff --git a/src/scripting/script_interpreter.hpp b/src/scripting/script_interpreter.hpp index 3788c3038..c1b7af0a1 100644 --- a/src/scripting/script_interpreter.hpp +++ b/src/scripting/script_interpreter.hpp @@ -3,8 +3,12 @@ #include #include +#include +#include #include "timer.hpp" #include "game_object.hpp" +#include "scripting/wrapper.hpp" +#include "scripting/wrapper_util.hpp" #include "scripting/sound.hpp" #include "scripting/level.hpp" @@ -23,9 +27,26 @@ public: void run_script(std::istream& in, const std::string& sourcename = "", bool remove_when_terminated = true); - - void expose_object(void* object, const std::string& name, - const std::string& type); + + template + void expose_object(T* object, const std::string& name, bool free = false) + { + sq_pushroottable(v); + sq_pushstring(v, name.c_str(), -1); + + sq_pushroottable(v); + SquirrelWrapper::create_squirrel_instance(v, object, free); + sq_remove(v, -2); + + // register instance in root table + if(sq_createslot(v, -3) < 0) { + std::ostringstream msg; + msg << "Couldn't register object '" << name << "' in squirrel root table"; + throw SquirrelError(v, msg.str()); + } + + sq_pop(v, 1); + } void set_wakeup_time(float seconds);