[Orxonox-commit 820] r3340 - in branches/resource/src: core orxonox/gamestates orxonox/gui

rgrieder at orxonox.net rgrieder at orxonox.net
Thu Jul 23 20:47:55 CEST 2009


Author: rgrieder
Date: 2009-07-23 20:47:55 +0200 (Thu, 23 Jul 2009)
New Revision: 3340

Modified:
   branches/resource/src/core/LuaBind.cc
   branches/resource/src/core/LuaBind.h
   branches/resource/src/orxonox/gamestates/GSRoot.cc
   branches/resource/src/orxonox/gui/GUIManager.cc
Log:
Generalised use of tolua interfaces a bit: GSRoot now adds the interface functions to LuaBind which supports two new functions: openToluaInterfaces and closeToluaInterfaces. Both accept a lua state and can therefore be used to inject our own tolua bindings (the GUIManager makes use of that).
There's also something new: When loading a level, you could now use the tolua bindings from Orxonox as well (not just those from Core).

Modified: branches/resource/src/core/LuaBind.cc
===================================================================
--- branches/resource/src/core/LuaBind.cc	2009-07-23 18:04:51 UTC (rev 3339)
+++ branches/resource/src/core/LuaBind.cc	2009-07-23 18:47:55 UTC (rev 3340)
@@ -38,7 +38,6 @@
 
 #include "util/Debug.h"
 #include "util/StringUtils.h"
-#include "ToluaBindCore.h"
 #include "Core.h"
 
 namespace orxonox
@@ -64,11 +63,22 @@
     luaopen_io(luaState_);
     luaopen_debug(luaState_);
 #endif
-    tolua_Core_open(luaState_);
+
+    // Open all available tolua interfaces
+    this->openToluaInterfaces(luaState_);
+
     output_ = "";
     isRunning_ = false;
   }
 
+  LuaBind::~LuaBind()
+  {
+    this->closeToluaInterfaces(luaState_);
+
+    assert(singletonRef_s);
+    LuaBind::singletonRef_s = NULL;
+  };
+
   void LuaBind::luaPrint(const std::string& str)
   {
     output_ += str;
@@ -314,4 +324,26 @@
     return output;
   }
 
+  void LuaBind::addToluaInterface(int (*function)(lua_State*), const std::string& name)
+  {
+    toluaInterfaces_.push_back(std::make_pair(name, function));
+    // Apply changes to our own lua state as well 
+    (*function)(luaState_);
+  }
+
+  void LuaBind::openToluaInterfaces(lua_State* state)
+  {
+    for (unsigned int i = 0; i < toluaInterfaces_.size(); ++i)
+      (*toluaInterfaces_[i].second)(state);
+  }
+
+  void LuaBind::closeToluaInterfaces(lua_State* state)
+  {
+    for (unsigned int i = 0; i < toluaInterfaces_.size(); ++i)
+    {
+      lua_pushnil(state);
+      lua_setglobal(state, toluaInterfaces_[i].first.c_str());
+    }
+  }
+
 }

Modified: branches/resource/src/core/LuaBind.h
===================================================================
--- branches/resource/src/core/LuaBind.h	2009-07-23 18:04:51 UTC (rev 3339)
+++ branches/resource/src/core/LuaBind.h	2009-07-23 18:47:55 UTC (rev 3340)
@@ -57,7 +57,7 @@
 
     public:
       LuaBind();
-      inline ~LuaBind() { assert(singletonRef_s); LuaBind::singletonRef_s = NULL; };
+      ~LuaBind();
 
       inline static LuaBind& getInstance() { assert(singletonRef_s); return *LuaBind::singletonRef_s; } // tolua_export
 
@@ -82,6 +82,10 @@
     inline void setIncludePath(const std::string& includepath)
         { this->includePath_ = includepath; }
 
+    void addToluaInterface(int (*function)(lua_State*), const std::string& name);
+    void openToluaInterfaces(lua_State* state);
+    void closeToluaInterfaces(lua_State* state);
+
     private:
       static LuaBind* singletonRef_s;
 
@@ -90,6 +94,7 @@
       lua_State* luaState_;
       bool isRunning_;
       std::string includePath_;
+      std::vector<std::pair<std::string, int (*)(lua_State *L)> > toluaInterfaces_;
 
   }; // tolua_export
 } // tolua_export

Modified: branches/resource/src/orxonox/gamestates/GSRoot.cc
===================================================================
--- branches/resource/src/orxonox/gamestates/GSRoot.cc	2009-07-23 18:04:51 UTC (rev 3339)
+++ branches/resource/src/orxonox/gamestates/GSRoot.cc	2009-07-23 18:47:55 UTC (rev 3340)
@@ -33,7 +33,10 @@
 #include "core/ConsoleCommand.h"
 #include "core/Game.h"
 #include "core/GameMode.h"
+#include "core/LuaBind.h"
 #include "network/NetworkFunction.h"
+#include "ToluaBindCore.h"
+#include "ToluaBindOrxonox.h"
 #include "tools/Timer.h"
 #include "interfaces/TimeFactorListener.h"
 #include "interfaces/Tickable.h"
@@ -57,6 +60,10 @@
     {
         this->ccSetTimeFactor_ = 0;
         this->ccPause_ = 0;
+
+        // Tell LuaBind about all tolua interfaces
+        LuaBind::getInstance().addToluaInterface(&tolua_Core_open, "Core");
+        LuaBind::getInstance().addToluaInterface(&tolua_Orxonox_open, "Orxonox");
     }
 
     GSRoot::~GSRoot()

Modified: branches/resource/src/orxonox/gui/GUIManager.cc
===================================================================
--- branches/resource/src/orxonox/gui/GUIManager.cc	2009-07-23 18:04:51 UTC (rev 3339)
+++ branches/resource/src/orxonox/gui/GUIManager.cc	2009-07-23 18:47:55 UTC (rev 3340)
@@ -58,8 +58,7 @@
 #include "util/OrxAssert.h"
 #include "core/Core.h"
 #include "core/Clock.h"
-#include "ToluaBindCore.h"
-#include "ToluaBindOrxonox.h"
+#include "core/LuaBind.h"
 
 namespace orxonox
 {
@@ -135,8 +134,7 @@
             guiSystem_.reset(new System(guiRenderer_.get(), resourceProvider_, 0, scriptModule_.get()));
 
             // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
-            tolua_Core_open(this->scriptModule_->getLuaState());
-            tolua_Orxonox_open(this->scriptModule_->getLuaState());
+            LuaBind::getInstance().openToluaInterfaces(this->luaState_);
 
             // initialise the basic lua code
             this->loadLuaCode();
@@ -161,10 +159,7 @@
     GUIManager::~GUIManager()
     {
         // destroy our own tolua interfaces
-        lua_pushnil(luaState_);
-        lua_setglobal(luaState_, "Orxonox");
-        lua_pushnil(luaState_);
-        lua_setglobal(luaState_, "Core");
+        LuaBind::getInstance().closeToluaInterfaces(this->luaState_);
 
         singletonRef_s = 0;
     }




More information about the Orxonox-commit mailing list