一直想学学cocos2dx中如何使用tolua++工具使得lua脚本调用C++函数,今天就来搞一下,顺便记录下来:

首先,我们打开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());

最后运行看看:

cocos2dx中使用tolua++使lua调用c++函数的更多相关文章

  1. 使用BabeLua在cocos2d-x中编辑和调试Lua

    使用BabeLua在cocos2d-x中编辑和调试Lua BabeLua是一款基于VS2012/2013的Lua集成开发环境,具有Lua语法高亮,语法检查.自己主动补全.高速搜索,注入宿主程序内对Lu ...

  2. Step By Step(Lua调用C函数)

    原文: http://www.cnblogs.com/stephen-liu74/archive/2012/07/23/2469902.html Lua可以调用C函数的能力将极大的提高Lua的可扩展性 ...

  3. cocos2d-x中CCEditbox导出到lua

    自从工作后感觉时间较少(每天工作9-22,晚上就不想动了,早上想多睡点),工作中用的是 cocos2d-x.cocos2d-x是一款手机游戏引擎,虽然支持lua,但和love2d相比非纯lua游戏引 ...

  4. lua调用c++函数返回值作用

    2015/05/28 lua调用c++接口,返回给lua函数的是压入栈的内容,可以有多个返回值.但是c++接口本身也是有返回值的,这个返回值也非常的重要,会决定最后返回到lua函数的值的个数. (1) ...

  5. lua调用c函数

    参考:http://blog.163.com/madahah@126/blog/static/170499225201121504936823/ 1.编辑C程序 vim luac.c #include ...

  6. 使用BabeLua3.x在cocos2d-x中编辑和调试Lua

    BabeLua是一款基于VS2012/2013的Lua集成开发环境,具有Lua语法高亮,语法检查,自动补全,快速搜索,注入宿主程序内对Lua脚本进行调试,设置断点观察变量值,查看堆栈信息等功能. 如何 ...

  7. (原+译)LUA调用C函数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5804924.html 原始网址: http://www.troubleshooters.com/cod ...

  8. asp.net中<input type=button>无法调用后台函数

    例如:用<input id="bt1" type="button" runat="server" Onclick="btnL ...

  9. Lua中调用C函数

    Lua利用一个虚拟的堆栈来给C传递值或从C获取值.每当Lua调用C函数,都会获得一个新的堆栈,该堆栈初始包含所有的调用C函数所需要的参数值(Lua传给C函数的调用实参),并且C函数执行完毕后,会把返回 ...

随机推荐

  1. 使用defined和require引入js

    define(function(require, exports, module) { var ceshiTwo = { dataCeshi:[1,2,3,4,5], arrCeshi:functio ...

  2. Spring bean作用域

    全当知识要点记录了,大家随意踩踩. spring的作用域有以下几种singleton作用域prototype作用域request作用域session作用域global-session作用域 1. si ...

  3. FreeFileSync 4.2 发布,文件夹比较和同步工具

    FreeFileSync 是一款开源的文件夹比较和同步工具,可用于 Win 和 Lin 平台,最近发布了 4.2 版本. FreeFileSync 采用双面板设计,使用方法很简单: 在左面版和右面版上 ...

  4. 【linux】文档查看

    cat: [root@localhost test]# cat log2013.log 2013-01 2013-02 2013-03 [root@localhost test]# cat -n lo ...

  5. 手淘flexible.js框架使用和源代码讲解

    手淘框架是一个用来适配移动端的js框架,下面我们来讲解一下如何使用手淘的这套框架. 其实手淘框架的核心原理就是根据不同的width给网页中html跟节点设置不同的font-size,然后所有的距离大小 ...

  6. 移动端开发之px,em和rem详解

    px:表示的是绝对的像素值,1px就是1像素大小 em:关于em,网上有资料说是关于父元素的,但是其实个人感觉这种说法是不对的,其实em的大小是根据自身的font-size确定的,而只是正常的情况下子 ...

  7. (转)ASP连接sql server实例解析

    本文转载自:http://blog.csdn.net/xys_777/article/details/5696276 1.首先确定自己的iis没有问题 2.其次确定自己sqlserver没有问题 然后 ...

  8. C++中如何强制inline函数(MSVC, GCC)

    #ifdef _MSC_VER_ // for MSVC #define forceinline __forceinline #elif defined __GNUC__ // for gcc on ...

  9. solr查询优化(实践了一下效果比较明显)

    什么是filtercache? solr应用中为了提高查询速度有可以利用几种cache来优化查询速度,分别是fieldValueCache,queryResultCache,documentCache ...

  10. Oracle NVL与NVL2函数

    nvl( ) 函数 从两个表达式返回一个非 null 值. 语法 NVL(eExpression1, eExpression2) 参数 eExpression1, eExpression2 如果 eE ...