Today we will start a tutorial on how to port the famous Cocos2dSimpleGame from objc to c++, using cocos2d-x engine, then run it on win32/iphone/android/wophone multi-platforms. The Cocos2dSimpleGame is written by Ray Wenderlich. Almost every beginner of cocos2d-iphone has read his tutorial series.
First of all, you need to download the last stable version of cocos2d-x from http://download.cocos2d-x.org. It’s cocos2d-0.99.5-x-0.8.2 when I write this article, and then update the game source to 0.9.1. The infix 0.99.5 means this c++ port is based on the parent cocos2d-iphone 0.99.5, and the suffix 0.8.2 is the version of our cocos2d–x port.
The source of Cocos2dxSimpleGame is here http://www.cocos2d-x.org/attachments/535/Cocos2dxSimpleGame-0.9.1.rar, I have tested it with cocos2d-1.0.1-x-0.9.1.
Let’s begin. You can jump to your platform directly from this catalogue.
cocos2d-win32 port is well tested on WinXP + VistualStudio2008/2010. But in vista/win7, I can not promise it runs well. Here’s a protal for unluck guys who cannot run this tutorial correctly on cocos2d-win32 port…
After downloading and unziping the package, you can go to the cocos2d-x folder, click “build-win32.bat” to compile the whole project, then click "install-templates-msvc.bat" to install the wizard. The bat scripts adapt vs2008 or 2010 automaticlly. For more detail, please refer to this document Cocos2d-x Application Wizard for Visual Studio User Guide
Now open cocos2d-x/cocos2d-win32.vc2008.sln via VS2008, we can see the libs here.
Right click the “Solution ‘cocos2d-win32.vc2008’ “, then “Add->New Project”,
select "cocos2d-x -> cocos2d-win32 Application", fill in the name box with "Cocos2dxSimpleGame", and use the default location setting.
NOTED: the location of games must stay in the directory of cocos2d-x root in win32 & wophone port.
Click “OK -> Next”, uncheck Box2D because Cocos2dSimpleGame will not use physical system. Make sure “SimpleAudioEngine in CocosDenshion” is checked, then “Finished”.
Now we can see the “Cocos2dxSimpleGame.win32” is created! Check the dependencies, our game depends on libcocos2d & libCocosDenshion now.
Well, build and run it, normally, you can see the following picture, if you are not so unlucky.
Perhaps someone can not run this HelloWorld successfully on win32, because the cocos2d-win32 port bases on the PowerVR OpenGL ES 1.1 SDK for PC Emulation (Windows ista/XP version)
http://www.imgtec.com/PowerVR/insider/sdkdownloads/index.asp#GLES1b
We integrated the PowerVR libraries in the path
cocos2d-x-root/cocos2dx/platform/win32/third_party/libraries
And the result of cocos2d-wophone HelloWorld is
We have templates for xcode now since version 0.8.2。 You can create cocos2d-x projects as easy as cocos2d-iphone projects.
At first, goto the root of cocos2d-x folder, then run the install-templates.sh in the mac shell
./install-templates-xcode.sh -u -f
and you can choose your xcode version here, xcode3 or xcode4.
After the installation finisehd, you can start the Xcode, then click "Create a new Xcode project". You can see this dialog
Select "cocos2d-x application", then click "choose", type in the project name. The cocos2d-x project will created by xcode for you.
Notice that, the cocos2d-x project for iOS can be create in anywhere as you wish. The wizard in xcode will copy the libraries of all cocos2dx, CocosDenshion, etc, into the project folder. This is different from the wizard for win32 & wophone in VisualStudio.
Ok, then build and run, you could see the HelloWorld scene as follows,
The project of android for ndk contains two parts, one for java, and the other for c++. We would run create-android-project.bat(or create-android-project.sh under linux) to create android ndk project.
"create-android-project.bat" is under the root of cocos2d-x. You should do some changes to work under your environment.
To adapt to my environment, I change the settings in create-android-project.bat.Now you can run "create-android-project.bat" to create an android project:
Goto the root of cocos2d-x, double click create-android-project.bat, input the project name and target id. Just like this
After version 0.8.2, you may be asked to enter the package path, the path format is like this: org.coco2dx.Cocos2dxSimpleGame.
The target ids are different in different environment, you should be sure to input valid target id. Now cocos2dx supports
2.1-update1 and Android 2.2, other versions have not been tested.
Now you can see a folder named "Cocos2dxSimpleGame" under the root of cocos2d-x. Run your cygwin, and goto
"Cocos2dxSimpleGame/android", then run "build_native.sh".
Run your eclipse, click File -> New -> Project -> Android Project, and import the project like this
Right click the project and select "Build Project", then right click the project and select "Run As" -> "Android Application". Then you can see the result:
You should run "create-android-project.sh" to create android project, the others are the same as under windows.
You should define some environment variables to run "create-android-project.sh" under your environment.联系客服