{"id":915,"date":"2011-11-12T17:30:25","date_gmt":"2011-11-12T16:30:25","guid":{"rendered":"http:\/\/www.frozax.com\/blog\/?p=915"},"modified":"2011-11-12T17:30:25","modified_gmt":"2011-11-12T16:30:25","slug":"android-game-development-java-c-cocos2d-x","status":"publish","type":"post","link":"https:\/\/www.frozax.com\/blog\/2011\/11\/android-game-development-java-c-cocos2d-x\/","title":{"rendered":"Android Game Development : from Java to C++ (with cocos2d-x)"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>In a previous post (<a href=\"http:\/\/www.frozax.com\/blog\/2011\/09\/android-game-development-libgdx\/\">here<\/a>), 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&#8217;s so big that it made me stop using libgdx : <strong>It is not portable on iOS<\/strong>. I knew this from the beginning but didn&#8217;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.<\/p>\n<h3>Cocos2d-x<\/h3>\n<p>I searched for multiplatform libraries (Android and iOS). As the game I&#8217;m porting (<a href=\"http:\/\/www.frozax.com\/games\/dont-feed-the-trolls\">Don&#8217;t Feed the Trolls<\/a>) was developed with XNA, I looked at C# solutions. There is at least one but it&#8217;s too expensive (I previously linked to it but I was asked to remove the link by the company, which I did). I&#8217;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 <a href=\"http:\/\/www.madewithmarmalade.com\/marmalade\">Marmalade<\/a>). I finally decided to use <a href=\"http:\/\/www.cocos2d-x.org\/\">cocos2d-x<\/a>. This is a C++ port of the famous <a href=\"http:\/\/www.cocos2d-iphone.org\/\">cocos2d library for iPhone<\/a> which is in Objective C. Cocos2d-x has an active community, some multiplatform games have been released already, it&#8217;s free and open source. All good stuff!<\/p>\n<h3>C++ on Android<\/h3>\n<p>I am mostly working using the Windows build of the library but I also did some work on Android. Mixing C++ and Java (using <a href=\"http:\/\/developer.android.com\/guide\/practices\/design\/jni.html\">JNI : Java Native Interface<\/a>) 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&#8217;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&#8217;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 (<a href=\"http:\/\/stackoverflow.com\/questions\/5314036\/how-to-use-addr2line-in-android\">here&#8217;s how to use it<\/a>) that allows you to get the callstack. It&#8217;s usually enough to understand and fix the bug. You also can still use old-school debugging techniques such as logging to the output \ud83d\ude09 Hopefully, most of my bugs are multiplatform, or in Java code, so I can fix them easily.<\/p>\n<h3>Java is still needed<\/h3>\n<p>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.<\/p>\n<h3>Conclusion<\/h3>\n<p>Even though libgdx is really a great lib, after a few weeks working on C++, I&#8217;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.<\/p>\n<p>Which language do you use to build your multiplatform mobile games? Do you know other good C++ solutions? Feel free to comment!<\/p>\n<p>Follow me on <a href=\"http:\/\/twitter.com\/Frozax\">twitter<\/a>, <a href=\"http:\/\/www.facebook.com\/frozax\">facebook<\/a> or <a href=\"https:\/\/plus.google.com\/117773429877335932919\">google+<\/a> for more game development information.<\/p>\n<div class=\"addtoany_share_save_container addtoany_content_bottom\"><div class=\"a2a_kit a2a_kit_size_32 addtoany_list a2a_target\" id=\"wpa2a_1\"><a class=\"a2a_button_twitter\" href=\"http:\/\/www.addtoany.com\/add_to\/twitter?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2011%2F11%2Fandroid-game-development-java-c-cocos2d-x%2F&amp;linkname=Android%20Game%20Development%20%3A%20from%20Java%20to%20C%2B%2B%20%28with%20cocos2d-x%29\" title=\"Twitter\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_facebook\" href=\"http:\/\/www.addtoany.com\/add_to\/facebook?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2011%2F11%2Fandroid-game-development-java-c-cocos2d-x%2F&amp;linkname=Android%20Game%20Development%20%3A%20from%20Java%20to%20C%2B%2B%20%28with%20cocos2d-x%29\" title=\"Facebook\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_google_plus\" href=\"http:\/\/www.addtoany.com\/add_to\/google_plus?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2011%2F11%2Fandroid-game-development-java-c-cocos2d-x%2F&amp;linkname=Android%20Game%20Development%20%3A%20from%20Java%20to%20C%2B%2B%20%28with%20cocos2d-x%29\" title=\"Google+\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_reddit\" href=\"http:\/\/www.addtoany.com\/add_to\/reddit?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2011%2F11%2Fandroid-game-development-java-c-cocos2d-x%2F&amp;linkname=Android%20Game%20Development%20%3A%20from%20Java%20to%20C%2B%2B%20%28with%20cocos2d-x%29\" title=\"Reddit\" rel=\"nofollow\" target=\"_blank\"><\/a>\n<script type=\"text\/javascript\"><!--\nwpa2a.script_load();\n\/\/--><\/script>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Introduction 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&#8217;s so big that it made me stop using libgdx : It is not&hellip; <a class=\"more-link\" href=\"https:\/\/www.frozax.com\/blog\/2011\/11\/android-game-development-java-c-cocos2d-x\/\">Continue reading <span class=\"screen-reader-text\">Android Game Development : from Java to C++ (with cocos2d-x)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,3,12],"tags":[55,61,65,87,88,90],"_links":{"self":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/915"}],"collection":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/comments?post=915"}],"version-history":[{"count":0,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/915\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/media?parent=915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/categories?post=915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/tags?post=915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}