Cocos2d-x setup

This guide will walk you through a step-by-step process to set up a basic Cocos2d-x project using the Gamedonia Backend Platform.

Get the Cocos2d-x Gamedonia SDK

Log in to Gamedonia and get the SDK at the Download Area. We recommend checking the SDK version, as we're regularly updating them.

Integrate game with backend

Setup Cocos2d-x Project

  1. Download Cocos2d-x from the official website. The last version Gamedonia supports is Cocos2d-x 2.2.5. Click here to download.

  2. Unzip the downloaded Cocos2d-x SDK in the Cocos2d-x root folder.

  3. Create a new Cocos2d-x project using the create_project.py script located at <cocos2d-x-root-folder>/tools/project-creator. Use a command like the following from a shell:

    ./create_project.py -project MyGame -package com.MyCompany.AwesomeGame -language cpp

Setup iOS Project

  1. Go to the folder <cocos2d-x-root-folder>/projects/MyGame/proj.ios/. Open the XCode project located there.

  2. Add the Gamedonia source files to the project. You'll find the Gamedonia SDK files at <cocos2d-x-root-folder>/Gamedonia_Cocos2d-x_x.x_SDK. Drag the folder from Finder to the root of the project. In the popup, select the following options:

  3. As the GamedoniaSDK is also Android compatible, we need to remove some folders from the imported Gamedonia folder. Remove References to Gamedonia/android and Gamedonia/external. We only need these folders in the Android project.

  4. Gamedonia includes a social plugin that works with Facebook, so we need to download the Facebook SDK (3.23) and add a reference to the FacebookSDK.framework in our project to be able to compile it. You can download it here.

  5. Once you've downloaded the Facebook SDK, drag it to your project frameworks folder. The project tree should look like the one in the next image:

  6. We also need to add the Storekit.framework. Go to your project Build Phases option and add the Storekit.framework by clicking on + under the Link Binary With Libraries subfolder.

  7. Now the project should be built without errors.

Setup Android Studio Project

  1. Download the Android NDK from here.

  2. Go to cocos2d-x-2.2.5/projects/your_game/proj.android/ and edit build_native.sh to point to the NDK folder like this:

    # paths
    NDK_ROOT=path/to/the/android-ndk-...
    if [ -z "${NDK_ROOT+aaa}" ];then
    echo "please define NDK_ROOT"
    exit 1
    fi     
    		
  3. Go to cocos2dx-x2.2.5/projects/your_game/proj.android/jni/ and edit Android.mk like this:

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    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 \
                        $(COCOS2DX_PATH)/Gamedonia_Cocos2d-x_1.3_SDK
    
    LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
    LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
    LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
    LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
    LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
    LOCAL_WHOLE_STATIC_LIBRARIES += gamedonia_static
    
    include $(BUILD_SHARED_LIBRARY)
    
    $(call import-module,cocos2dx)
    $(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
    $(call import-module,CocosDenshion/android)
    $(call import-module,extensions)
    $(call import-module,external/Box2D)
    $(call import-module,external/chipmunk)
    $(call import-module,Gamedonia-1.3/android)
    		
  4. Open a terminal, go to cocos2d-x-2.2.5/projects/your_game/proj.android/ and execute build_native.sh like this:

    ./build_native.sh
    		

    If you get an error in a file called LabelReader.cpp, go to cocos2d-x-2.2.5/extensions/CocoStudio/Reader/WidgetReader/LaberlReader/LabelReader.cpp and add this line:

    #include <algorithm>
    		
  5. Download Android Studio from here and open it.

  6. Select File > Import Project... and choose your proj.android folder in cocos2d-x-2.2.5/projects/your_game/.

  7. Choose an import destination directory and click on Next.

  8. Leave all checkboxes marked and click on Finish.

  9. In the file system, copy the gamedoniaandroidcocos2dxsdk folder of the Gamedonia SDK at the root of your project folder.

  10. Now your project structure should look like this (Android Studio detects the new folder automatically):

  11. Open the build.gradle file of the folder you just copied and substitute the compile facebook line with this one:

    compile 'com.facebook.android:facebook-android-sdk:3.23.0'
    		
  12. Open the settings.gradle file and add this line:

    include ':gamedoniaandroidcocos2dxsdk'
    		
  13. Open the local.properties file adding the location of your NDK:

    ndk.dir=/path/to/the/android-ndk-...
    		
  14. Open build.gradle file of your project and add the following changes:

    • Set the compileSdkVersion, minSdkVersion and targetSdkVersion to 15 or higher.

    • Add the next 3 lines to the dependencies:

      compile 'com.facebook.android:facebook-android-sdk:3.23.0'
      compile 'com.google.android.gms:play-services:7.3.0'
      compile project(':gamedoniaandroidcocos2dxsdk')
      				
    • Add this after the defaultConfig section:

      sourceSets.main {
          jni.srcDirs = []
      }
      
      packagingOptions {
          exclude 'META-INF/LICENSE'
          exclude 'META-INF/NOTICE'
          exclude 'META-INF/NOTICE.txt'
          exclude 'META-INF/LICENSE.txt'
      }
      				
  15. Open the AndroidManifest.xml file of your project and add the following lines:

    <activity android:name="com.facebook.LoginActivity"/>
    <activity android:name="com.gamedonia.sdk.social.LoginActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"></activity>
    <activity android:name="com.gamedonia.sdk.social.DialogActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"></activity>
    		

Setup Eclipse ADT Project

  1. Import the Android project in your Eclipse ADT development environment. You'll find the android project at <cocos2d-x-root-folder>/projects/MyGame/proj.android/.

  2. Import the Google Play Services project into your Eclipse ADT. You should find the project at <your-adt-install-folder>/sdk/extras/google/google_play_services/libproject/google-play-services_lib. If it's not present, then open the ADT Download Manager and check it to install.

  3. Download the Android Facebook SDK. You can get it here.

  4. Unzip the downloaded Facebook SDK. Import the Android project to <facebook-sdk-root>/facebook in your Eclipse ADT.

  5. Import to Eclipse ADT the libcocos2dx Android project. You can find it at <your-cocos2dx-root-folder>/cocos2dx/platform/android/java.

  6. From Eclipse ADT, right click on your Android game project and select the option Properties. Select the option Androidand in the libraries section at the bottom, add the following projects that already exist in your ADT environment:

    1. libcocos2dx.

    2. FacebookSDK.

    3. google-play-services.

  7. Your Android libraries for the project should look like in the following image.

  8. From your <cocos2d-x-root-folder>/projects/MyGame/proj.android run the build_native.sh script to build your project.

  9. If everything worked correctly, now you should be able to run the app from Eclipse ADT.

Create a Game in Gamedonia

We need to create a game in the Gamedonia Dashboard to be able to continue. The Dashboard is the developer tool that will allow you to manage all the related modules of the game. Now let's see how we can create a Game.

  1. Click the Add New Game button.

  2. Write the name of your new game, select the platform and press Save when ready, your game will be created inmediately and will be ready to start communicating with your game client.

Configure the Cocos2d-X Project

  1. The last part is initializing the GamedoniaSDK inside your project using your Gamedonia credentials. To do this, write the following line in your AppDelegate.cpp file inside your project Classes folder. Go to the Game Management > Information page to pick the credentials for your game.

    We recommend using Xcode in MacOSX for the development as it's easier to setup the project.

#include "GamedoniaSDK.h"
.
.
.
bool AppDelegate::applicationDidFinishLaunching() {
    ... //Common Cocos2d-x stuff

        GamedoniaSDK::initialize("<your-api-key>", "<your-shared-secret>", "http://api.gamedonia.com", "v1");

}

When developing your app for the App Store or Google Play or whatever public market you should use the normal key that will respect all the security policies defined in your app. Use this as your default.

The master key lets you override permissions and access all entities and collections in the game, ignoring their Read/Write permissions.

User Authentication

Now your app is ready to perform calls to Gamedonia Backend!

You may be wondering what could be the first thing to do now that you have access to your own game backend. Why not create a User and log him in? You will need a logged in user (valid Gamedonia session) to invoke most of the Gamedonia API calls.

Create your first user

We will start with an easy example, where we create a user with email credentials ( email and password ). Here's how to create a user with Cocos2d-x.

Login a user

The call to log in a user is the one that gets you a session token and, with it, the authentication to perform the rest of the API calls of the Gamedonia API. The credentials must be the ones of a created user at the Users collection. See how to log in a user with Cocos2d-x.