Cocos2dx For Multi-Platform: Chapter 1 - How to Create a New cocos2d-x project in multi-platforms

Chapter 1 - How to Create a New cocos2d-x project on multi-platforms

This article is written by Walzer and modified by FlyingPacer, all rights reserved.

(http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_1_-_How_to_Create_a_New_cocos2d-x_project_in_multi-platforms)

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.0 when I write this article. The infix 0.99.5 means this c++ port is based on the parent cocos2d-iphone 0.99.5, and the suffix 0.8.0 is the version of our cocos2d–x port.

Let’s begin. You can jump to your platform directly from this catalogue.

  • Chapter 1 - How to Create a New cocos2d-x project in multi-platforms
    • 1. start with win32
      • 1.2 install Wizard on VisualStudio
      • 1.3 Create a new cocos2d-win32 project
      • 1.4 Build and Run
      • 1.5 Portal for unlucky guys
    • 2. Start with WoPhone
    • 3. Start with iPhone
      • 3.1 Recommend: using templates
      • 3.2 Discard: create project manually
        • 3.2.1 Create a new project
        • 3.2.2 Add cocos2d-iphone-cpp srouces
        • 3.2.3 Config target info of cocos2d-x
        • 3.2.4 Copy source from HelloWorld to Cocos2dSimpleGame
        • 3.2.5 Config target info of Cocos2dSimpleGame
        • 3.2.6 Build and run
    • 4. Start with Android
      • 4.1 Create new android project
      • 4.2 Create new package
      • 4.3 Copy files from HelloWorld
      • 4.4 Change some files
      • 4.5 Build project
      • 4.6 Run

1. start with win32

cocos2d-win32 port is well tested on WinXP + VistualStudio2008. But in vista/win7, or vs2010, I can not promise it runs well. Here’s a portal for unlucky guy who cannot run this tutorial correctly on cocos2d-win32 port…

1.2 install Wizard on VisualStudio

After download and unzip the package, you can go into cocos2d-x\template\CCApplicationWizard.vs, click “InstallWizardForVS2008.js” to install. It’s the wizard in Microsoft VistualStudio 2008.

193

1.3 Create a new cocos2d-win32 project

Now open cocos2d-x/cocos2d-win32.vc2008.sln via VS2008, we can see the libs here.

194

Right click the “Solution ‘cocos2d-win32.vc2008’ “, then “Add->New Project”,

195

select "cocos2d-x -> cocos2d-win32 Application", fill in the name "Cocos2dxSimpleGameForWin32", 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 physics. Make sure “SimpleAudioEngine in CocosDenshion” is checked, then “Finished”.

1.4 Build and Run

Now we can see the “Cocos2dxSimpleGameForWin32.win32” is created! Check the dependencies, our game depends on libcocos2d & libCocosDenshion now.

106

Well, build and run it, if you are not unlucky enough, you can see

196

1.5 Portal for unlucky guys

I can not promise that everyone can run this HelloWorld successfully on win32. Because the cocos2d-win32 port is based 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

So:
  • For win7 users: PowerVR has not release Windows7 port yet. But you still can have a try. Many win7 users in cocos2d-x community successfully run this case.
  • For winxp/vista users: If HelloWorld crashed at any glXXX or eglXXX function, please update the driver of your video card, goto the website of NVIDIA/ATI/INTEL to look for a last driver
  • For unlucky users: You can create cocos2d-x project in XCode manually, or goto church/temple to look for a better luck :p

2. Start with WoPhone

If you are a wophone developer, the steps of win32 above also fits you. There are only 3 differences:
  1. you must locate cocos2d-x-root in D:/Work7, wophone has a strict app path requirement.
  2. please open D:/Work7/cocos2d-x/cocos2d-wophone.sln via VS2008 instead of cocos2d-win32
  3. select “cocos2d-wophone application” in the “Add New Project” dialog, just like this

197

And the result of cocos2d-wophone HelloWorld is

198

3. Start with iPhone

3.1 Recommend: using templates

We have templates for xcode now since version 0.8.0! 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

sudo ./install-templates-xcode3.sh

After the installation finisehd, you can start the Xcode, then click "Create a new Xcode project". You can see this dialog

210

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,

211

3.2 Discard: create project manually

If you perfer to do this job manually, and make every config clear, you can follow the steps below.

3.2.1 Create a new project

Open XCode, “Create a new Xcode project”, choose iOS -> Application -> View-based Application, save as “Cocos2dSimpleGame”

101

Context menu -> “Add -> New Target”, then choose iOS -> Cocoa Touch -> Static Library, click “next”, set target name as “cocos2d-x”, click “Finish”. Then close “target cocos2d-x info” dialog directly, we will config it layer.

3.2.2 Add cocos2d-iphone-cpp srouces

Now right click “Targets-> cocos2d-x -> Add -> Existing Files”, select the cocos2d-x/cocos2dx folder, recursively add to cocos2d-x target.

110

You can see “cocos2dx” group in Cocos2dSimpleGame now. But we need to remove the sources for other platforms.

109

Delete references of
  • platform/android folder
  • platform/wophone folder
  • platform/win32 folder
  • proj.wophone folder
  • proj.win32 folder
  • Android.mk file
3.2.3 Config target info of cocos2d-x

Now double click “Targets -> cocos2d-x”, In the “Target cocos2d-x info” dialog, we had to modify it, follow the items below, don’t miss anything

  • Build tab
    • Search Paths
      • Header Search Paths, add:
        • $(SRCROOT)/../cocos2dx/
        • $(SRCROOT)/../cocos2dx/platform/iphone/third_party/opengles/
        • $(SRCROOT)/../cocos2dx/platform/iphone/third_party/libxml2/
    • GCC 4.2 – Preprocessing
      • Preprocessor Macros, add:
        • TARGET_OS_IPHONE
        • USE_FILE32API
        • GL_GLEXT_PROTOTYPES

OK. Build Targets->cocos2d-x, you’ll successfully get the libcocos2d.a now

3.2.4 Copy source from HelloWorld to Cocos2dSimpleGame
Copy these 6 files to cocos2d-x/Cocos2dSimpleGame/Classes folder:
  • HelloWorld/AppDelegate.cpp
  • HelloWorld/AppDelegate.h
  • HelloWorld/HelloWorldScene.cpp
  • HelloWorld/HelloWorldScene.h
  • HelloWorld/iphone/AppControl.h
  • HelloWorld/iphone/AppControl.mm
Copy 3 png files from HelloWorld/Resource folder to Cocos2dSimpleGame/Resource folder
  • CloseNormal.png
  • CloseSelected.png
  • HelloWorld.png
Add these 6 files into Xcode->Cocos2dSimpleGame/Classes, and remove these 8 references
  • Classes
    • Cocos2dSimpleGameAppDelegate.h
    • Cocos2dSimpleGameAppDelegate.m
    • Cocos2dSimpleGameViewController.h
    • Cocos2dSimpleGameViewController.m
  • Other sources
    • main.m
  • Resources
    • Cocos2dSimpleGameViewController.xib
    • MainWindow.xib

The result in finder is

103

And the result in Xcode is

104

3.2.5 Config target info of Cocos2dSimpleGame

General tab, add dependencies & linked libraries

105

Build tab
  • Search Paths
    • Header Search Paths, add below:
      • $(SRCROOT)/../cocos2dx/
      • $(SRCROOT)/../cocos2dx/platform/iphone/third_party/opengles
      • $(SRCROOT)/../cocos2dx/platform/iphone
  • GCC 4.2 – Code Generation
    • Inline Methos Hidden [uncheck]
    • Symbols Hidden by Default [uncheck]
  • GCC 4.2 – Preprocessing
    • Preprocessor Macros, add below:
      • TARGET_OS_IPHONE

At last, open Cocos2dSimpleGame-Info.plist, check the last item “Main nib file base name” = NULL.

3.2.6 Build and run

OK, build and run, you can see the cocos2d logo shown on the simulator

100

4. Start with Android

The project of android for ndk contains tow parts. One for java, and another for c++.

4.1 Create new android project

Open eclipse, File->New->Project...
205
206

4.2 Create new package

Right click folder src-> New-> Packgage.
Notice that the package name should be org.cocos2dx.lib, because it is already defined in the cocos2dx engine.
204

4.3 Copy files from HelloWorld

Copy 3 files to Cocos2dSimpleGameForAndroid/src/org/cocos2dx\lib
  • HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxActivity.java
  • HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java
  • HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxRenderer.java
Copy folder to Cocos2dSimpleGameForAndroid
  • HelloWorld/android/jni
Copy folder to Cocos2dSimpleGameForAndroid
  • HelloWorld/android/Resource
Copy 4 files to Cocos2dSimpleGameForAndroid/jni/helloworld
  • HelloWorld/AppDelegate.h
  • HelloWorld/AppDelegate.cpp
  • HelloWorld/HelloWorldScene.cpp
  • HelloWorld/HelloWorldScene.h
Copy 1 files to Cocos2dSimpleGameForAndroid/
  • HelloWorld/android/build_native.sh

4.4 Change some files

  1. change Cocos2dSimpleGameForAndroid/jni/Android.mk
    The new file contents are
    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)

    subdirs := $(addprefix $(LOCAL_PATH)/http://www.cnblogs.com/,$(addsuffix /Android.mk, \
    cocos2dx \
    ))
    subdirs += $(LOCAL_PATH)/helloworld/Android.mk

    include $(subdirs)
  2. change Cocos2dSimpleGameForAndroid/jni/helloworld/Android.mk
    New file contents are
    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE := helloworld

    LOCAL_SRC_FILES := main.cpp \
    AppDelegate.cpp \
    HelloWorldScene.cpp

    LOCAL_C_INCLUDES := $(LOCAL_PATH)/http://www.cnblogs.com/../cocos2dx \
    $(LOCAL_PATH)/http://www.cnblogs.com/../cocos2dx/platform \
    $(LOCAL_PATH)/http://www.cnblogs.com/../cocos2dx/include \
    $(LOCAL_PATH)

    LOCAL_LDLIBS := -L$(LOCAL_PATH)/http://www.cnblogs.com/libs/armeabi -lcocos2d -llog

    # it is used for ndk-r5
    # because the new Windows toolchain doesn't support Cygwin's drive
    # mapping (i.e /cygdrive/c/ instead of C:/)
    # LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/http://www.cnblogs.com/libs/armeabi) \
    # -lcocos2d -llog

    include $(BUILD_SHARED_LIBRARY)

    If your ndk version is r5, you should read
    http://www.cocos2d-x.org/wiki/cocos2d-x/How_to_run_HelloWorld_on_ndk_r4_and_r5
    and change Cocos2dSimpleGame/jni/helloworld/Android.mk and cocos2dx/Android.mk.
  3. change Cocos2dSimpleGameForAndroid/src/org/cocos2dx/cocos2dsimplegameForAndroid/Cocos2dSimpleGameForAndroid.java
    New file contents are
     1 package org.cocos2dx.Cocos2dxSimpleGameForAndroid;
    2 import org.cocos2dx.lib.Cocos2dxActivity;
    3 import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
    4
    5 import android.opengl.GLSurfaceView;
    6 import android.os.Bundle;
    7
    8 public class Cocos2dxSimpleGameForAndroid extends Cocos2dxActivity {
    9 protected void onCreate(Bundle savedInstanceState){
    10 super.onCreate(savedInstanceState);
    11
    12 // get the packageName,it's used to set the resource path
    13 String packageName = getApplication().getPackageName();
    14 super.setPackgeName(packageName);
    15
    16 mGLView = new Cocos2dxGLSurfaceView(this);
    17 setContentView(mGLView);
    18 }
    19
    20 @Override
    21 protected void onPause() {
    22 super.onPause();
    23 mGLView.onPause();
    24 }
    25
    26 @Override
    27 protected void onResume() {
    28 super.onResume();
    29 mGLView.onResume();
    30 }
    31
    32 private GLSurfaceView mGLView;
    33
    34 static {
    35 System.loadLibrary("cocos2d");
    36 System.loadLibrary("helloworld");
    37 }
    38 }
  4. change Cocos2dSimpleGameForAndroid/AndroidManifest.xml
    New file contents are
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.cocos2dx.Cocos2dxSimpleGameForAndroid"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Cocos2dxSimpleGameForAndroid"
    android:label="@string/app_name">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    </application>
    </manifest>
  1. change Cocos2dSimpleGameForAndroid/build_native.sh
    New file contents are
    # set params
    ANDROID_NDK_ROOT=/cygdrive/d/anroid/android-ndk-r4-crystax
    COCOS2DX_ROOT=/cygdrive/d/Work7/cocos2d-x
    HELLOWORLD_ROOT=$COCOS2DX_ROOT/Cocos2dxSimpleGameForAndroid
    # make sure assets is exist
    if [ -d $HELLOWORLD_ROOT/assets ]; then
    rm -rf $HELLOWORLD_ROOT/assets
    fi
    mkdir $HELLOWORLD_ROOT/assets
    # copy resources
    for file in $HELLOWORLD_ROOT/Resource/*
    do if [ -d $file ]; then
    cp -rf $file $HELLOWORLD_ROOT/assets
    fi

    if [ -f $file ]; then
    cp $file $HELLOWORLD_ROOT/assets
    fi
    done
    # build
    pushd $ANDROID_NDK_ROOT
    ./ndk-build -C $HELLOWORLD_ROOT $*
    popd

4.5 Build project

Implement the file build_native.sh as follows:

Administrator@XXX /cygdrive/d/Work7/Cocos2d-x/Cocos2dxSimpleGameForAndroid
$ ./build_native.sh

4.6 Run

Use eclipse to run the programe. Right click your programe->Run as->Android Application.
You can see the result in android simulater.
208

转载于:https://www.cnblogs.com/FlyingPacer/archive/2011/03/21/1990284.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值