cocos2dx使用lua和protobuf

 

为了使游戏开发更加方便快捷,我继续了protobuf在lua下的尝试。javascript

 

socket使用的是cocos2dx集成的websocket。java

 

先说下环境:cocos2d-x-2.2.1 + protobuf 2.5.0 + protoc-gen-lua + Python 2.7.5python

 

 

 

1.在protobuf目录下依次执行以下命令web

 

 

 

[plain]  view plain copy 在CODE上查看代码片 派生到个人代码片
  1. python setup.py build  
  2. python setup.py install  


2.在protoc-gen-lua目录下的plugin目录中新建protoc-gen-lua.bat文件,并将以下内容粘贴到里面
服务器

 

 

 

 

[plain]  view plain copy 在CODE上查看代码片 派生到个人代码片
  1. @python <你的目录>\protoc-gen-lua\plugin\protoc-gen-lua  


3.生成protobuf对应的lua文件,执行以下命令:
websocket

 

 

 

 

[plain]  view plain copy 在CODE上查看代码片 派生到个人代码片
  1. <你的路径>/protoc.exe --lua_out=./ --plugin=protoc-gen-lua="<你的路径>\protoc-gen-lua\plugin\protoc-gen-lua.bat" test.proto  

执行完后就会生成test_pb.lua文件。app

 

 

4.使用cocos2dx的create_project.py建立lua工程;socket

 

5.将protoc-gen-lua/protobuf目录下的pb.c文件复制到lua工程的Classes目录下,并加入到C++工程中;函数

 

6.将protoc-gen-lua/protobuf目录下的全部lua文件复制到lua工程的Resources目录下;oop

 

7.编辑AppDelegate.cpp文件,添加以下代码:

 

 

 

[cpp]  view plain copy 在CODE上查看代码片 派生到个人代码片
  1. extern "C"{    
  2. #include <lua.h>    
  3. #include <lualib.h>    
  4. #include <lauxlib.h>    
  5.     int luaopen_pb (lua_State *L);    
  6. }  


8.在AppDelegate::applicationDidFinishLaunching()方法中加入初始化方法:

 

 

 

 

[cpp]  view plain copy 在CODE上查看代码片 派生到个人代码片
  1. luaopen_pb(tolua_s);  


9.此时对lua工程进行编译,若是出错,请检查并修正;编译经过,而且能够正常运行后继续下面的步骤;

 

 

10.cocos2dx默认产生的lua工程包含2个文件hello.lua与hello2.lua,打开hello2.lua,将以下内容添加到文件末尾(由于我使用的是websocket,各位可根据本身的实际状况进行修改):

 

 

 

[javascript]  view plain copy 在CODE上查看代码片 派生到个人代码片
  1. local wsProtobuf=nil  
  2. function testProtobuf()  
  3.     wsProtobuf = WebSocket:create("ws://localhost:8080/web")  
  4.     local function onOpen(strData)  
  5.         print("socket open ...")  
  6.         require "test_pb"  
  7.         local msg=test_pb.Message()  
  8.         msg.id=101        
  9.         local person =test_pb.Person()  
  10.         person.id=111  
  11.         person.name="user1"  
  12.         person.email="a1@a.a"  
  13.         msg.data=person:SerializeToString()  
  14.         local pb_data = msg:SerializeToString()  
  15.         local t={string.byte(pb_data,1,-1)}  
  16.         wsProtobuf:sendBinaryMsg(t,table.getn(t))  
  17.     end  
  18.     local function onMessage(strData)  
  19.         print("socket message ...")  
  20.     end  
  21.     local function onClose(strData)  
  22.         print("socket close ...")  
  23.     end  
  24.     local function onError(strData)  
  25.         print("socket error")  
  26.     end  
  27.       
  28.     if nil ~= wsProtobuf then  
  29.         wsProtobuf:registerScriptHandler(onOpen,kWebSocketScriptHandlerOpen)  
  30.         wsProtobuf:registerScriptHandler(onMessage,kWebSocketScriptHandlerMessage)  
  31.         wsProtobuf:registerScriptHandler(onClose,kWebSocketScriptHandlerClose)  
  32.         wsProtobuf:registerScriptHandler(onError,kWebSocketScriptHandlerError)  
  33.     end   
  34. end  


11.而后在hello.lua中调用testProtobuf()函数便可。

 

 

测试运行,你能够在服务器端查看收到的消息。

相关文章
相关标签/搜索