Cocos2d-x protobuf; cocos2dx protocol buffer

Source: Internet
Author: User
Tags android games

I learned yesterday that protocol buffer is used in the project. I checked this evening and learned that protobuf is essentially an information expression protocol + editing and parsing library.

 


All open-source software in linux is in the same mode. First,./configure -- help> build. sh to see what is there, and then configure make...

Get a simple write according to the official website documentation, and read the test to get a general idea of what the situation is.

 


Okay. Now we have used protocol buffer. We will compile it into cocos2dx.

Basically, it is the iphone simulator, iOS, and android ndk versions. (win has a project and I haven't tried it yet)

 

The original article is as follows:
By admin | on March 9, 2013 | in Blog

Protocol buffers are a flexible, efficient, automated mechanic for serializing structured data-think XML, but smaller, faster, and simpler. you define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of ages. as google puts it: "Protocol buffers Are now Google's lingua franca for data "and if it's good enough for google, then it must be worth checking out!

We use protocol buffers to serialize persistant data for our IOS and Android games. creating classes that persist data like shop, high scores, player preferences, achievements can be done in minutes with protobuf. and that why we♥Protobuf!

This is not a protobuf usage tutorial, for that, you can check out the official documentation: https://developers.google.com/protocol-buffers/docs/overview. what you won't find in the official documentation is how to make protobuf for iOS, and this is what this tutorial aims to explain. if you need to build protobuf for iOS, then read on


I'll show you how to build protobuf library for armv7, armv7s, and i386 architecture, then merge those libraries together into a single fat library that can be used in iOS projects. this library will work on the simulator as well as on the device.


-We used protobuf2.4.1 (the last stable version at the time of writing) https://code.google.com/p/protobuf/downloads/list


-Command line tools can be updated by going to Xcode-> Preferences .. then selecting the Downloads tab and selecting Command Line Tools


-To convert protobuf files into source code, you will need a protobuf compiler! You can build and install this compiler by going to protobuf directory and executing the following commands:
$./Configure
$ Make
$ Make check
$ Make install


To build the ios protobuf static library
Copy this script into protobuf directory and name it build-proto-ios.sh, then execute this to give the file execute privilegies:

$ Chmod a + x./build-proto-ios.sh
$./Build-proto-ios.sh

Go make a cup of coffie, this will take a few minutes You can also download the precompiled fat ios library from here.

After the compilation is done you will see a new folder under protobuf root called: ios-build. This folder contains libprotobuf-lite.a. This is a static library for iOS.


To import libprotobuf-lite.a into Xcode, just right click on a project directory where you want to import the library, from the drop-down list choose "Add Files to project-name ..." And add libprotobuf-lite.a library.


You still need to tell Xcode where to look for protobuf headers. to do this go to your project target build settings and under "Header Search Paths" add the path to protobuf-base-folder/src. the "protobuf-base-folder" being the folder in which you have the protobuf library from google.


Option optimize_for = LITE_RUNTIME;
At the start of your proto definition file.

For a tutorial on how to use protobuf, it's best to check out googles official documentation: https://developers.google.com/protocol-buffers/docs/overview

Happy protobuffing.

If you got any suggestions or question, feel free to ask/suggest!

------------------- The shell script mentioned in the article is as follows;

configure_for_platform() {export PLATFORM=$1#export PLATFORM=iPhoneOS echo "Platform is ${PLATFORM}"if [ "$PLATFORM" == "iPhoneSimulator" ]; thenexport ARCHITECTURE=i386export ARCH=i686-apple-darwin10fiif [ "$PLATFORM" == "iPhoneOS" ]; thenexport ARCHITECTURE=$2export ARCH=arm-apple-darwin10 fiexport ARCH_PREFIX=$ARCH- <SPAN style="COLOR: #ff0000">export SDKVER="6.0" </SPAN>export DEVROOT=/Applications/Xcode.app/Contents/Developer/Platforms/${PLATFORM}.platform/Developer export SDKROOT="$DEVROOT/SDKs/${PLATFORM}$SDKVER.sdk" export PKG_CONFIG_PATH="$SDKROOT/usr/lib/pkgconfig:$DEVROOT/usr/lib/pkgconfig" export AS="$DEVROOT/usr/bin/as" export ASCPP="$DEVROOT/usr/bin/as" export AR="$DEVROOT/usr/bin/ar" export RANLIB="$DEVROOT/usr/bin/ranlib" #export CPP="$DEVROOT/usr/bin/c++" #export CXXCPP="$DEVROOT/usr/bin/c++" export CC="$DEVROOT/usr/bin/gcc" export CXX="$DEVROOT/usr/bin/g++" export LD="$DEVROOT/usr/bin/ld" export STRIP="$DEVROOT/usr/bin/strip" export LIBRARY_PATH="$SDKROOT/usr/lib"export CPPFLAGS="" #export CFLAGS="-arch armv7 -fmessage-length=0 -pipe -fpascal-strings -miphoneos-version-min=4.0 -isysroot=$SDKROOT -I$SDKROOT/usr/include -I$SDKROOT/usr/include/c++/4.2.1/" export CFLAGS="-arch ${ARCHITECTURE} -fmessage-length=0 -pipe -fpascal-strings -miphoneos-version-min=4.0 -isysroot=$SDKROOT -I$SDKROOT/usr/include -I$SDKROOT/usr/include/c++/4.2.1/" export CXXFLAGS="$CFLAGS" #export LDFLAGS="-isysroot='$SDKROOT' -L$SDKROOT/usr/lib/system -L$SDKROOT/usr/lib/"export LDFLAGS="-arch ${ARCHITECTURE} -isysroot='$SDKROOT' -L$SDKROOT/usr/lib/system -L$SDKROOT/usr/lib/"./configure --host=${ARCH} --with-protoc=protoc --enable-static --disable-shared }mkdir ios-build#build for iPhoneSimulatorconfigure_for_platform iPhoneSimulatormake clean<SPAN style="COLOR: #ff0000">make</SPAN>
<SPAN style="COLOR: #ff0000">cd src;make libprotobuf-lite.la;cd ..</SPAN>cp src/.libs/libprotobuf-lite.a ios-build/libprotobuf-lite-i386.a#build for iPhoneOS armv7configure_for_platform iPhoneOS armv7make cleanmake
<PRE class = cpp name = "code"> <SPAN style = "COLOR: # ff0000"> cd src; make libprotobuf-lite.la; cd .. </SPAN> </PRE> <DIV> <SPAN style = "COLOR: # ff0000"> <BR> </SPAN> </DIV> cp src /. libs/libprotobuf-lite.a ios-build/libprotobuf-lite-armv7.a # build for iPhone OS armv7sconfigure_for_platform iPhoneOS armv7smake cleanmake <PRE> </PRE> <PRE class = cpp name = "code" sizcache = "1" sizset = "6"> <PRE class = cpp name = "code"> <SPAN style = "COLOR: # ff0000"> cd src; make libprotobuf-lite.la; cd .. </SPAN> </PRE> cp src /. libs/libprotobuf-lite.a ios-build/libprotobuf-lite-armv7s.amake clean # cerate a fat library containing all achitectures in libprotobuf-lite.axcrun-sdk iphoneos lipo-arch armv7 ios-build/libprotobuf-lite-armv7.a-arch armv7s ios-build/libprotobuf-lite-armv7s.a- arch i386 ios-build/libprotobuf-lite-i386.a-create-output io S-build/libprotobuf-lite.a <PRE> </PRE> <PRE class = cpp name = "code"> the red part of the script is my change point. (Sorry, after the release, we found that html tags cannot be referenced in the code section. Instead, it messed up the script. You can see it carefully. </PRE>

1. My xcode version is 6.1. This version affects the cross compiler address. If it is incorrect, you will be prompted during make, which is easy to fix.
2. We only need to compile a lite library, compiler and test... It has nothing to do with us, so, directly use make libprotobuf-lite.la to compile the relevant 16 files. The chances of errors are much lower. (You do not need to compile and install a protoc as described in the article .)
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.