Cocos2d-x3.0 lua绑定C++类

这里记录下我实现Lua绑定的全过程。

原文地址:http://blog.csdn.net/qqmcy/article/details/26099859

准备工作:

1、创一个一个Lua的2dx工程。(这个网上已经有好多了)

2、创一个C++类。

TestScene.h  这个只是一个简单的场景

  1. //  
  2. //  TestScene.h  
  3. //  uitestLua  
  4. //  
  5. //  Created by 杜甲 on 14-5-17.  
  6. //  
  7. //  
  8.   
  9. #ifndef __uitestLua__TestScene__  
  10. #define __uitestLua__TestScene__  
  11.   
  12. #include "cocos2d.h"  
  13. USING_NS_CC;  
  14.   
  15.   
  16.   
  17. class TestScene :public Layer{  
  18.       
  19. public:  
  20.     static Scene* createScene();  
  21.     virtual bool init();  
  22.       
  23.     CREATE_FUNC(TestScene);  
  24. };  
  25.   
  26. #endif /* defined(__uitestLua__TestScene__) */  


TestScene.cpp

  1. //  
  2. //  TestScene.cpp  
  3. //  uitestLua  
  4. //  
  5. //  Created by 杜甲 on 14-5-17.  
  6. //  
  7. //  
  8.   
  9. #include "TestScene.h"  
  10. Scene* TestScene::createScene()  
  11. {  
  12.     auto layer= TestScene::create();  
  13.     auto scene = Scene::create();  
  14.     scene->addChild(layer);  
  15.     return scene;  
  16.       
  17. }  
  18.   
  19. bool TestScene::init()  
  20. {  
  21.     bool bRet = false;  
  22.     do {  
  23.         CC_BREAK_IF(!Layer::init());  
  24.           
  25.         auto sprite = Sprite::create("res/dog.png");  
  26.         sprite->setPosition(Point(100, 200));  
  27.         addChild(sprite);  
  28.         bRet = true;  
  29.     } while (0);  
  30.     return bRet;  
  31. }  

上面的代码很简单没什么好说的。接下是本文的主要内容。将上面的C++类绑定Lua

步骤:

1、绑定基本变量:这个按照readme中的去做,注意NDK要用:r9b一定要用这个。

2、编译自己的ini文件。这个需要参照其他的ini。我参考的是cocos2dx_physics.ini

自己创建一个文本文件将cocos2dx_physics.ini中的内容全部粘过来,之后我们只需要修改几个地方。

下面是修改好的脚本:

testscene.ini

  1. [testscene]  
  2. # the prefix to be added to the generated functions. You might or might not use this in your own  
  3. # templates  
  4. prefix = testscene  
  5.   
  6. # create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)  
  7. # all classes will be embedded in that namespace  
  8. target_namespace = cc  
  9.   
  10. android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include  
  11. android_flags = -D_SIZE_T_DEFINED_   
  12.   
  13. clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include   
  14. clang_flags = -nostdinc -x c++ -std=c++11  
  15.   
  16. cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/2d -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/physics -I%(cocosdir)s/cocos/2d/platform -I%(cocosdir)s/cocos/2d/platform/android -I%(cocosdir)s/cocos/math/kazmath -I%(cocosdir)s/cocos/physics  
  17. cocos_flags = -DANDROID -DCOCOS2D_JAVASCRIPT -DCC_USE_PHYSICS=1  
  18.   
  19. cxxgenerator_headers =   
  20.   
  21. # extra arguments for clang  
  22. extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s   
  23.   
  24. # what headers to parse  
  25. headers = /Users/tokou/WORK/5-Cocos2dx/Project/UI/uitestLua/frameworks/runtime-src/Classes/TestScene.h  
  26.   
  27. # what classes to produce code for. You can use regular expressions here. When testing the regular  
  28. # expression, it will be enclosed in "^$", like this: "^Menu*$".  
  29. classes = TestScene  
  30.   
  31. # what should we skip? in the format ClassName::[function function]  
  32. # ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also  
  33. # regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just  
  34. # add a single "*" as functions. See bellow for several examples. A special class name is "*", which  
  35. # will apply to all class names. This is a convenience wildcard to be able to skip similar named  
  36. # functions from all classes.  
  37.   
  38. skip =   
  39.          
  40.   
  41. rename_functions =   
  42.   
  43. rename_classes =   
  44.   
  45. # for all class names, should we remove something when registering in the target VM?  
  46. remove_prefix =   
  47.   
  48. # classes for which there will be no "parent" lookup  
  49. classes_have_no_parents =   
  50.   
  51. # base classes which will be skipped when their sub-classes found them.  
  52. base_classes_to_skip = Layer  
  53.   
  54. # classes that create no constructor  
  55. # Set is special and we will use a hand-written constructor  
  56. abstract_classes =   
  57.   
  58. # Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.  
  59. script_control_cpp = no  

主要修改的地方:

  1. [testscene]    
  2. prefix = testscene    
  3. target_namespace =    
  4. headers = /Users/tokou/WORK/5-Cocos2dx/Project/UI/uitestLua/frameworks/runtime-src/Classes/TestScene.h  --这里我用的是绝对路径用,如果用之前的变量路径找不到 TestScene.h  
  5. classes = TestScene    
  6. skip =    
  7. abstract_classes =    

之后我们再自己写一个genbindings_testscene.py脚本这个脚本就是在genbindings.py这个脚本的基础上改的。


只要修改命令参数就OK。

将:

  1. cmd_args = {'cocos2dx.ini' : ('cocos2d-x''lua_cocos2dx_auto'), \  
  2.                    'cocos2dx_extension.ini' : ('cocos2dx_extension''lua_cocos2dx_extension_auto'), \  
  3.                    'cocos2dx_ui.ini' : ('cocos2dx_ui''lua_cocos2dx_ui_auto'), \  
  4.                    'cocos2dx_studio.ini' : ('cocos2dx_studio''lua_cocos2dx_studio_auto'), \  
  5.                    'cocos2dx_spine.ini' : ('cocos2dx_spine''lua_cocos2dx_spine_auto'), \  
  6.                    'cocos2dx_physics.ini' : ('cocos2dx_physics''lua_cocos2dx_physics_auto'), \  
  7.                        
  8.                    }  

修改成:

  1. cmd_args = {'testscene.ini': ('testscene','lua_testscene_auto') }  


之后打开命令行工具:

cd 到tolua文件夹

./genbindings_testscene.py


编译就完成了。

我们接下来使用以下我们自己的类。

首先,导入我们编译出来的.hpp和.cpp文件。他们在哪里呢?

见下图:

1、

2、


之后在AppDelegate.cpp

加入头文件:

#include "lua_testscene_auto.hpp"

在 bool AppDelegate::applicationDidFinishLaunching()中加入如下代码。

  1. auto engine = LuaEngine::getInstance();  
  2.     ScriptEngineManager::getInstance()->setScriptEngine(engine);  
  3.     engine->executeScriptFile("src/test2.lua");  
  4.     /***************这里加入如下代码*/  
  5.     register_all_testscene(engine->getLuaStack()->getLuaState());  



.lua

  1. scene = TestScene:createScene()  
  2. c.Director:getInstance():runWithScene(scene)  

运行效果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值