为cocos2d-x3.2版本增加protobuffer2.5.0支持

来源:互联网 发布:淘宝国际物流订单运费 编辑:程序博客网 时间:2024/06/07 17:19

目录[-]

  • 为cocos2d-x3.2版本增加protobuffer2.5.0支持(win32&android)
  • Setp1:新建cocos2d-x 3.2项目
  • Setp2:复制protobuffer2.5源码到cocos2d-x文件夹
  • Setp3:打开ProtobufferDemoWithLib项目,
  • 添加libprotobuffer依赖
  • Setp4:Win32平台直接编译通过
  • Setp4:用protobuf-win32提供的protoc.exe生成测试.proto的.pb.h .pb.cc(建议将protoc.exe复制到c盘的system32中,方便一点)
  • Setp5:编写proto文件并生成pb.h和pb.cc
  • Setp5:修改pb.cc和pb.h后缀(主要是android平台的AL_CPP_EXTENSION选项不支持具有两个小数点的后缀,形如”.pb.cc”),同时修改头文件包含中的后缀
  • Setp5:在HelloWorldScene.cpp中编写测试代码
  • 下面进行android平台protobuffer添加
  • Setp1:先直接编译未添加protobuffer支持的android
  • Setp2:解决问题,此类问题 很多 百度之
  • Setp3:为protobuffer编写Android.mk
  • Setp4:打开项目的android.mk 添加 protobuffer相关
  • Setp5:cygwin中进入protobuffer 运行./configure && make && make install产生config.h
  • 再次编译成功
  • Setp6: 打开win32平台的测试代码,android.mk中添加编译部分
  • cocos2d-x3.2版本增加protobuffer2.5.0支持(win32&android)


    因为不想排版 所以这里只有文字 可以下载word版观看:点击下载

    作用:添加protobuffer支持后,可以利用protobuffer特性来序列化和反序列化二进制数据流。

     

    软件准备:cocos2d-x3.2, protobuffer2.5.0protoc-2.5.0-win32, vs2012, anroid相关环境(SDK, NDK , CYGWIN)

     

    Setp1:新建cocos2d-x 3.2项目

    安装完成cocos2d-x3.2 配置好环境变量,利用命令新增项目

    命令 “cocos new ProtobufferDemoWithLib -p com.jcracker.ProtobufferDemoWithLib -l cpp -d/

    创建成功界面:

     

    Setp2:复制protobuffer2.5源码到cocos2d-x文件夹

    模仿external模块。将protobuffer文件复制到external中。

     

    Setp3:打开ProtobufferDemoWithLib项目,

    添加libprotobuffer依赖

     

     

     

     

    Setp4Win32平台直接编译通过

     

    Setp4:用protobuf-win32提供的protoc.exe生成测试.proto.pb.h .pb.cc(建议将protoc.exe复制到c盘的system32中,方便一点)

     

    Setp5:编写proto文件并生成pb.hpb.cc

     

    生成pb.hpb.cc

    命令“protoc TestName.proto --cpp_out.

     

     

    Setp5:修改pb.ccpb.h后缀(主要是android平台的AL_CPP_EXTENSION选项不支持具有两个小数点的后缀,形如.pb.cc),同时修改头文件包含中的后缀

     

    可以看到 其中头文件“#include <google/protobuf/stubs/common.h>

    ”均找不到文件。所以设置项目的头文件路径到protobuffer

     

    显示正常

     

    编译通过

     

    Setp5:在HelloWorldScene.cpp中编写测试代码

     

    编译运行

     

    至此 win32平台protobuffer库添加完成。

    下面进行android平台protobuffer添加

    Setp1:先直接编译未添加protobuffer支持的android

     

     

    eclipse导入

    Setp2:解决问题,此类问题 很多 百度之

    找不到gen是因为没设置一个选项

     

     

     

     

    先注释掉在win32平台上添加的 TestName,保证最原始的android能编译通过

     

    碰到这个错误 记得复制一份源码就可以解决,解决方法如下:

     

     

     

    将它复制一份到

     

     

    Ok 搞定

     

    Eclipse中编译运行 构建成功

     

    Setp3:为protobuffer编写Android.mk

     

    原文如下:

    LOCAL_PATH := $(call my-dir)

     

    include $(CLEAR_VARS)

     

     

    TARGET_CPU_ABI := armeabi

    #TARGET_CPU_ABI := armeabi-v7a

    #TARGET_CPU_ABI := armeabi-v7a-hard

    #TARGET_CPU_ABI := armeabi

     

    #设置c++11头文件路径和链接路径

    prebuilt_stdcxx_PATH := E:\android-ndk-r9d-windows-x86_64\android-ndk-r9d\sources\cxx-stl\gnu-libstdc++\4.8

     

    LOCAL_C_INCLUDES += \

    $(prebuilt_stdcxx_PATH)/include \

        $(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI)/include

     

    LOCAL_LDFLAGS += -L$(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI) -lgnustl_static -lsupc++

        

    LOCAL_MODULE := protobuf_static

     

    LOCAL_MODULE_FILENAME := libprotobuf

     

    LOCAL_CPP_EXTENSION := .cc

     

    LOCAL_SRC_FILES := \

    src/google/protobuf/io/coded_stream.cc\

    src/google/protobuf/stubs/common.cc \

    src/google/protobuf/descriptor.cc \

    src/google/protobuf/descriptor.pb.cc\

    src/google/protobuf/descriptor_database.cc\

    src/google/protobuf/dynamic_message.cc\

    src/google/protobuf/extension_set.cc\

    src/google/protobuf/extension_set_heavy.cc\

    src/google/protobuf/generated_message_reflection.cc \

    src/google/protobuf/generated_message_util.cc \

    src/google/protobuf/io/gzip_stream.cc \

    src/google/protobuf/compiler/importer.cc\

    src/google/protobuf/message.cc\

    src/google/protobuf/message_lite.cc \

    src/google/protobuf/stubs/once.cc \

    src/google/protobuf/compiler/parser.cc\

    src/google/protobuf/io/printer.cc \

    src/google/protobuf/reflection_ops.cc \

    src/google/protobuf/repeated_field.cc \

    src/google/protobuf/service.cc\

    src/google/protobuf/stubs/structurally_valid.cc \

    src/google/protobuf/stubs/strutil.cc\

    src/google/protobuf/stubs/substitute.cc \

    src/google/protobuf/stubs/stringprintf.cc \

    src/google/protobuf/text_format.cc\

    src/google/protobuf/io/tokenizer.cc \

    src/google/protobuf/unknown_field_set.cc\

    src/google/protobuf/wire_format.cc\

    src/google/protobuf/wire_format_lite.cc \

    src/google/protobuf/io/zero_copy_stream.cc\

    src/google/protobuf/io/zero_copy_stream_impl.cc \

    src/google/protobuf/io/zero_copy_stream_impl_lite.cc

     

    #LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include/protobuf

     

    #LOCAL_C_INCLUDES := $(LOCAL_PATH)/include/protobuf

    #LOCAL_CFLAGS := -std=c99

    #LOCAL_CFLAGS += -std=c++11 -stdlib=libstdc++ -Wno-error=c++11-narrowing

     

    include $(BUILD_STATIC_LIBRARY)

     

    Setp4:打开项目的android.mk 添加 protobuffer相关

    原文如下:

     

    LOCAL_PATH :$(call my-dir)

     

    include $(CLEAR_VARS)

     

    $(call import-add-path,$(LOCAL_PATH)/../../cocos2d)

    $(call import-add-path,$(LOCAL_PATH)/../../cocos2d/external)

    $(call import-add-path,$(LOCAL_PATH)/../../cocos2d/cocos)

     

    LOCAL_MODULE := cocos2dcpp_shared

     

    LOCAL_MODULE_FILENAME := libcocos2dcpp

     

    LOCAL_SRC_FILES := hellocpp/main.cpp \

                       ../../Classes/AppDelegate.cpp \

                       ../../Classes/HelloWorldScene.cpp

     

    LOCAL_C_INCLUDES :$(LOCAL_PATH)/../../Classes

     

    LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static

    LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static

     

    # LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static

    # LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static

    # LOCAL_WHOLE_STATIC_LIBRARIES += spine_static

    # LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static

    # LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static

    # LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static

    LOCAL_WHOLE_STATIC_LIBRARIES += protobuf_static

     

    include $(BUILD_SHARED_LIBRARY)

     

    $(call import-module,.)

    $(call import-module,audio/android)

     

    # $(call import-module,Box2D)

    # $(call import-module,editor-support/cocosbuilder)

    # $(call import-module,editor-support/spine)

    # $(call import-module,editor-support/cocostudio)

    # $(call import-module,network)

    # $(call import-module,extensions)

     $(call import-module,external/protobuf-2.5.0)

     

    Setp5cygwin中进入protobuffer 运行./configure && make && make install产生config.h

     

    必须保证全部正常 否则后面会出现依赖问题

    这里如果出现错误 比如缺少包 工具 请运行cygwin setup.exe 选择internet模式 添加

     

    如果出现找不到google/protobuf头文件之类的。 将protobuffersrc下的所有.h拷贝一份 放到ndk的平台相关目录中:

     

     

     

     

    开始编译protobuffer

     

     

    链接 出现这种错误

     

    是因为我们头文件是用的gun stl 因此去修改 Application.mk 中的APP_STLgunstl_static即可

    原文如下:

    APP_STL := gnustl_static

    NDK_TOOLCHAIN_VERSION=clang

     

    APP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -std=c++11 -fsigned-char

    APP_LDFLAGS := -latomic

     

     

    APP_DEBUG :$(strip $(NDK_DEBUG))

    ifeq ($(APP_DEBUG),1)

      APP_CPPFLAGS +-DCOCOS2D_DEBUG=1

      APP_OPTIM := debug

    else

      APP_CPPFLAGS += -DNDEBUG

      APP_OPTIM := release

    Endif

     

    再次编译成功

     

    Setp6: 打开win32平台的测试代码,android.mk中添加编译部分

     

    成功被编译

     

    运行在小米3上 出现JCracker TestName

     

    这里截图不方便  就不放图了

     

    接下来 做序列化和反序列化

    TODO IOS支持

     

    0 0
    原创粉丝点击