In a previous post (here), I was talking about my progress with libgdx, a Java library for Android (and other platforms). Shortly after writing this article, I realized that there is a big disadvantage that I forgot to talk about. It’s so big that it made me stop using libgdx : It is not portable on iOS. I knew this from the beginning but didn’t really think about using anything else than Java. I finally realized that while porting from one language to another is easy, it can take a lot of time.
I searched for multiplatform libraries (Android and iOS). As the game I’m porting (Don’t Feed the Trolls) was developed with XNA, I looked at C# solutions. There is at least one but it’s too expensive (I previously linked to it but I was asked to remove the link by the company, which I did). I’ve been doing C++ indie/casual games in the past and I develop in C++ in my day job, so this is the obvious choice. I like the efficiency of languages such as C# or Java (easy to write and debug), but if I want to avoid porting the game another time for the iPhone, I should to use C++. There are quite some libraries in C++ (such as Marmalade). I finally decided to use cocos2d-x. This is a C++ port of the famous cocos2d library for iPhone which is in Objective C. Cocos2d-x has an active community, some multiplatform games have been released already, it’s free and open source. All good stuff!
C++ on Android
I am mostly working using the Windows build of the library but I also did some work on Android. Mixing C++ and Java (using JNI : Java Native Interface) is doable, but really not user friendly. Debugging is terrible, building is slow, you have to install tools such as cygwin, use makefiles (how old school!), and so on. It really does not seem the right solution at first. However, once everything is set up correctly, it’s working fine. Cocos2d-x has good tutorials about this. I develop with Visual C++ 2010 Express on the Windows build, I have all the great tools to debug generic problems. The only real troubles are when you have android-specific bugs in the C++ code but it’s really not frequent. And there are a few ways to debug it with gdb and gdb-server (I never had to use it yet). When you have a crash, there is a very useful tool in the NDK called addr2line (here’s how to use it) that allows you to get the callstack. It’s usually enough to understand and fix the bug. You also can still use old-school debugging techniques such as logging to the output 😉 Hopefully, most of my bugs are multiplatform, or in Java code, so I can fix them easily.
Java is still needed
To develop Android-specific code, you still need Java. For example, I use Java for AdMob integration, In-App Purchases or the Facebook SDK for instance. Setting up Java functions called from C++ is not very intuitive, so I opted for a very simple and centralized way: I have one Java function that can be called from C++ with two String parameters. In the Java code, I read these parameters that contains all I need to know to call the specific Java code.
Even though libgdx is really a great lib, after a few weeks working on C++, I’m confident that I made the right choice with cocos2d-x. I did not start developing on iPhone already but it should be quite fast compared to porting Java to C++ (or worse, Objective-C!). I have a working Android version of my game and the performances seems good.
Which language do you use to build your multiplatform mobile games? Do you know other good C++ solutions? Feel free to comment!