cocos2dx lua 注册 class

原理介绍

http://www.cnitblog.com/luckydmz/archive/2012/08/02/83959.html

关于Lua与C/C++的交互
C/C++调用Lua的函数还是比较简单的,可以参考lua tinker的实现。
Lua调用C/C++的函数如果用最原始的方式有很大限制,只能调用类型为
typedef int (*lua_CFunction) (lua_State *L);
的函数,如果想调用任意类型的函数甚至C++的类成员函数就需要进行一些封装,有一些现成的库已经帮我们实现了。我试下来比较好用的就是lua tinker和tolua++。

lua tinker
license未知,官网没有找到,可以从这里下载到http://download.csdn.net/download/esrrhs/3838889
体积小,只有2个文件lua_tinker.h/lua_tinker.cpp
因为采用C++模板实现,绑定函数的参数个数有限制,当然可以自己扩展。
函数绑定到lua中的名称可以自定义,可以与C++中不同,例如加上特定前缀或后缀。
除了函数绑定还lua tinker还封装了函数调用,读取设置变量等操作。

tolua++
使用MIT license。官网http://www.codenix.com/~tolua/
这其实是一个工具,编写.pkg文件声明绑定的函数和类,通过tolua++.exe生成绑定代码(.cpp),然后在程序中调用生成的绑定代码来完成绑定。
需要通过下载的文件编译出tolua++.exe和一个lib,lib用来和自己的工程链接以支持绑定。
tolua++是用C实现的,因为绑定代码是通过工具分析pkg生成的,没有参数上的限制。
但限制了绑定函数在lua中的名称和C++中相同。
为了方便我将.pkg文件写成了
$#include "lua_interface.h"
$ifile "lua_interface.h"
将需要提供给lua的接口全部声明在lua_interface.h中。
当接口发生变化后,使用
tolua++.exe -n lua_interface -o lua_interface.cpp lua_interface.pkg
更新.cpp文件。

参考http://www.aichengxu.com/article/C%20%20/13083_4.html

A tolua++.exe 使用生成 猿文件

1 新建 MyClass.h

 1 class MyClass
 2 {
 3 public:
 4     MyClass(){};
 5     
 6     static MyClass* create()
 7     {
 8         return new MyClass();
 9     };
10     
11     static int f(int a){return 10;};
12     int fun(){return 20;}
13 };

2 新建 MyClass.pkg

class MyClass
{
    static MyClass* create()
    {
        return new MyClass();
    };
    static int f(int a);
    int fun(){return 20;}
};

3 新建 MyCTol.h

#ifndef __MY_CPPTOLUA_H_
#define __MY_CPPTOLUA_H_


extern "C" {
#include "tolua++.h"
#include "tolua_fix.h"
}
#include "MyClass.h" 
TOLUA_API int tolua_MyCToL_open(lua_State* tolua_S);
#endif 
// __MY_CPPTOLUA_H_

4 新建 MyCToL.pkg

$#include "MyCToL.h"
 
$pfile "MyClass.pkg"

5  find your cocos2d-x root\tools\tolua++\tolua++.rar and 解压成 tolua++.exe

cmd 进入 tolua++.exe 所在目录 (MyCToL.pkg ,MyCToL.h,MyClass.pkg 也copy到该目录下

cmd 执行 tolua++ -tCocos2d -o MyCToL.cpp MyCToL.pkg 命令

8  目录下会生成 MyCToL.cpp

到此为止程序所需猿文件已经生成

//--------------------------end A tolua++.exe 使用生成 猿文件

B 如何使用

1 新贱 CallMyClass.lua 文件

 

-- cclog
cclog = function(...)
    print(string.format(...))
end -- end function cclog

-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
    print("----------------------------------------")
    print("LUA ERROR: " .. tostring(msg) .. "\n")
    print(debug.traceback())
    print("----------------------------------------")
end -- end function __G__TRACKBACK__

function main()
    -- avoid memory leak
    collectgarbage("setpause", 100)
    collectgarbage("setstepmul", 5000)
    
    local rs= MyClass:f(3)
    cclog(rs)
    
    local test = MyClass:create()
    print("test:fun()=",test:fun())
    
end -- end function main

xpcall(main, __G__TRACKBACK__)

 

2  在调用lua的cpp中 (如 AppDelegate.cpp)中 #include "MyCToL.h"

3  注册class 到 CCLuaEngine 在调用lua的cpp中 (如 AppDelegate.cpp)中

CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();下面一行添加
tolua_MyCToL_open(pEngine->getLuaStack()->getLuaState()); 

4 pEngine 执行 CallMyClass.lua (CallMyClass.lua在Prj\Resources下)

pEngine->executeScriptFile("CallMyClass.lua");

//--------------------------end B 如何使用

感谢作者分享:)

http://www.cnblogs.com/mrblue/archive/2013/06/08/3126997.html

感谢作者分享:)

http://blog.sina.com.cn/s/blog_62b2318d0101g1vl.html

 

 

完!

posted @ 2014-05-30 20:32  thc  阅读(641)  评论(0)    收藏  举报