Chartboost integration in Cocos2dx game

Recently i created a library called EasyNDK (https://github.com/aajiwani/EasyNDK-for-cocos2dx). I was very happy when my code got live, to me it made complete sense, but i guess that wasn’t the case for all the developers out there. Integrating their own SDKs or using 3rd party SDKs wasn’t easy using my code as it seems to be. So, i thought to create a simple tutorial for the most common and pretty easy SDK that is available in both iOS and Android, known as Chartboost (https://www.chartboost.com).

Lets see how easy is the process to integrate it using EasyNDK to have a single code base in C++ and integrate it with Android(Java) and iOS(Objective-C).

1.) Go to Chart boost’s website to create an account or to already use one. (https://www.chartboost.com/)

2.) Download their SDK for the required platform

3.) Create the required application with settings under your account from your dashboard. (https://dashboard.chartboost.com/)

4.) For EasyNDK to work as expected, lets make a receiver held responsible from the native side (Java / Obj-C) to receive messages from C++ and execute them on the native platforms. Here are the steps to be taken.

  • For iOS, in RootViewController.mm

    #include "IOSNDKHelper.h"

    Add this line of code in init method of your RootViewController

    [IOSNDKHelper SetNDKReciever:self];

  • For Android, in your MainActivity.java


    import org.json.JSONException;
    import org.json.JSONObject;
    import com.easyndk.classes.AndroidNDKHelper;

    Add this line of code in onCreate method of your MainActivity

    AndroidNDKHelper.SetNDKReciever(this);

5.) Lets integrate the SDKs with the help of guide available on Chart boost’s site. Please note do not write the Interstitial on the applicationDelegate in iOS or in onCreate method of your activity in Android. I will later show how we will write the showInterstitial method in both platforms. Else than that follow every step that is written under the help sections.

6.) This is the time where our EasyNDK will work, for simplicity in the attached example i am working with the basic hello world project of Cocos2dx, i will simply add a pin button on the left hand side of the close button that is already provided in the example. On tapping it, the showInterstitial method will start working, lets see how.

Step 1.) In your class file (C++) where you want to see the chart boost button, add a button and attach a callback function to it, for here i am doing it in HelloWorldScene.cpp

  • Includes


    #include "NDKHelper.h"
    #include
    using namespace std;

  • Adding a button to the menu in the scene


    // Chartboost button to load interstitial
    CCMenuItemImage *pChartBoostItem = CCMenuItemImage::create(
    "sample_pin.png",
    "sample_pin.png",
    this,
    menu_selector(HelloWorld::chartboostInterstitialCallback) );

    // Adjust Chartboost button
    pChartBoostItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 80, 20) );

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, pChartBoostItem, NULL);

  • Adding the call back that will respond to the button click and will forward message to the native language accordingly, using EasyNDK helper.


    void HelloWorld::chartboostInterstitialCallback(CCObject* pSender)
    {
    // If you want to load interstitial in your desired platform
    // Simply message the platform to do it for you
    // Like this
    SendMessageWithParams(string("LoadInterstitial"), NULL);
    }

That is all from the C++ side, we can now successfully call the Interstitial method from any platform using the same code. Lets implement the handler on both the platform and see the magic happening.

Step 2.)Adding the message handler on both the platforms, using only the ones that are required. Be sure to name the methods(Java) or messages(Objective-C) to be of the same string as you passed from C++. For here i have passed LoadInterstitial as the string from C++ hence i will implement the method with the same name.

  • For iOS, in RootViewController file, as we have attached the receiver to be RootViewController, add this code.


    - (void) LoadInterstitial:(NSObject *)prms
    {
    // Show an interstitial
    [[Chartboost sharedChartboost] showInterstitial];
    }

  • For Android, in MainActivity file, as we have attached the receiver to be MainActivity, add this code.


    public void LoadInterstitial(JSONObject prms)
    {
    // Show an interstitial
    this.cb.showInterstitial();
    }

7.) This is it, we have successfully integrated Chart boost SDK on both Android and iOS with a single code base in C++. Similarly you can connect any SDK you like that is already available on the native platform to work with Cocos2dx.

For the ease and saving time, i am adding sample projects for both iOS and Android with Chart boost already included, so that you can download and test the application if you haven’t understood from the above tutorial. Here are the links to the required downloads.

EasyNDK
EasyNDK Wiki Link
Chart Boost sample iOS project
Chart Boost sample Android project

Thanks for reading the post.

19 thoughts on “Chartboost integration in Cocos2dx game

  1. Hi Ameer,

    Nice Tutorial and an awesome library. Would it be possible for you to write a tutorial and add a sample project on integrating Flurry as well ? It would be a very nice learning experience.

    Thanks.

  2. Hi Ameer, Thanks for your hard work. In my case, I try to call the method like following on objective c and the app stop at CCObject.cpp line 94 (retain method) (reference count shold greater than 0) , can you help?

    [IOSNDKHelper SendMessage:@”ChangeLabelSelector” WithParameters:nil];

    1. Hey Barry, your welcome. Thanks to you to try my work. For the error, can you please give me some more pointers of the code? This error is a memory management error that occurs when we release a resource and call release over it. If you want i can read your code and help you with that.

      Thanks.

  3. Hi Amir, thanks for your prompt reply. I opened your “sample iOS Project” and clicked the “Tap to change text” button. there is a same issue. i am using xcode 4.6 and tried run in iPhone 5.0,6.0,6.1 simulator. had tried replace the cocos2dx version to 2.1.3, doesn’t help. do you think it is my development config problem or?

    1. Hey Barry,

      It might be possible that i haven’t copied the files into the sample folder. Please refer to the individual folder I have created. Sorry for the mishap.

  4. Hey Amir,

    Thanks for the tutorial. I’m trying to integrate this with my Android app but can seem to get Chartboost ad showing. I followed every step you’ve outlined but I’m not getting anything. You what I could be missing?

    1. Hi Mobile CAPPtivate, LLC

      Can you please tell me what is the state of project you are seeing infront of you. Like there may be different problems. Can you send me a screenshot or if you want you can download the example code from my repository on github, may be that can help? Please let me know about it, if it would be me, i will try to get rid of the problem as soon as I could :) Thanks for using my work :)

      1. Actually, I think it’s just Chartboost because the intersitials always failed to load. That’s what it shows in the logs. I’ve downloaded the sample project but I still wasn’t able to get it working.

  5. Hi thanks for tutorial, i import your project for android version, but the main “android charboost” project not showing up, only easy NDK, please tell me my mistake, so that i can follow that project.

    1. Issue resolved. Again thanks for this tutorial.
      I was getting a crash due to path you have given in NDKHelper.cpp was #define CLASS_NAME “com/easyndk/classes/AndroidNDKHelper”

      I didn’t modify that according to my package name and path.
      So it will be good if you mention that thing in your tutorial. Thanks Anyway

  6. great work Amir!! , I have been able to set the library and its working,but how to call chartboost’s delegate methods ? (which are essential part )in iOS , cb.delegate=self in AppController.mm won’t work due to data type compatibility issues. Any suggestions ?

  7. Hey Hi Amir. Thanks for nice library. I have integrated EasyNDK into my iOS project, but only error I am facing is ”iostream not found in “IOSNDKHelper_C_Interface” file. I have set up C++ standard library as “libc++” in build settings. Still unable to solve the issue. Please can you help me on this? Thanks.

Leave a reply to Amir Ali Jiwani Cancel reply