diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2011-06-12 21:51:47 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2011-06-12 21:51:47 +0200 |
commit | 48b4b813a51c374fce43a553608c67c6ca08ea37 (patch) | |
tree | 853cfe0c7551b44d3544f776e926e70e9382b121 | |
parent | b44b2cb4c16995d218e8d4c767c01c7b87c89816 (diff) |
Added a say() function to lua.
-rw-r--r-- | game.cpp | 7 | ||||
-rw-r--r-- | game.h | 2 | ||||
-rw-r--r-- | scripting.cpp | 8 | ||||
-rw-r--r-- | scripting.h | 3 | ||||
-rw-r--r-- | scripting/test.cpp | 7 | ||||
-rw-r--r-- | scripting/test.h | 1 | ||||
-rw-r--r-- | scripting/test.pkg | 1 |
7 files changed, 29 insertions, 0 deletions
@@ -10,12 +10,19 @@ Game* Game::game = NULL; Game::Game() : socket(io_service) { scene = new Scene(); + + scene->lua->set_say_func(boost::bind(&Game::say, this, _1)); } Game::~Game() { delete scene; } +void Game::say(const std::string msg) { + message::Message m(msg); + m.send(socket); +} + void Game::run(const std::string host, const unsigned int port) { run(host, boost::lexical_cast<std::string>(port)); } @@ -19,6 +19,8 @@ class Game { Game(); ~Game(); + void say(const std::string msg); + void run(std::string host, unsigned int port); void run(std::string host, std::string port); diff --git a/scripting.cpp b/scripting.cpp index ed671a2..520ee9d 100644 --- a/scripting.cpp +++ b/scripting.cpp @@ -27,6 +27,10 @@ void Lua::set_log_func(boost::function<void (const char*)> log_func) { log_func("Lua::set_log_func() called"); } +void Lua::set_say_func(boost::function<void (const char*)> say_func) { + this->say_func = say_func; +} + void Lua::print(const char *s) { if(log_func) log_func(s); @@ -34,6 +38,10 @@ void Lua::print(const char *s) { std::cout << s << std::endl; } +void Lua::say(const char *s) { + say_func(s); +} + void Lua::dostring(std::string s) { int before = lua_gettop(L); int err = luaL_dostring(L, s.c_str()); diff --git a/scripting.h b/scripting.h index d14aa3c..4b0bacf 100644 --- a/scripting.h +++ b/scripting.h @@ -16,13 +16,16 @@ class Lua { protected: lua_State *L; boost::function<void (const char*)> log_func; + boost::function<void (const char*)> say_func; public: Lua(); virtual ~Lua(); void set_log_func(boost::function<void (const char*)> log_func); + void set_say_func(boost::function<void (const char*)> say_func); void print(const char *s); + void say(const char *s); void dostring(std::string s); }; diff --git a/scripting/test.cpp b/scripting/test.cpp index 1a378b7..5ec27da 100644 --- a/scripting/test.cpp +++ b/scripting/test.cpp @@ -9,3 +9,10 @@ void print(lua_State *L, const char *s) { lua_pop(L, 1); lua->print(s); } + +void say(lua_State *L, const char *s) { + lua_getglobal(L, "lua_instance"); + Lua *lua = static_cast<Lua*>(lua_touserdata(L, -1)); + lua_pop(L, 1); + lua->say(s); +} diff --git a/scripting/test.h b/scripting/test.h index 48f560e..7ff5719 100644 --- a/scripting/test.h +++ b/scripting/test.h @@ -4,5 +4,6 @@ #include <lua.hpp> void print(lua_State *L, const char *s); +void say(lua_State *L, const char *s); #endif diff --git a/scripting/test.pkg b/scripting/test.pkg index 38525c6..0f64be7 100644 --- a/scripting/test.pkg +++ b/scripting/test.pkg @@ -1,5 +1,6 @@ $hfile "scripting/test.h" void print(lua_State *L, const char *s); +void say(lua_State *L, const char *s); // vim: syn=cpp |