Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
The Android Game Developer???s Handbook

You're reading from  The Android Game Developer???s Handbook

Product type Book
Published in Aug 2016
Publisher Packt
ISBN-13 9781785885860
Pages 368 pages
Edition 1st Edition
Languages
Author (1):
Avisekhar Roy Avisekhar Roy
Profile icon Avisekhar Roy

Table of Contents (20) Chapters

The Android Game Developer's Handbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
1. Android Game Development 2. Introduction to Different Android Platforms 3. Different Android Development Tools 4. Android Development Style and Standards in the Industry 5. Understanding the Game Loop and Frame Rate 6. Improving Performance of 2D/3D Games 7. Working with Shaders 8. Performance and Memory Optimization 9. Testing Code and Debugging 10. Scope for Android in VR Games 11. Android Game Development Using C++ and OpenGL 12. Polishing Android Games 13. Third-Party Integration, Monetization, and Services Index

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.

How the NDK works

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.

Advantages of using C++

Let's first have a look at the positive side of using C++ for game programming through the following points:

  • Universal game programming language

  • Cross platform portability

  • Faster execution

  • CPU architecture support

Universal game programming language

In the case of...

Native code performance


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:

int[] testArray = new int[1000];
for ( int i = 0; i < 1000; ++ i)
{
  testArray[i] = i;
}

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:

int testArray[1000];
int *ptrArray = testArray;
for ( int i = 0; i < 1000; ++ i)
{
  *ptrArray = i;
  ptrArray += i * sizeof(int...

Rendering using OpenGL


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.

OpenGL versions

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 1.x

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.

OpenGL 2.0

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

  • x86

  • Neon

  • MIPS

ARM

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:

  • ARMv5TE

  • ARMv7

  • ARMv8

x86

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...

Summary


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...

lock icon The rest of the chapter is locked
You have been reading a chapter from
The Android Game Developer???s Handbook
Published in: Aug 2016 Publisher: Packt ISBN-13: 9781785885860
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}