cocos2d_x+lua【9】 ------tolua++使用(将2d_x自定义类导入lua)

来源:互联网 发布:直销管理平台系统源码 编辑:程序博客网 时间:2024/05/02 05:03

oDustGgg个人原创、欢迎转载、转载请注明出处、

http://blog.csdn.net/odustggg/article/details/8195137

一、首先在引擎的跟目录下找到cocos2d-x自带的工具tolua++


二、使用tolua++生成自定义类的声明

打开tool文件夹中的readme文件如下:
[cpp] view plaincopy
  1. 1. Generating the lua<-->C bindings with tolua++  
  2. tolua++.exe -tCocos2d -o LuaCocos2d.cpp Cocos2d.pkg  
我们为了方便可以制作一个批处理文件、放在tolua.exe同目录下、每次点击就可产生自定义类的声明、如下
[cpp] view plaincopy
  1. -----------------------------------------createCpp.bat---------------------------  
  2. @echo on  
  3. E:\gkh_netdisk\tools\tolua++\tolua++.exe -o testCtoLua.cpp testCtoLua.pkg  
  4. @pause  
  5. ---------------------------------------------------------------------------------  
然后开始编写testCtoLua.pkg 要遵循以下的规则
[cpp] view plaincopy
  1. 2.Writing .pkg files  编写pkg文件  
  2. 1)enum keeps the same 保持枚举类型不变  
  3. 2)remove CC_DLL for the class defines, pay attention to multi inherites 删除CC_DLL的类定义、改为多继承  
  4. 3)remove inline keyword for declaration and implementation 删掉声明的inline关键词  
  5. 4)remove public protect and private 删除访问限定  
  6. 5)remove the decalration of class member variable 删除类的成员变量  
  7. 6)keep static keyword  保留statiic关键词  
  8. 7)remove memeber functions that declared as private or protected 成员函数声明为私人或受保护的都删掉  
 我这里就不去写了、就直接用现成的CCDirector类做个测试
[cpp] view plaincopy
  1. ----------- CCDirector.pkg-----------      
  2. typedef enum {     
  3.     /// Device oriented vertically, home button on the bottom  
  4.     kCCDeviceOrientationPortrait = 0, // UIDeviceOrientationPortrait,  
  5.     /// Device oriented vertically, home button on the top  
  6.     kCCDeviceOrientationPortraitUpsideDown = 1, // UIDeviceOrientationPortraitUpsideDown,  
  7.     /// Device oriented horizontally, home button on the right  
  8.     kCCDeviceOrientationLandscapeLeft = 2, // UIDeviceOrientationLandscapeLeft,  
  9.     /// Device oriented horizontally, home button on the left  
  10.     kCCDeviceOrientationLandscapeRight = 3, // UIDeviceOrientationLandscapeRight,  
  11. } ccDeviceOrientation;  
  12.   
  13. class CCDirector : public CCObject  
  14. {  
  15.     CCScene* getRunningScene(void);  
  16.     double getAnimationInterval(void);  
  17.     bool isDisplayFPS(void);  
  18.     void setDisplayFPS(bool bDisplayFPS);  
  19.     bool isRetinaDisplay();  
  20.     bool isPaused(void);  
  21.     unsigned int getFrames(void);  
  22.     CCSize getWinSize(void);  
  23.     CCSize getWinSizeInPixels(void);  
  24.     CCSize getDisplaySizeInPixels(void);  
  25.     CCPoint convertToGL(CCPoint obPoint);  
  26.     CCPoint convertToUI(CCPoint obPoint);  
  27.     void runWithScene(CCScene *pScene);  
  28.     void pushScene(CCScene *pScene);  
  29.     void popScene(void);  
  30.     void replaceScene(CCScene *pScene);  
  31.     CGFloat getContentScaleFactor(void);  
  32.     int getDeviceOrientation();  
  33.     static CCDirector* sharedDirector(void);  
  34. };  

然后将编写好的*.pkg文件复制到tolua++文件目录下、双击前文的批处理文件、不出意外就能生成相对应的Cpp文件了
 
CCDirector.cpp中的关键代码是这些:

[cpp] view plaincopy
  1. tolua_beginmodule(tolua_S,"CCDirector");  
  2. tolua_function(tolua_S,"getRunningScene",tolua_CCDirector_CCDirector_getRunningScene00);  
  3. tolua_function(tolua_S,"getAnimationInterval",tolua_CCDirector_CCDirector_getAnimationInterval00);  
  4. tolua_function(tolua_S,"isDisplayFPS",tolua_CCDirector_CCDirector_isDisplayFPS00);  
  5. tolua_function(tolua_S,"setDisplayFPS",tolua_CCDirector_CCDirector_setDisplayFPS00);  
  6. tolua_function(tolua_S,"isRetinaDisplay",tolua_CCDirector_CCDirector_isRetinaDisplay00);  
  7. tolua_function(tolua_S,"isPaused",tolua_CCDirector_CCDirector_isPaused00);  
  8. tolua_function(tolua_S,"getFrames",tolua_CCDirector_CCDirector_getFrames00);  
  9. tolua_function(tolua_S,"getWinSize",tolua_CCDirector_CCDirector_getWinSize00);  
  10. tolua_function(tolua_S,"getWinSizeInPixels",tolua_CCDirector_CCDirector_getWinSizeInPixels00);  
  11. tolua_function(tolua_S,"getDisplaySizeInPixels",tolua_CCDirector_CCDirector_getDisplaySizeInPixels00);  
  12. tolua_function(tolua_S,"convertToGL",tolua_CCDirector_CCDirector_convertToGL00);  
  13. tolua_function(tolua_S,"convertToUI",tolua_CCDirector_CCDirector_convertToUI00);  
  14. tolua_function(tolua_S,"runWithScene",tolua_CCDirector_CCDirector_runWithScene00);  
  15. tolua_function(tolua_S,"pushScene",tolua_CCDirector_CCDirector_pushScene00);  
  16. tolua_function(tolua_S,"popScene",tolua_CCDirector_CCDirector_popScene00);  
  17. tolua_function(tolua_S,"replaceScene",tolua_CCDirector_CCDirector_replaceScene00);  
  18. tolua_function(tolua_S,"getContentScaleFactor",tolua_CCDirector_CCDirector_getContentScaleFactor00);  
  19. tolua_function(tolua_S,"getDeviceOrientation",tolua_CCDirector_CCDirector_getDeviceOrientation00);  
  20. tolua_function(tolua_S,"sharedDirector",tolua_CCDirector_CCDirector_sharedDirector00);  
只要把cpp中的对应的代码复制到引擎目录下cocos2d_support文件夹中Luacocos2d.cpp中就搞定了 然后重新编译项目、就可以在lua中使用这个接口了
原创粉丝点击