使用tolua++编译pkg,从而创建自定义类让Lua脚本使用

本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明Himi )
转载自【黑米GameDev街区】
原文链接: http://www.himigame.com/lua-game/1259.html


          ☞ 点击订阅 ☜
本博客最新动态!及时将最新博文通知您!

                 

分享
 

此篇基本【COCOS2DX(2.X)_LUA开发之三】在LUA中使用自定义精灵(LUA脚本与自创建类之间的访问)及LUA基础讲解

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

首先介绍整个过程:

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

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

下面进行详细步骤讲解:

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

MySprite.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
//  MySprite.h
//  mtet
//
//  Created by Himi on 13-4-7.
//
//
 
#ifndef __mtet__MySprite__
#define __mtet__MySprite__
 
#include "cocos2d.h"
using namespace cocos2d;
 
class MySprite : public CCSprite{
public:
    static MySprite* createMS(const char* fileName);
};
#endif /* defined(__mtet__MySprite__) */

MySprite.cpp

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

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

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

然后你看到很多的pkg文件,你可以使用文本打开,就会发现都是Cocos2dx引擎封装的类、函数定义,如下CCSprite.pkg

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
typedef enum {
    //! Translate with it's parent
    CC_HONOR_PARENT_TRANSFORM_TRANSLATE =  1 << 0,
    //! Rotate with it's parent
    CC_HONOR_PARENT_TRANSFORM_ROTATE    =  1 << 1,
    //! Scale with it's parent
    CC_HONOR_PARENT_TRANSFORM_SCALE     =  1 << 2,
    //! Skew with it's parent
    CC_HONOR_PARENT_TRANSFORM_SKEW      =  1 << 3,
 
    //! 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,
 
} ccHonorParentTransform;
*/
class CCSprite : public CCNode
{
    void setDirty(bool bDirty);
    bool isDirty(void);
 
    ccV3F_C4B_T2F_Quad getQuad(void);
 
    CCRect getTextureRect(void);
    //bool isUsesBatchNode(void);
    bool isTextureRectRotated(void);
 
    void setAtlasIndex(unsigned int uAtlasIndex);
    unsigned int getAtlasIndex(void);
    //void setUsesSpriteBatchNode(bool bUsesSpriteBatchNode);
    void setTextureAtlas(CCTextureAtlas *pobTextureAtlas);
    CCTextureAtlas* getTextureAtlas(void);
    //void setSpriteBatchNode(CCSpriteBatchNode *pobSpriteBatchNode);
    //CCSpriteBatchNode* getSpriteBatchNode(void);
    //void setHonorParentTransform(ccHonorParentTransform eHonorParentTransform);
    //ccHonorParentTransform getHonorParentTransform(void);
    void setBlendFunc(ccBlendFunc blendFunc);
    ccBlendFunc getBlendFunc(void);
 
    CCPoint getOffsetPosition(void);
 
    void ignoreAnchorPointForPosition(bool newValue);
    void setFlipX(bool bFlipX);
    void setFlipY(bool bFlipY);
    bool isFlipX(void);
    bool isFlipY(void);
 
    void removeChild(CCNode* pChild, bool bCleanUp);
    void removeAllChildrenWithCleanup(bool bCleanup);
    void reorderChild(CCNode* pChild, int zOrder);
    void addChild(CCNode* pChild);
    void addChild(CCNode* pChild, int zOrder);
    void addChild(CCNode* pChild, int zOrder, int tag);
    void sortAllChildren();
    //void setPosition(CCPoint pos);
    void setRotation(float rotation);
    void setSkewX(float sx);
    void setSkewY(float sy);
    void setScale(float fScale);
    void setScaleX(float fScaleX);
    void setScaleY(float fScaleY);
    void setVertexZ(float fVertexZ);
    void setAnchorPoint(const CCPoint & anchor);
    void setVisible(bool bVisible);
 
    void setOpacity(GLubyte opacity);
    GLubyte getOpacity(void);
 
    void setColor(ccColor3B color3);
    ccColor3B getColor(void);
    void setOpacityModifyRGB(bool bValue);
    bool isOpacityModifyRGB(void);
 
    void setTexture(CCTexture2D *texture);
    CCTexture2D* getTexture(void);
 
    void updateTransform(void);
    //void useSelfRender(void);
    void setTextureRect(CCRect rect);
    void setTextureRect(CCRect rect, bool rotated, CCSize size);
    void setVertexRect(CCRect rect);
    //void useBatchNode(CCSpriteBatchNode *batchNode);
    void setDisplayFrame(CCSpriteFrame *pNewFrame);
    bool isFrameDisplayed(CCSpriteFrame *pFrame);
    CCSpriteFrame* displayFrame(void);
    void setBatchNode(CCSpriteBatchNode* pBatchNode);
    CCSpriteBatchNode* getBatchNode();
    void setDisplayFrameWithAnimationName(const char *animationName, int frameIndex);
 
    static CCSprite* createWithTexture(CCTexture2D *pTexture);
    static CCSprite* createWithTexture(CCTexture2D *pTexture, CCRect rect);
    static CCSprite* createWithSpriteFrame(CCSpriteFrame *pSpriteFrame);
    static CCSprite* createWithSpriteFrameName(const char *pszSpriteFrameName);
    static CCSprite* create(const char *pszFileName, CCRect rect);
    static CCSprite* create(const char *pszFileName);
    static CCSprite* create();
};

没错,我们也会按照类似方式进行创建我们自定义类的pkg文件。

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

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

MySprite.pkg

1
2
3
class MySprite : public CCSprite{
    static MySprite* createMS(const char* fileName);
};

在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__)

运行截图如下:

OK,今天就到这,希望大家多多探讨,有问题请及时留言。

 

【COCOS2DX-LUA 脚本开发之四】的更多相关文章

  1. cocos2d-x lua脚本开发 1

    自从开始关注OpenResty之后,逐渐关注Lua语言,发现这个语言真真是容易让人喜爱的语言.偶然间发现了cocos2d-x,还支持lua,所以果断尝试一下. 这里是在cocos2d-x官方网站下载了 ...

  2. Cocos2d-x教程(26)-Cocos2d-x + Lua脚本实现大地图缩放功能

    欢迎增加 Cocos2d-x 交流群: 193411763 视频教程地址:http://www.tudou.com/programs/view/qRiOfppMghM/ 转载请注明原文出处:http: ...

  3. Cocos2d-x Lua游戏开发Mac环境搭建以及一点点感悟

    接触Cocos2d-x 最近由于公司项目的需要,自己开始接触Cocos,开始做一些简单的轻量级的游戏,以前没有接触过这一块的东西,也是借助这个机会学习一下游戏的开发,由于以前自己接触的全都是iOS和A ...

  4. 【COCOS2DX-LUA 脚本开发之四】使用tolua++编译pk创建自定义类

    此篇基本[COCOS2DX(2.X)_LUA开发之三]在LUA中使用自定义精灵(LUA脚本与自创建类之间的访问)及LUA基础讲解 在Lua第三篇中介绍了,如何在cocos2dx中使用Lua创建自定义类 ...

  5. 第二十三篇:在SOUI中使用LUA脚本开发界面

    像写网页一样做客户端界面可能是很多客户端开发的理想. 做好一个可以实现和用户交互的动态网页应该包含两个部分:使用html做网页的布局,使用脚本如vbscript,javascript做用户交互的逻辑. ...

  6. Redis进阶之使用Lua脚本开发

    1.在Redis中使用Lua 在Redis中执行Lua脚本有两种方法:eval和evalsha. (1)eval eval 脚本内容 key个数 key列表 参数列表 下面例子使用了key列表和参数列 ...

  7. Cocos2d-x lua游戏开发之安装Lua到mac系统

    注意:mac ox .lua version :5.15 下载lua官网的lua, 注意:最好是5.15下面.5.2的lua不支持table的getn()方法,这让我情何以堪.(获取table长度.相 ...

  8. 【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!

    [COCOS2DX-LUA 脚本开发之一]在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途! 分类: [Cocos2dx Lua 脚本开发 ] 2012-04-1 ...

  9. SpringBoot通过RedisTemplate执行Lua脚本

    如果你对Redis和Lua的关系不太清楚,请先阅读:Redis进阶之使用Lua脚本开发 1.RedisScript 首先你得引入spring-boot-starter-data-redis依赖,其次把 ...

随机推荐

  1. (32)C#文件读写

    一.File 类 这是一个静态类,提供用于创建.复制.删除.移动和打开单一文件的静态方法,并协助创建 FileStream 对象 using System.IO; 没有构造函数和属性  写入数据 1. ...

  2. Python的网络编程[6] -> Modbus 协议 -> Modbus 的基本理论与 Python 实现

    Modbus协议 / Modbus Protocol 目录 Modbus 协议简介 Modbus RTU协议 Modbus TCP协议与 Python 实现 Modbus 功能码 Modbus TCP ...

  3. hdu6038

    hdu6038 分析 求函数 \(f\) 的构成方案,\(f\) 确定下来后,\(f\) 和 \(b\) 的值也是一一对应的了( \(f(i)=b_{f(a_i)}\) ),观察 \(a\) 数组,代 ...

  4. App保持登录状态的常用方法(转)

    我们在使用App时,一次登录后App如果不主动退出登录或者清除数据,App会在很长一段时间内保持登录状态,或者让用户感觉到登录一次就不用每次都输入用户密码才能进行登录.银行.金融涉及到支付类的App一 ...

  5. 设计模式之不变模式(Immutable Pattern)分析

    http://www.iteye.com/topic/959751 最近老有人问我不变模式,我其实也理解得不深,于是花了一些时间进行学习总结,分析了一下不变模式(immutable pattern), ...

  6. 20170416郑州市轻工业学院ACM校赛

    这是个星期天,但是,这种非一线城市,重点城市什么的高中,放假从来不按套路出牌,几乎可以说能给你一天是福气.当然,比县里好的多,问在县里上高中的初中同学,放假更是比我们一天里的午休+晚上吃饭时间还要少. ...

  7. [BZOJ 1806] Miners 矿工配餐

    Link: BZOJ 1806 传送门 Solution: 为了使状态包含每个节点前所有必须的信息: 设$dp[i][a1][a2][b1][b2]$为配送到第$i$个,一厂前两个为$a1,a2$,二 ...

  8. Bluetooth篇 开发实例之七 匹配&UUID

    匹配和通信是两回事. 1.用过Android系统设置(Setting)的人都知道蓝牙搜索之后可以建立配对和解除配对,但是这两项功能的函数没有在SDK中给出.但是可以通过反射来获取. 知道这两个API的 ...

  9. 基于Prometheus,Alermanager实现Kubernetes自动伸缩

    到目前为止Kubernetes对基于cpu使用率的水平pod自动伸缩支持比较良好,但根据自定义metrics的HPA支持并不完善,并且使用起来也不方便. 下面介绍一个基于Prometheus和Aler ...

  10. SyBase 百科

    ylbtech_database_sybase 1, 百度百科 http://baike.baidu.com/view/118488.htm?fr=aladdin