Programming for Android in a 100% C++ Environment?
Asked Answered
P

2

11

I've been working on a game engine runtime environment in C++ for my future games, and have started to consider android as a platform. I noticed that it was tightly bound to Java and uses Java VMs heavily.

But is it possible to sustain a full C++ runtime environment in Android NDK? I have nothing against Java and am prepared to use it if I have to, but performance is one of my prime concerns (I intend for my games to be resource-intensive), especially on phones.

And if a full C++ environment is possible, how exactly would I implement it in Eclipse Indigo CDT? Would I be able to create a compiled game executable for Android ahead of time for maximum performance? And would there be any additional plugins I'd need to install in Eclipse? Could I use MinGW for compiling my games, or would I need to use a different compiler? If I had to use Java in one way or another, would compilation of the C++ code even be required? These are all questions I'd like to answer to get a sturdy development environment going in the Eclipse IDE.

Please know that I'm still fairly new to Android development, and multiplatform programming in general. My goal is to create a game engine that'll take the most advantage of the new hardware out there, especially on phones!

Thanks for any advice you guys can give!

Piggin answered 1/10, 2011 at 18:21 Comment(4)
Android has a special JIT Java compiler and a large portion of the Android environment is in Java. There isn't really a reason to prefer C++ to Java for speed on Android, much of the traditional performance impact isn't there.Buddie
Alright, that's one big thing I was wondering about, thanks. I've actually just found the NativeActivity class (developer.android.com/reference/android/app/NativeActivity.html), which looks really promising, especially since it looks like they're aiming it at C++ games. Would I still need the Android SDK and JIT compiler in order for this to work?Piggin
The JIT is part of Dalvik (Android's VM) and Dalvik is present in every process on the system whether you use NativeActivity or not - it's used to communicate with the rest of the system.Shulamite
Alright, so I'm assuming I'll need the SDK along with the NDK in order for this to work. I'll implement it in Eclipse accordingly. Thanks!Piggin
S
9

NativeActivity, added in Android 2.3 (API 9) might be what you're looking for in terms of writing Android games using only C++. http://developer.android.com/reference/android/app/NativeActivity.html

However since you say you're new to this you may want to start with one of the currently available Android game engines instead of writing your own right away. Mobile devices have a very different set of constraints than other platforms. A phone isn't very useful if the battery dies mid-afternoon, so starting a project with the stated goal of being "resource-intensive" is already getting off to a bad start. Few people will want to play your games for 20 minutes here and there if it means they won't be able to make a phone call in the evening.

If what you meant is that you are shooting for high-end graphics, keep in mind that devices have a wide range of capability in this area and targeting only the high end limits your audience. Different GPUs have very different strategies and performance characteristics and all have cases where they shine or lag behind. The most successful mobile games aren't the ones with the highest polycounts or the most complex lighting shaders, they're the games that achieve a consistently smooth framerate and have a distinctive style.

Have a look at some of the existing game engines for Android and try them out. Write a couple small games to take them for a test drive and see where they do and don't mesh with what you're trying to do. If you find yourself feeling limited with what's available, take what you've learned and try to write your own engine that fits with the types of games you want to make.

Here are some links to get you started. These engines power some very popular games on Android Market and many developers have found them useful:

http://www.andengine.org/ andengine is written in Java and is open source.

http://unity3d.com/ Unity is based on C# and Mono.

http://www.madewithmarmalade.com/ Marmalade, formerly Airplay SDK is based on C++.

Shulamite answered 1/10, 2011 at 19:1 Comment(1)
This is a very good response. And you are correct, I am aiming at high end graphics. I suppose I meant I wanted to take advantage of resources as much as possible without crippling battery life/frame rate, and I'm hoping my games will be able to adapt to the graphics capabilities of the phone (meaning adjusting poly count, shader quality, and etc). I have been studying game engine architecture recently, but you're right that I should use and analyze the existing engines out there before I decide how I'm going to make my final runtime environment. Thank you very much adamp!Piggin
S
2

The answer you want to pay close attention to is @adamp's. Ignore his answer at your own peril.

To address a couple of other points in your question, though:

Would I be able to create a compiled game executable for Android ahead of time for maximum performance?

No, insofar as Android does not use "compiled game executables". Your C/C++ code will be compiled into a Linux shared object (.so) file and packaged with other stuff (like your manifest) in an APK file. Compiling the .so will be ahead of time, though.

Could I use MinGW for compiling my games would I need to use a different compiler?

I get the distinct impression that you will claw your eyes out trying to do NDK development in Cygwin on Windows. I heartily encourage you to do your NDK development on OS X or Linux. At least set up a VirtualBox with Ubuntu or something in it to do your NDK compiles.

Regardless, the NDK comes with its own GCC compiler. AFAIK, MinGW is not a cross-compiler, and most Android devices are not running on x86 CPUs.

If I had to use Java in one way or another, would compilation of the C++ code even be required?

Yes. Now, for Honeycomb, you have the option of using Renderscript for OpenGL work -- that amounts to a C99 environment whose code you will not need to compile with the NDK.

Stilu answered 1/10, 2011 at 19:25 Comment(1)
Alright, I'll get VirtualBox set up with Ubuntu at some point if I need to. I wish I had a good OS X dev machine that is under $600, as I really want to develop on iOS as well. I suppose that can wait for now though.Piggin

© 2022 - 2024 — McMap. All rights reserved.