COCOS2DX-LUA 脚本开发之四使用tolua++编译pkg,从而创建自定义类

在Lua第三篇中介绍了,如何在cocos2dx中使用Lua创建自定义类供Lua脚本调用使用,当时出于Himi对Lua研究不够深入,所以当时使用了笨方法手动添加的方式进行的,那么本篇将介绍利用tolua++快速将我们自定义的c2dx类嵌入,供 lua脚本使用。

首先介绍整个过程:

之前我们的过程: 自定义类->手动到LuaCoco2d.cpp中手动添加binding->lua使用

现在我们的过程是: 自定义类->使用tolua++工具编译到LuaCoco2d.cpp中->lua使用

下面进行详细步骤讲解:

步骤一:首先自定义类(这里Himi自定义类名 “MySprite”)

MySprite.h
  1. // // MySprite.h 
  2. // mtet 
  3. // 
  4. // Created by Himi on 13-4-7. 
  5. // // 
  6. #ifndef __mtet__MySprite__ 
  7. #define __mtet__MySprite__ 

  8. #include "cocos2d.h" 
  9. using namespace cocos2d; 
  10. class MySprite : public CCSprite{ public: 
  11. static MySprite* createMS(const char* fileName); 
  12. }; 
  13. #endif /* defined(__mtet__MySprite__) */
复制代码

MySprite.cpp
  1. // 
  2. // MySprite.cpp 
  3. // mtet 
  4. // 
  5. // Created by Himi on 13-4-7. 
  6. // // 
  7. #include "MySprite.h" 
  8. MySprite* MySprite::createMS(const char* fileName){ 
  9. MySprite* sp = new MySprite(); 
  10. if(sp && sp->initWithFile(fileName){ 
  11. sp->setPosition(ccp(100,100)); sp->autorelease(); 
  12. return sp; 

  13. CC_SAFE_DELETE(sp); 
  14. return NULL; 
  15. }
复制代码

步骤二:利用tolua++编译我们创建的pkg,将自定义类嵌入LuaCocos2d.cpp中

首先我们到cocos2dx引擎目录下找到tools下的tolua++文件夹。

然后你看到很多的pkg文件,你可以使用文本打开,就会发现都是Cocos2dx引擎封装的类、函数定义,如下CCSprite.pkg
  1. /* 
  2. typedef enum { 
  3. //! Translate with it's parent CC_HONOR_PARENT_TRANSFORM_TRANSLATE = 1 << 0, 
  4. //! Rotate with it's parent CC_HONOR_PARENT_TRANSFORM_ROTATE = 1 << 1, 
  5. //! Scale with it's parent CC_HONOR_PARENT_TRANSFORM_SCALE = 1 << 2, 
  6. //! Skew with it's parent CC_HONOR_PARENT_TRANSFORM_SKEW = 1 << 3, 
  7. //! All possible transformation enabled. Default value. CC_HONOR_PARENT_TRANSFORM_ALL = CC_HONOR_PARENT_TRANSFORM_TRANSLATE | CC_HONOR_PARENT_TRANSFORM_ROTATE | CC_HONOR_PARENT_TRANSFORM_SCALE | CC_HONOR_PARENT_TRANSFORM_SKEW, 
  8. } ccHonorParentTransform; 
  9. */
  10. class CCSprite : public CCNode { 
  11. void setDirty(bool bDirty); 
  12. bool isDirty(void); ccV3F_C4B_T2F_Quad getQuad(void); 
  13. CCRect getTextureRect(void); 
  14. //bool isUsesBatchNode(void); 
  15. bool isTextureRectRotated(void); void setAtlasIndex(unsigned int uAtlasIndex); 
  16. unsigned int getAtlasIndex(void); 
  17. //void setUsesSpriteBatchNode(bool bUsesSpriteBatchNode); 
  18. void setTextureAtlas(CCTextureAtlas *pobTextureAtlas); 
  19. CCTextureAtlas* getTextureAtlas(void); 
  20. //void setSpriteBatchNode(CCSpriteBatchNode *pobSpriteBatchNode); 
  21. //CCSpriteBatchNode* getSpriteBatchNode(void); 
  22. //void setHonorParentTransform(ccHonorParentTransform eHonorParentTransform); 
  23. //ccHonorParentTransform getHonorParentTransform(void); 
  24. void setBlendFunc(ccBlendFunc blendFunc); 
  25. ccBlendFunc getBlendFunc(void); CCPoint getOffsetPosition(void); void ignoreAnchorPointForPosition(bool newValue); 
  26. void setFlipX(bool bFlipX); 
  27. void setFlipY(bool bFlipY); 
  28. bool isFlipX(void); 
  29. bool isFlipY(void); 
  30. void removeChild(CCNode* pChild, bool bCleanUp); 
  31. void removeAllChildrenWithCleanup(bool bCleanup); 
  32. void reorderChild(CCNode* pChild, int zOrder); 
  33. void addChild(CCNode* pChild); 
  34. void addChild(CCNode* pChild, int zOrder); 
  35. void addChild(CCNode* pChild, int zOrder, int tag); 
  36. void sortAllChildren(); 
  37. //void setPosition(CCPoint pos); 
  38. void setRotation(float rotation); 
  39. void setSkewX(float sx); 
  40. void setSkewY(float sy); 
  41. void setScale(float fScale); 
  42. void setScaleX(float fScaleX); 
  43. void setScaleY(float fScaleY); 
  44. void setVertexZ(float fVertexZ); 
  45. void setAnchorPoint(const CCPoint & anchor); 
  46. void setVisible(bool bVisible); 
  47. void setOpacity(GLubyte opacity); GLubyte getOpacity(void); 
  48. void setColor(ccColor3B color3); ccColor3B getColor(void); 
  49. void setOpacityModifyRGB(bool bValue); 
  50. bool isOpacityModifyRGB(void); 
  51. void setTexture(CCTexture2D *texture); 
  52. CCTexture2D* getTexture(void); void updateTransform(void); 
  53. //void useSelfRender(void); 
  54. void setTextureRect(CCRect rect); void setTextureRect(CCRect rect, bool rotated, CCSize size); 
  55. void setVertexRect(CCRect rect); 
  56. //void useBatchNode(CCSpriteBatchNode *batchNode); void setDisplayFrame(CCSpriteFrame *pNewFrame); 
  57. bool isFrameDisplayed(CCSpriteFrame *pFrame); 
  58. CCSpriteFrame* displayFrame(void); void setBatchNode(CCSpriteBatchNode* pBatchNode); 
  59. CCSpriteBatchNode* getBatchNode(); 
  60. void setDisplayFrameWithAnimationName(const char *animationName, int frameIndex); 
  61. static CCSprite* createWithTexture(CCTexture2D *pTexture); 
  62. static CCSprite* createWithTexture(CCTexture2D *pTexture, CCRect rect); 
  63. static CCSprite* createWithSpriteFrame(CCSpriteFrame *pSpriteFrame); static CCSprite* createWithSpriteFrameName(const char *pszSpriteFrameName); 
  64. static CCSprite* create(const char *pszFileName, CCRect rect); 
  65. static CCSprite* create(const char *pszFileName); 
  66. static CCSprite* create(); 
  67. };
复制代码
没错,我们也会按照类似方式进行创建我们自定义类的pkg文件。

我们自定义一个文件(文本、xcode等都可以),后缀 .pkg ,然后将Himi自定义的MySprite类定义到pkg中,如下:

注意:只要自定义类.h中的内容,至于cpp的实现,binding后lua自动调用你类的函数

MySprite.pkg
  1. class MySprite : public CCSprite{ 
  2. static MySprite* createMS(const char* fileName);
  3. };
复制代码

在pkg中我只是定义了创建函数而已,至于更多的函数就交给大家自定义啦,另外我们注意书写pkg时是需要几条规则的,其实在tolua++这个文件夹中我们也能看到有一个名字叫 README 的文件,打开如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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


这个文件声明了书写pkg的规则,不多赘述。

书写好我们的pkg之后,将pkg文件放置此tolua++文件夹下即可,然后配置我们tolua++工具。

继续在tolua++文件夹中解压tolua++.Mac.zip 文件,会得到一个tolua++的工具,如下图:



解压出工具之后,我们还要在tolua++文件夹中,配置tolua++路径,打开“build.sh”文件,如下:



这里 TOLUA 是tolua++工具的位置(路径后面要架上 /tolua++  表示这个工具),最下面配置的是编译后的luaCocos2d.cpp文件导出的位置,Himi这里配置到桌面,配置如下:


最后,我们要将我们定义的pkg文件注册到 tolua++文件夹下的Cocos2d.pkg中,如下:


如上步骤都OK后,我们就可以使用“终端”,先cd到tolua++的文件夹下,然后使用“make”命令执行tolua++工具。

(如果这里终端不能正常执行, 请继续修改tolua++文件夹下的: makefile  ,将其路径配置一下即可。)

终端正常执行后,会在一开始指定的目录生成LuaCocos2d.cpp 文件,且其中已经binding好了自定义类,将生成的LuaCocos2d.cpp替换到你项目的/libs/lua/cocos2dx_support下的LuaCocos2d.cpp 文件。

Himi建议生成的LuaCocos2d.cpp 文件路径直接设置你的项目的/libs/lua/cocos2dx_support下很方便

注意:这时候LuaCoco2d.cpp中虽然已经binding了我们的自定义类,但是没有引用我们的头文件,所以我们还需要在LuaCocos2d.h中倒入我们自定义类.h 。

步骤三:Lua测试我们的自定义类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- for CCLuaEngine traceback 
function __G__TRACKBACK__(msg)
    print("----------------------------------------")
    print("LUA ERROR: " .. tostring(msg) .. "\n")
    print(debug.traceback())
    print("----------------------------------------")
end
  
local function main()
    -- avoid memory leak 
    collectgarbage("setpause", 100)
    collectgarbage("setstepmul", 5000)
  
    local cclog = function(...)
        print(string.format(...))
    end
  
    require "hello2"
    cclog("result is " .. myadd(3, 5))
  
    --------------- 
  
    -- create farm 
    local function createLayerFarm()
        local layerFarm = CCLayer:create()
  
        local font = CCLabelTTF:create("Himi 使用tolua++ binding自定义类", "Verdana-BoldItalic", 20)
        font:setPosition(ccp(220,260))
        layerFarm:addChild(font)
  
        local ms  = MySprite:createMS("Icon.png")
        layerFarm:addChild(ms)
  
        return layerFarm 
    end
  
    -- run 
    local sceneGame = CCScene:create()
    sceneGame:addChild(createLayerFarm())
    CCDirector:sharedDirector():runWithScene(sceneGame)
end
  
xpcall(main, __G__TRACKBACK__)


运行截图如下:




原文链接:http://www.himigame.com/lua1/1259.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
蛋白质是生物体中普遍存在的一类重要生物大分子,由天然氨基酸通过肽键连接而成。它具有复杂的分子结构和特定的生物功能,是表达生物遗传性状的一类主要物质。 蛋白质的结构可分为四级:一级结构是组成蛋白质多肽链的线性氨基酸序列;二级结构是依靠不同氨基酸之间的C=O和N-H基团间的氢键形成的稳定结构,主要为α螺旋和β折叠;三级结构是通过多个二级结构元素在三维空间的排列所形成的一个蛋白质分子的三维结构;四级结构用于描述由不同多肽链(亚基)间相互作用形成具有功能的蛋白质复合物分子。 蛋白质在生物体内具有多种功能,包括提供能量、维持电解质平衡、信息交流、构成人的身体以及免疫等。例如,蛋白质分解可以为人体提供能量,每克蛋白质能产生4千卡的热能;血液里的蛋白质能帮助维持体内的酸碱平衡和血液的渗透压;蛋白质是组成人体器官组织的重要物质,可以修复受损的器官功能,以及维持细胞的生长和更新;蛋白质也是构成多种生理活性的物质,如免疫球蛋白,具有维持机体正常免疫功能的作用。 蛋白质的合成是指生物按照从脱氧核糖核酸(DNA)转录得到的信使核糖核酸(mRNA)上的遗传信息合成蛋白质的过程。这个过程包括氨基酸的活化、多肽链合成的起始、肽链的延长、肽链的终止和释放以及蛋白质合成后的加工修饰等步骤。 蛋白质降解是指食物中的蛋白质经过蛋白质降解酶的作用降解为多肽和氨基酸然后被人体吸收的过程。这个过程在细胞的生理活动中发挥着极其重要的作用,例如将蛋白质降解后成为小分子的氨基酸,并被循环利用;处理错误折叠的蛋白质以及多余组分,使之降解,以防机体产生错误应答。 总的来说,蛋白质是生物体内不可或缺的一类重要物质,对于维持生物体的正常生理功能具有至关重要的作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值