cocos2dx中使用tolua++使lua调用c++函数
2015-04-26 19:08
465 查看
一直想学学cocos2dx中如何使用tolua++工具使得lua脚本调用C++函数,今天就来搞一下,顺便记录下来:
首先,我们打开cocos2dx-2.2.4中projects下的test的VS工程,可以看到这个例子里面已经有一个HelloWorld的类,我们就用它来说明一下。
然后,我们照着HelloWorld类的定义来写pkg文件:
我们打开README文件可以看看书写pkg文件的语法:
1. Generating the lua<-->C bindings with tolua++
Build scripts for windows (build.bat) and unix (build.sh) are provided
to generate the relevant files after modifying the .pkg files. These
scripts basically run the following command:
tolua++.exe -L basic.lua -o LuaCocos2d.cpp Cocos2d.pkg
This will generate the bindings file and patch it with come cocos2dx
specific modifications.
On POSIX systems you can also just run "make" to build the bindings
if/when you change .pkg files.
2. Writing .pkg files
1) enum keeps the same
2) remove CC_DLL for the class defines, pay attention to multi inherites
3) remove inline keyword for declaration and implementation
4) remove public protect and private
5) remove the decalration of class member variable
6) keep static keyword
7) remove memeber functions that declared as private or protected
有一点还需要注意的是static cocos2d::CCScene* scene(); 这句话最好写成static CCScene* scene();不然的话会出现下面这种错误:
error in function 'runWithScene'.
argument #2 is 'cocos2d::CCScene'; 'CCScene' expected.
原因我也不太清楚,按照错误提示替换掉就行了。
接着,我们把写好的pkg文件放在tolua++目录下,在cocos2d.pkg文件末尾添加$pfile "MyClass.pkg",点击运行build.bat,这个文件主要是将cocos2d.pkg内包含的pkg文件整合生成LuaCocos2d.cpp,看下内部语法就知道了:
tolua++ -L basic.lua -o "../../scripting/lua/cocos2dx_support/LuaCocos2d.cpp" Cocos2d.pkg
然后,我们在LuaCocos2d.h中引入我们的HelloWorld的头文件,在将lualib工程添加到该工程来,在该工程添加lua头文件的引用,在链接其中添加lua51.lib和lualib.lib。
接下来我们添加一个lua脚本HelloWorld.lua,内容如下:
在AppDelegate.cpp引入CCLuaEngine.h头文件,代码稍作如下修改:
最后运行看看:
首先,我们打开cocos2dx-2.2.4中projects下的test的VS工程,可以看到这个例子里面已经有一个HelloWorld的类,我们就用它来说明一下。
然后,我们照着HelloWorld类的定义来写pkg文件:
//MyClass.pkg class HelloWorld : public cocos2d::CCLayer { virtual bool init(); static cocos2d::CCScene* scene(); void menuCloseCallback(CCObject* pSender); static HelloWorld* create(); };
我们打开README文件可以看看书写pkg文件的语法:
1. Generating the lua<-->C bindings with tolua++
Build scripts for windows (build.bat) and unix (build.sh) are provided
to generate the relevant files after modifying the .pkg files. These
scripts basically run the following command:
tolua++.exe -L basic.lua -o LuaCocos2d.cpp Cocos2d.pkg
This will generate the bindings file and patch it with come cocos2dx
specific modifications.
On POSIX systems you can also just run "make" to build the bindings
if/when you change .pkg files.
2. Writing .pkg files
1) enum keeps the same
2) remove CC_DLL for the class defines, pay attention to multi inherites
3) remove inline keyword for declaration and implementation
4) remove public protect and private
5) remove the decalration of class member variable
6) keep static keyword
7) remove memeber functions that declared as private or protected
有一点还需要注意的是static cocos2d::CCScene* scene(); 这句话最好写成static CCScene* scene();不然的话会出现下面这种错误:
error in function 'runWithScene'.
argument #2 is 'cocos2d::CCScene'; 'CCScene' expected.
原因我也不太清楚,按照错误提示替换掉就行了。
接着,我们把写好的pkg文件放在tolua++目录下,在cocos2d.pkg文件末尾添加$pfile "MyClass.pkg",点击运行build.bat,这个文件主要是将cocos2d.pkg内包含的pkg文件整合生成LuaCocos2d.cpp,看下内部语法就知道了:
tolua++ -L basic.lua -o "../../scripting/lua/cocos2dx_support/LuaCocos2d.cpp" Cocos2d.pkg
然后,我们在LuaCocos2d.h中引入我们的HelloWorld的头文件,在将lualib工程添加到该工程来,在该工程添加lua头文件的引用,在链接其中添加lua51.lib和lualib.lib。
接下来我们添加一个lua脚本HelloWorld.lua,内容如下:
print("begin ...... ") local director = CCDirector:sharedDirector() local scene = HelloWorld:scene() director:runWithScene(scene) print("end ........ ")
在AppDelegate.cpp引入CCLuaEngine.h头文件,代码稍作如下修改:
//CCScene *pScene = HelloWorld::scene(); //pDirector->runWithScene(pScene); CCScriptEngineProtocol* pEngine = CCLuaEngine::defaultEngine(); CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine); string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename("HelloWorld.lua"); CCLuaEngine::defaultEngine()->executeScriptFile(fullpath.c_str());
最后运行看看:
![](http://images.cnitblog.com/blog2015/303959/201504/261907181973067.png)
相关文章推荐
- cocos2dx中注册导出c++函数供lua调用(使用tolua++)
- 使用tolua++实现C++与LUA相互调用
- Cocos2d-Lua 3.12 生成绑定 导出C++类给LUA调用( tolua genbindings.py 的使用)
- Quick-Cocos2d-x 使用tolua工具导出C++的类给Lua调用
- cocos2dx lua中使用自定义类以及tolua++的使用
- 使用tolua++实现C++与LUA相互调用
- 【COCOS2DX-LUA 脚本开发之四】使用tolua++编译pk创建自定义类 推荐
- 【COCOS2DX-LUA 脚本开发之四】使用tolua++编译pkg,从而创建自定义类让Lua脚本使用
- Quick-Cocos2d-x 使用tolua工具导出C++的类给Lua调用
- COCOS2DX与LUA互相调用使用方法
- cocos2dx-lua中如何使用自定义类以及tolua++的使用
- cocos2dx lua中使用自定义类以及tolua++的使用
- Cocos2d-Lua 3.12 生成绑定 导出C++类给LUA调用( tolua genbindings.py 的使用)
- 使用tolua++实现C++与LUA相互调用
- Quick-Cocos2d-x 使用tolua工具导出C++的类给Lua调用
- tolua 使用 Lua调用c++多返回值函数
- 【COCOS2DX-LUA 脚本开发之四】使用tolua++编译pkg,从而创建自定义类让Lua脚本使用
- Quick-Cocos2d-x 2.26 使用tolua工具导出C++的类给Lua调用WINDOWS篇
- Tolua使用,利用pkg文件,封装自己的lua支持的cocos2dx的接口
- Cocos2dx-lua中如何使用自定义类以及tolua++的使用