Cocos2dx -- automatically and manually bound lua
The main steps are as follows:
1. Compile the c ++ class to be exported. If it is added in libcocos2d, add the export Tag: class CC_DLL Test.
2. Configure the environment according to README. mdown in the tolua directory:
* Make sure that you have installed `android-ndk-r9b`.* Download python2.7.3 (32bit) from (http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi).* Add the installed path of python (e.g. C:Python27) to windows environment variable named 'PATH'.* Download pyyaml from http://pyyaml.org/download/pyyaml/PyYAML-3.10.win32-py2.7.exe and install it.* Download pyCheetah from https://raw.github.com/dumganhar/my_old_cocos2d-x_backup/download/downloads/Cheetah.zip, unzip it to C:Python27Libsite-packages* Set environment variables (`NDK_ROOT`)* Go to cocos2d-x/tools/tolua folder, and run genbindings.py. The generated codes will be under cocosscriptingauto-generatedjs-bindings.
3. Create an ini file, such as cocos2dx_custom.ini, and modify it by referring to other ini files:
# the prefix to be added to the generated functions. You might or might not use this in your own# templatesprefix = cocos2dx_custom# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)# all classes will be embedded in that namespacetarget_namespace = cc# what headers to parseheaders = %(cocosdir)s/cocos/for_lua/Test.h# what classes to produce code for. You can use regular expressions here. When testing the regular# expression, it will be enclosed in ^$, like this: ^Menu*$.classes = Test.*skip = # classes for which there will be no parent lookupclasses_have_no_parents = Testabstract_classes =
4. Copy genbindings_custom.py and modify pai_args:
cmd_args = {'cocos2dx_custom.ini' : ('cocos2dx_custom', 'lua_cocos2dx_custom_auto'), }
5. Running genbindings_custom.py will generate xx_auto.h/cpp to the cocosscriptinglua-bindingsauto directory, and then you will add it to the libluaco cos2d project of the engine.
6. You need to use it in lua and register it at startup. In version 3.7, AppDelegate: applicationDidFinishLaunching will call lua_module_register. Therefore:
#include lua_cocos2dx_custom_auto.hpp#include fun.hint lua_module_register(lua_State* L){ register_cocosdenshion_module(L);register_all_cocos2dx_custom(L);register_foo(L); return 1;}
7. Then you can use it in lua:
-- test custom local msg = cc.Test:helloMsg() print(msg)
[Manual binding]
Reference: http://www.tairan.com/archives/5493
1. Create a c ++ class (fun. h ):
#pragma once#include
#include
extern C{#include
#include
#include
}class Foo{public:Foo(const std::string & name) : name(name){std::cout << Foo is born << std::endl;}std::string Add(int a, int b){std::stringstream ss;ss << name << : << a << + << b << = << (a + b);return ss.str();}~Foo(){std::cout << Foo is gone << std::endl;}private:std::string name;};void register_foo(lua_State *L);
2. Export to lua (fun. cpp ):
#include fun.hint l_Foo_constructor(lua_State *L) {const char *name = luaL_checkstring(L, 1);Foo **udata = (Foo**)lua_newuserdata(L, sizeof(Foo*));*udata = new Foo(name);luaL_getmetatable(L, luaL_Foo);// stack:// -1metatable luaL_Foo// -2userdata// -3string paramlua_setmetatable(L, -2);return 1;}Foo* l_CheckFoo(lua_State *L, int n) {return *(Foo**)luaL_checkudata(L, n, luaL_Foo);}int l_Foo_Add(lua_State *L) {Foo *foo = l_CheckFoo(L, 1);int a = luaL_checknumber(L, 2);int b = luaL_checknumber(L, 3);std::string s = foo->Add(a, b);lua_pushstring(L, s.c_str());// stack:// -1 result string// -2 metatable luaL_Foo// -3 userdata// -4 string paramreturn 1;}int l_Foo_destructor(lua_State *L) {Foo *foo = l_CheckFoo(L, 1);delete foo;return 0;}void register_foo(lua_State *L) {luaL_Reg sFooRefs[] = {{ new, l_Foo_constructor },{ add, l_Foo_Add },{ __gc, l_Foo_destructor },{ NULL, NULL }};luaL_newmetatable(L, luaL_Foo);luaL_register(L, NULL, sFooRefs);lua_pushvalue(L, -1);// stack:// -1: metatable luaL_Foo// -2: metatable luaL_Foo// this pops the stacklua_setfield(L, -1, __index);lua_setglobal(L, Foo);}
3. Register at startup
4. Use in lua:
function Foo:speak()print(hello, i am a Foo) end local foo = Foo.new(adfan) local m = foo:add(3, 4) print(m) foo:speak() Foo.add_ = Foo.add function Foo:add(a, b)return magic: .. self:add_(a, b) end m = foo:add(9, 8) print(m)
Note: For manual binding, refer to the manual. cpp exported by the engine. It is very convenient to use the interfaces of tolua. The above method mainly demonstrates this process.