GREE Engineering

Integrate GREE Platform SDK into your cocos2dx game for Android

Integrate GREE Platform SDK into your cocos2dx game for Android

Hi everyone, I’m kazuma from GREE. I have a strong interest in cross-platform development tools that allow developers to write code once and deploy to multiple platforms.

Integrate GREE Platform SDK into your cocos2dx game for Android (will support iOS in the future)

This guide provides step-by-step instructions on how to integrate GREE SNS features (User information, FriendList, Payment, Achievement, Leaderboard and so on) with your cocos2dx applications.

This article is a little bit lengthy, but I hope it will be interesting for you, and inspire you to try it out yourself.

Requirements

You should have Eclipse with the Android Developer Tools installed. If not, please setup your environment with the instructions at:
http://developer.android.com/sdk/installing/installing-adt.html

Get GREE Platform SDK for Android

The latest version of the GREE Platform SDK is available from:
https://developer.gree.net
https://developer.gree.net/en/
Please download the SDK for Android. Instructions in this article are written for GREE SDK version v3-3-8.

You can also find a Developer’s Guide at the URL above. Please follow the instructions in the guide and import the SDK into Eclipse.
To activate your GREE Application, you need a GREE Application Id, Consumer Key, Consumer Secret, and edit your config files so that they are properly set. I will show you how to do that later in this guide.
Detailed instructions can also be found in the Developer’s Guide at the URL above.

Get AndroidNDK

Cocos2dx is written in C++ and needs to be built with AndroidNDK.
AndroidNDK is available from:
http://developer.android.com/tools/sdk/ndk/index.html
Please download and extract it to the proper location on your machine.

Get Cocos2d-x and GREE Platform SDK for Cocos2d-x

Now that you’ve finished setting up the basic environment for developing a cocos2dx application, its time to start building it. Get GREE Platform SDK for Cocos2d-x.

setup.sh downloads cocos2dx source code and adds GREE Platform SDK for Cocos2d-x code to it. It also provides a sample application which connects to the GREE Platform.

Create a cocos2dx project

Normally you would create a cocos2dx application project with create-android-project.sh. With this binding however, we shall use create-android-gree-project.sh to create a cocos2dx application that connects to the GREE Platform. create-android-gree-project.sh requires both Android NDK and Android SDK, so you need specify their paths for the script to execute properly. You can directly modify create-android-gree-project.sh, or set their paths as as environmental variables.

You will get a couple of prompts. Please provide values for the fields requested.
“Name of package”
“API version your app support”
“Project name of your app”

Now you have a cocos2dx sample application that connects to the GREE Platform.

Build the project

Add the project

Launch Eclipse, and add the created project and cocos2dx java files.
File -> New -> Others

Select Android -> Android Project from Existing Code.

Select the “proc.android” directory of the created project.

Select the “./cocos2dx/platform/android/java” directory under cocos2dx source code.

Project Setup

From the project explorer window, choose “java” project and select Properties. Set GreeSDK as reference library.

Now you should have no errors with java. Next, you have to configure the build environment for native code.

Configure the build environment for native code

(You can skip this step if you are going to build native code with the command line)
To build native code on Eclipse, you have to install CDT for Eclipse.
Select Help -> Install New Software…. from the main menu, and select http://download.eclipse.org/releases/xxxxx, check “C/C++ Development Tools”, and install it.

Select File -> New -> Others and select C/C++ -> Convert to a C/C++ Project (Adds C/C++ Nature)

Check the project you created, and select the Makefile project from the Project type window.

From the Project Explorer window, select your project -> Properties.
Select C/C++Build -> Environment and add NDK_ROOT environment variable.

Select C/C++Build, uncheck Use default build command and set “bash < path of the build_native.sh on your machine >”

The steps above need to be done only once for each project.

Now, you can build your native code with the button below.

Set information required for GREE Platform

Set your Application Id, Consumer Key, and Consumer Secret in res/xml/gree_platform_configuration.xml.

Also replace “xxxxx” in AndroidManifest.xml with your Application Id.

These steps are the same as the steps performed when creating an android application for the GREE platform.

Launch Application

Your are now ready to run the application. It should show up like this:

You can log in to GREE Platform by clicking on the icon in the top right corner.

The default sample app, “GreeBasicSample”, includes Authorizer, Dashboard, User, Friends, Payment, Achievement, Leaderboard APIs.

How to use each API

General

To use GREE Platform SDK extensions,

Callbacks

GREE Platform SDK uses callbacks for several APIs. To implement them, please inherit CCGreePlatformDelegate class. In the default sample:

Next, call the setDelegatexxxx method, and implement callback method to be called.
If you’d like to catch authorizeAuthorized, authorizeError, and authorizeCancel callbacks of the Authorizer APIs — authorizeAuthorized, authorizeCancel, authorizeError –. please override them in HelloWorld.h.

If you don’t need to handle a specific callback, you don’t have to implement it. (for example, it is okay to leave the authorizeCancel method without implementing it, if you don’t need to catch the cancel callback)

In HelloWorld.cpp:

The available callbacks are shown in the corresponding CCGreeXXXXDelegate class in each CCGreeXXXX.h file. For example, if you’d like to get loadThumbnail callback, the prototype of the method is:

This callback returns thumbnail data as a CCImage. You can use it for example in the following way:

You can do anything with it as you could with a sprite; draw, move, scale and so on.
Note: Callbacks don’t run on the UI thread, so you cannot perform draws in callbacks.

Authorizer API

The basic usage of the Authorizer API is:

If you’d like to log out, call the logout method:

You can also get Authorizer related information.

Currently supported APIs are:

User and Friend APIs

With User and Friend APIs, you can get user information (e.g. name, age, gender and so on) of users, as well their friends’ information.
To get login user information:

To load the user’s avatar thumbnail:

To get a list of friends:

Currently supported APIs are:

Payment APIs

You can create Payment items and submit Payment requests. To submit a Payment request, you have to create Payment Items first, and specify them in the request.

Implement callback methods as required.

Currently supported APIs are:

Achievement APIs

You can load a list of Achievements:

Get the achievement list by implementing the success callback.
As with the friends list, achievement items retrieved are stored in a CCArray.

You can lock/unlock the achievements:

And get the corresponding lock/unlock icon as a CCImage:

Currently supported APIs are:

Dashboard API

To launch Dashboard, simply use:

Leaderboard API

You can load the Leaderboard list, and get information on each item in the list:

Also you can set the score for each leaderboard you loaded:

Dialogs(InviteDialog, RequestDialog, ShareDialog)

You can create and display dialogs with this library. Below is an example that shows how to display an InviteDialog. Pass parameters as CCDictionary, as how you would do normally with cocos2dx.

関連記事