From 48b4b813a51c374fce43a553608c67c6ca08ea37 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 12 Jun 2011 21:51:47 +0200 Subject: Added a say() function to lua. --- game.cpp | 7 +++++++ game.h | 2 ++ scripting.cpp | 8 ++++++++ scripting.h | 3 +++ scripting/test.cpp | 7 +++++++ scripting/test.h | 1 + scripting/test.pkg | 1 + 7 files changed, 29 insertions(+) diff --git a/game.cpp b/game.cpp index d198720..42e8b53 100644 --- a/game.cpp +++ b/game.cpp @@ -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(port)); } diff --git a/game.h b/game.h index 7b3ad55..44222c2 100644 --- a/game.h +++ b/game.h @@ -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 log_func) { log_func("Lua::set_log_func() called"); } +void Lua::set_say_func(boost::function 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 log_func; + boost::function say_func; public: Lua(); virtual ~Lua(); void set_log_func(boost::function log_func); + void set_say_func(boost::function 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_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 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 -- cgit v1.2.3