Game Development Reference
In-Depth Information
Chapter 13
Going Native with the NDK
After all the 3D extravagance in the past three chapters, it's time to look into one more aspect of
programming games for Android. While Java and the Dalvik VM are sufficient for a lot of game
genres in terms of execution speed, there are times when you need a bit more power. This is
especially true for physics simulations, complex 3D animation, collision detection, and so on.
This type of code is best written in more “to-the-metal� languages like C/C++ or even assembly
language. The Android native development kit (NDK) lets us do exactly that.
Implementing 3D animation or a physics engine in C/C++ is way outside the scope of this
book. However, in Chapter 8 we identified a bottleneck that can be fixed with a bit of native
code. Copying a float array to a ByteBuffer is terribly slow on Android. Some of our OpenGL
ES classes rely on this mechanism. In this chapter we'll look into fixing this with a bit of
C/C++ code!
Note The following sections show you how to interface with C/C++ code from your Java
application. If you don't feel confident jumping into this topic, just skip this chapter and return to it
if you want to know more.
What Is the Android NDK?
The NDK is an addition to the Android SDK that lets you write C/C++ and assembly code that
you can then integrate into your Android application. The NDK consists of a set of Android-
specific C libraries, a cross-compiler toolchain based on the GNU Compiler Collection (GCC)
that compiles to all the different CPU architectures supported by Android (ARM, x86, and MIPS),
and a custom build system that should make compiling C/C++ code easier when compared to
writing your own makefiles.
In the early versions of the NDK, debugging native code with Eclipse's debugger was not an
officially supported feature. Now there are some official new tools in the works to make it easier
to do native debugging. At the time of writing, this functionality is brand new and untested, but
633
 
 
Search WWH ::




Custom Search