Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3340 for code/branches


Ignore:
Timestamp:
Jul 23, 2009, 8:47:55 PM (15 years ago)
Author:
rgrieder
Message:

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).

Location:
code/branches/resource/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/resource/src/core/LuaBind.cc

    r3301 r3340  
    3939#include "util/Debug.h"
    4040#include "util/StringUtils.h"
    41 #include "ToluaBindCore.h"
    4241#include "Core.h"
    4342
     
    6564    luaopen_debug(luaState_);
    6665#endif
    67     tolua_Core_open(luaState_);
     66
     67    // Open all available tolua interfaces
     68    this->openToluaInterfaces(luaState_);
     69
    6870    output_ = "";
    6971    isRunning_ = false;
    7072  }
     73
     74  LuaBind::~LuaBind()
     75  {
     76    this->closeToluaInterfaces(luaState_);
     77
     78    assert(singletonRef_s);
     79    LuaBind::singletonRef_s = NULL;
     80  };
    7181
    7282  void LuaBind::luaPrint(const std::string& str)
     
    315325  }
    316326
     327  void LuaBind::addToluaInterface(int (*function)(lua_State*), const std::string& name)
     328  {
     329    toluaInterfaces_.push_back(std::make_pair(name, function));
     330    // Apply changes to our own lua state as well
     331    (*function)(luaState_);
     332  }
     333
     334  void LuaBind::openToluaInterfaces(lua_State* state)
     335  {
     336    for (unsigned int i = 0; i < toluaInterfaces_.size(); ++i)
     337      (*toluaInterfaces_[i].second)(state);
     338  }
     339
     340  void LuaBind::closeToluaInterfaces(lua_State* state)
     341  {
     342    for (unsigned int i = 0; i < toluaInterfaces_.size(); ++i)
     343    {
     344      lua_pushnil(state);
     345      lua_setglobal(state, toluaInterfaces_[i].first.c_str());
     346    }
     347  }
     348
    317349}
  • code/branches/resource/src/core/LuaBind.h

    r3196 r3340  
    5858    public:
    5959      LuaBind();
    60       inline ~LuaBind() { assert(singletonRef_s); LuaBind::singletonRef_s = NULL; };
     60      ~LuaBind();
    6161
    6262      inline static LuaBind& getInstance() { assert(singletonRef_s); return *LuaBind::singletonRef_s; } // tolua_export
     
    8383        { this->includePath_ = includepath; }
    8484
     85    void addToluaInterface(int (*function)(lua_State*), const std::string& name);
     86    void openToluaInterfaces(lua_State* state);
     87    void closeToluaInterfaces(lua_State* state);
     88
    8589    private:
    8690      static LuaBind* singletonRef_s;
     
    9195      bool isRunning_;
    9296      std::string includePath_;
     97      std::vector<std::pair<std::string, int (*)(lua_State *L)> > toluaInterfaces_;
    9398
    9499  }; // tolua_export
  • code/branches/resource/src/orxonox/gamestates/GSRoot.cc

    r3304 r3340  
    3434#include "core/Game.h"
    3535#include "core/GameMode.h"
     36#include "core/LuaBind.h"
    3637#include "network/NetworkFunction.h"
     38#include "ToluaBindCore.h"
     39#include "ToluaBindOrxonox.h"
    3740#include "tools/Timer.h"
    3841#include "interfaces/TimeFactorListener.h"
     
    5861        this->ccSetTimeFactor_ = 0;
    5962        this->ccPause_ = 0;
     63
     64        // Tell LuaBind about all tolua interfaces
     65        LuaBind::getInstance().addToluaInterface(&tolua_Core_open, "Core");
     66        LuaBind::getInstance().addToluaInterface(&tolua_Orxonox_open, "Orxonox");
    6067    }
    6168
  • code/branches/resource/src/orxonox/gui/GUIManager.cc

    r3339 r3340  
    5959#include "core/Core.h"
    6060#include "core/Clock.h"
    61 #include "ToluaBindCore.h"
    62 #include "ToluaBindOrxonox.h"
     61#include "core/LuaBind.h"
    6362
    6463namespace orxonox
     
    136135
    137136            // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
    138             tolua_Core_open(this->scriptModule_->getLuaState());
    139             tolua_Orxonox_open(this->scriptModule_->getLuaState());
     137            LuaBind::getInstance().openToluaInterfaces(this->luaState_);
    140138
    141139            // initialise the basic lua code
     
    162160    {
    163161        // destroy our own tolua interfaces
    164         lua_pushnil(luaState_);
    165         lua_setglobal(luaState_, "Orxonox");
    166         lua_pushnil(luaState_);
    167         lua_setglobal(luaState_, "Core");
     162        LuaBind::getInstance().closeToluaInterfaces(this->luaState_);
    168163
    169164        singletonRef_s = 0;
Note: See TracChangeset for help on using the changeset viewer.