Quick-Cocos2d-x 3.3下集成云风pbcWindows版

主要分为下面几个步骤:

1,开始前的准备工作

首先从github上下载Google的protobuf编译出protoc.exe 地址

然后也是从github上下载云风大侠的pbc

2,将pbc中的相关代码加入到vs(我用的是vs2013)工程项目中

在frameworks\runtime-src\Classes目录下加入protobuf文件文件夹中加入pbc相关代码,它们的结构是这样的

1
2
3
4
protobuf\src
protobuf\pbc.h
protobuf\pbc-lua.c
protobuf\pbc-lua.h

其中src文件夹即为pbc下的src,pbc.h为pbc下的pbc.h,pbc-lua.c为pbc\binding\lua目录下的puc-lua.c

pbc-lua.h是我们自己创建的头文件,它的内容是:

4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef__LUA_PBC_EXTRA_H_
#define__LUA_PBC_EXTRA_H_
#if__cplusplus
extern "C" {
#endif
#include"lauxlib.h"
int luaopen_protobuf_c(lua_State*L);
#if__cplusplus
}
#endif
#endif

然后在vs中新建筛选器protobuf和src将相关代码添加进去,通过操作项目工程属性将protobuf文件夹目录包含进去。

在AppDelegate.cpp头部加入#include "protobuf/pbc-lua.h" 然后在bool AppDelegate::applicationDidFinishLaunching()

方法中LuaStack* stack = engine->getLuaStack();这一句的上一行加入luaopen_protobuf_c(L);函数调用

重新编译工程。

3,在脚本中加入protobuf.lua(为了项目看上去结构比较好看,在app目录下加入了protobuf目录)

在src\app目录下加入protobuf文件夹,在文件夹中加入pbc中binding\lua\protobuf.lua文件

4,编写protobuf文件并用protoc.exe生成pb文件

我的例子protobuf源文件为:person.proto

6
packagetest.protocol.sgp;
messagePerson{
requiredint32id=1;
requiredstringname=2;
optionalstringemail=3;
}

在网上找了个批处理文件,pb.bat然后根据需求将其改了下

15
@echooff
setDIR=%~dp0
cd/d "%DIR%"
setlocalenabledelayedexpansion
for /r%%iin(*.proto) do (
setpbname=%%i
setpbname=!pbname:~0,-5!b
protoc-I%DIR%--descriptor_set_out!pbname!%%i
)
if exist "./pb" rmdir/s/q "./pb"
mkdir "./pb"
move*.pb./pb
echo "finished"

将protoc.exe,pb.bat,person.proto 放入同一目录下并执行pb.bat

执行完成后再当前目录下会生成一个pb文件夹,文件夹中即为我们所需要的person.pb文件

5,编写测试代码

生成的pb文件夹放入项目资源目录res中

我的MyApp.lua

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require( "config" )
"cocos.init" )
"framework.init" )
"app.protobuf.protobuf" )
MyGame={}
MyGame.protobuf=protobuf
localMyApp= class ( "MyApp" ,cc.mvc.AppBase)
functionMyApp:ctor()
MyApp.super.ctor(self)
end
functionMyApp.registerallProtobuf()
localpbAllName={
"pb/person.pb" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
}
i=1,#pbAllName do
localfileData=cc.HelperFunc:getFileData(pbAllName[i])
MyGame.protobuf. register (fileData)
end
end
functionMyApp:run()
cc.FileUtils:getInstance():addSearchPath( "res/" )
MyApp.registerallProtobuf()
self:enterScene( "MainScene" )
end
return MyApp

我的MainScene.lua

14
localMainScene= ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,function()
display.newScene( end)
functionMainScene:ctor()
localdata={}
data.id=12
data.name= "xiaoming"
localstrData=MyGame.protobuf.encode( "test.protocol.sgp.Person" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,data)
localperson=MyGame.protobuf.decode( ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,strData)
dump(person)
end
MainScene

最后运行项目如果能够dump出person就表示集成成功啦!

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...