Chapter 11. Android Game Development Using C++ and OpenGL
We have already seen the differences between an Android application and an Android game. The Android SDK is very capable of taking care of both. Certainly, the question that arises is "What is the requirement of a separate development toolset in native languages such as C and C++?" Compared to Java, C and C++ are much more difficult to manage and write code in. The answer lies in the question itself. The Java architecture runs on JVM, which is associated with the Android operating system. This creates an extra latency, which has a performance lag. The scale of the lag depends on the scale of the application.
A highly CPU-intensive application may cause a significant amount of visible lag in Java architecture. Native language code can be processed faster. Moreover, native code can be varied depending on the CPU/platform architecture, which is not possible in the case of the Java architecture used by the Android SDK.
Android uses the...
Introduction to the Android NDK
Android is actually based on the Java architecture. However, part of an Android application can be developed using native languages such as C and C++. This is where the Android NDK comes into the picture.
The Android NDK is a toolset used to develop a module of an application that will interact with hardware much faster. It is a well-known fact that C and C++ have the ability to interact with a native component directly, which reduces the latency between the application and hardware.
An Android native code segment interacts with main application through the Java Native Interface (JNI). The Android NDK comes with a build script that converts native code into binary and includes it in the main Android application.
This binary is basically a native code library that can be used in any Android application as per requirement. An NDK build script creates .so
files and adds them to the application path.
The Android build process creates Dalvik Executable...
C++ for games – pros and cons
There is a never-ending debate between C++ and Java. However, we will not go into the controversy and will try to look at them from the perspective of game development. C++ has a slight performance edge over Java, and Java is known for its simplicity.
There may be many programmers who are more comfortable in C++ than Java, or vice versa. In game development, personal choice of programming language does not matter. Hence, using NDK or SDK has to be determined depending on the requirements. It is always recommended that you use the Android SDK to develop an application rather than using the NDK.
Let's discuss the advantages and disadvantages of using native language for game programming.
Let's first have a look at the positive side of using C++ for game programming through the following points:
Universal game programming language
In the case of...
As we already know, native code can run faster with better processing speed. This can be further optimized for a specific CPU architecture. The main reason behind this performance boost is the use of pointers in memory operations. However, it depends on the developer and the coding style.
Let's look at a simple example where we can have a better understanding of performance gain in native language.
Consider this Java code:
In this case, the address of 1000 fields in the array is handled by JVM (DVM in the case of an Android Dalvik system). So, the interpreter parses to the ith
position and performs an assignment operation each time, which takes a lot of time.
Now, let's implement the same functionality using native C/C++ language and use pointers:
Android uses OpenGL for rendering. Android SDK libraries include the OpenGL libraries, specially optimized for Android. Android started supporting OpenGL from API level 4 and then increased its support as the level increased. Currently, the maximum supported version of OpenGL is OpenGL ES 3.1 from API level 21.
Different OpenGL versions have a different set of features. Versions 1.0 and 2.0 have a lot of differences in terms of coding style, API convenience, functionality, and feature support. Let's discuss the following OpenGL ES versions that are significant to Android development:
OpenGL ES 1.x
OpenGL ES 2.0
OpenGL ES 3.0
OpenGL ES 3.1
OpenGL version 1.x has been supported from Android API level 4 with a shared OpenGL ES 1.x library, libGLESv1.so
. The headers gl.h
and glext.h
contain all the necessary APIs for OpenGL functionality.
In the current industry, a developer prefers to use OpenGL ES 2.0 for games, because almost every device...
Different CPU architecture support
The developer has the opportunity to optimize an Android application for a separate processor architecture. At a high-level point of view, it is a great feature. However, this feature comes at a significant cost. Let's have a look at the details of this feature.
Available CPU architectures
Here are the architectures currently supported by the NDK build:
ARM stands for Acorn RISC Machine. This is a RISC (Reduced Instruction Set Computing) based processor, mainly targeting embedded or mobile computing. As the base says, it is highly efficient for an operating system such as Android.
Currently, most used processors of the Android platform are from the ARM family. It can be further sub-categorized as follows:
Intel introduced the x86 architecture for processors. At first, these processors were mainly used for desktop/laptop PCs. However, they were optimized to be used in mobile devices in the form of Celeron or Atom processors...
We looked at Android NDK briefly in this chapter and cleared a few doubts on native development. There are many developers who think that developing games in a native language gives enormous processing power. This is, however, not always true. Processing and performance depend on the development style and standard. In most common scenarios, the difference between native development and SDK development is negligible.
OpenGL works with Android in any scenario. The backend rendering is based on OpenGL for both NDK and SDK. We have already discussed all the technical aspects of OpenGL. Here, you learned which version of OpenGL works with Android and what we should use. Clearly, OpenGL ES 2.0 is a good choice as most Android devices support it. On the other hand, OpenGL ES 1.0 is obsolete, and OpenGL ES 3.0 is not supported by most Android devices yet.
Until now, we have covered almost every aspect of Android game development. However, finishing the implementation for the game does not...