Book Image

Android NDK Game Development Cookbook

Book Image

Android NDK Game Development Cookbook

Overview of this book

Android NDK is used for multimedia applications which require direct access to a system's resources. Android NDK is also the key for portability, which in turn provides a reasonably comfortable development and debugging process using familiar tools such as GCC and Clang toolchains. If your wish to build Android games using this amazing framework, then this book is a must-have.This book provides you with a number of clear step-by-step recipes which will help you to start developing mobile games with Android NDK and boost your productivity debugging them on your computer. This book will also provide you with new ways of working as well as some useful tips and tricks that will demonstrably increase your development speed and efficiency.This book will take you through a number of easy-to-follow recipes that will help you to take advantage of the Android NDK as well as some popular C++ libraries. It presents Android application development in C++ and shows you how to create a complete gaming application. You will learn how to write portable multithreaded C++ code, use HTTP networking, play audio files, use OpenGL ES, to render high-quality text, and how to recognize user gestures on multi-touch devices. If you want to leverage your C++ skills in mobile development and add performance to your Android applications, then this is the book for you.
Table of Contents (16 chapters)
Android NDK Game Development Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Mobility and the demand for high-performance computations are often very tightly coupled. Current mobile applications do many computationally-intense operations such as 3D and stereoscopic rendering, images and audio recognition, and video decoding and encoding, especially the birth of new technologies such as the augmented reality. This include mobile games, 3D user interface software, and social software, which involves media stream processing.

In some sense, mobile game development forces us to travel back in time several years due to the limited hardware capabilities, low memory bandwidth, and precious battery resources, but also makes us reconsider the basic forms of interaction with the user.

A smooth and responsive user interface based on gesture input, Internet access, ambient sound effects, high-quality text, and graphics are the ingredients of a successful mobile application.

All major mobile operating systems give software developers different possibilities to develop close-to-the-hardware. Google provides an Android Native Development Kit (NDK) to ease the porting of existing applications and libraries from other platforms to Android, and exploit the performance of the underlying hardware offered by the modern mobile devices. C, and especially C++, both have a reputation of being a hard language to learn, and a hard language to write user interface code in. This is indeed true, but only when someone attempts to write everything from scratch. In this book we use C and C++ programming languages, and link them to well-established third-party libraries to allow the creation of content-rich applications with a modern touch-based interface and access to the Representational State Transfer (REST) APIs of popular sites such as Facebook, Twitter, Flickr, Picasa, Instagram, and a myriad of others.

Despite the availability of the information on how to use Internet resources in the applications written in Java or .NET languages, not much has been said about doing this in C++ programming language. Modern OpenGL versions require a sufficient amount of effort to create and use the latest extensions. The programming using the OpenGL API is usually described in literature in a platform-specific way. Things get even more complicated with the mobile version, the OpenGL ES, as developers have to adapt existing shader programs to allow them to run on the mobile graphics processing units (GPUs). Sound playback using standard Android facilities in C++ is also not straightforward, for example, things should be done to re-use the existing PC code for the OpenAL library. This book tries to shed some light on these topics and combine a number of useful recipes to simplify the multiplatform-friendly development with Android NDK.

Android is a mobile operating system based on the Linux kernel and designed for smartphones, tablet computers, netbooks, and other portable devices. Initial development of Android was started by Android Inc, which was bought by Google in 2005. In November 2007, the first version was unveiled, however, the first commercially available Android-based smartphone, HTC Dream, was released almost one year later in 2008.

Android versions, besides a numerical denomination, have official code names—each major release is named after a sweet dessert. The following are some significant milestones in Android platform technologies and features related to NDK:

  • Version 1.5 (Cupcake): This Android version featured the first release of Android Native Development Kit supporting ARMv5TE instructions.

  • Version 1.6 (Donut): First introduction of OpenGL ES 1.1 native library support.

  • Version 2.0 (Eclair): OpenGL ES 2.0 native library support.

  • Version 2.3 (Gingerbread):

    • Concurrent garbage collector in Dalvik VM. This has faster gaming performance and improved efficiency of OpenGL ES operations.

    • Capabilities of Native Development Kit are greatly extended, including sensors access, native audio OpenSL ES, the EGL library, activity life cycle management, and native access to assets.

  • Version 3.0 (Honeycomb):

    • Support for tablet computers with large touch screens

    • Support of multicore processors

  • Version 4.0 (Ice Cream Sandwich):

    • Unified UI for smartphones and tablet

    • Hardware-accelerated 2D rendering. VPN client API

  • Versions 4.1 and 4.2 (Jelly Bean):

    • This has improved rendering performance and triple buffering

    • External display support, including external displays over Wi-Fi

    • They have high-dynamic range camera support

    • New built-in developer options for debugging and profiling. Dalvik VM runtime optimizations

  • Version 4.3 (Jelly Bean): OpenGL ES 3.0 native library support.

  • Version 4.4 (KitKat): Introduced access to RenderScript from NDK. This feature is backwards compatible with any device running Android 2.2 or higher.

Android Native Development Kit (NDK) is used for multimedia applications that require performance that Dalvik is unable to provide, and direct access to the native system libraries. NDK is also the key for portability, which in turn allows a reasonably comfortable development and debugging process using familiar tools such as GCC and Clang toolchains or alike. The typical usage of NDK determines the scope of this book—integration of some of the most commonly used C/C++ libraries for graphics, sound, networking, and resource storage.

Initially, NDK was based on the Bionic library. It is a derivation of the BSD standard C library (libc) developed by Google for Android. The main goals of Bionic were as follows:

  • License: Original GNU C Library (glibc) is GPL-licensed and Bionic has a BSD license.

  • Size: Bionic is much smaller in size compared to GNU C Library.

  • Speed: Bionic is designed for mobile CPUs with relatively low clock frequencies. For example, it has a custom implementation of pthreads.

Bionic lacks many important features found in full libc implementations, such as RTTI and C++ exceptions handling support. However, NDK provides several libraries with different C++ helper runtimes which implement these features. These are GAbi++ runtime, STLport runtime, and GNU Standard C++ Library. Besides the basic POSIX features, Bionic has support for Android-specific mechanisms such as logging.

The NDK is a very effective way to reuse a great body of existing C and C++ code.

What this book covers

Chapter 1, Establishing a Build Environment, explains how to install and configure Android SDK and NDK on Microsoft Windows and Ubuntu/Debian Linux flavors, and how to build and run your first application on an Android-based device. You will learn how to use different compilers and toolchains that come with the Android NDK. Debugging and deploying the application using the adb tool is also covered.

Chapter 2, Porting Common Libraries, contains a set of recipes to port well-established C++ projects and APIs to Android NDK, such as FreeType fonts rendering library, FreeImage images loading library, libcurl and OpenSSL (including compilation of libssl and libcrypto), OpenAL API, libmodplug audio library, Box2D physics library, Open Dynamics Engine (ODE), libogg, and libvorbis. Some of them require changes to the source code, which will be explained. Most of these libraries are used later in subsequent chapters.

Chapter 3, Networking, shows you how to use the well-known libcurl library to download files using the HTTP protocol and how to form requests and parse responses from popular Picasa and Flickr online services directly using C++ code. Most applications nowadays use network data transfer in one way or another. HTTP protocol is the foundation of the APIs for all of the popular websites such as Facebook, Twitter, Picasa, Flickr, SoundCloud, and YouTube. The remaining part of the chapter is dedicated to the web server development. Having a mini web server in the application allows a developer to control the software remotely and monitor its runtime without using the OS-specific code. The beginning of the chapter also introduces a task queue for background download processing and simple smartpointers to allow efficient cross-thread data interchange. These threading primitives are used later on in Chapter 4, Organizing a Virtual Filesystem and Chapter 5, Cross-platform Audio Streaming.

Chapter 4, Organizing a Virtual Filesystem, is devoted entirely to the asynchronous file handling, resource proxies, and resource compression. Many programs store their data as a set of files. Loading these files without blocking the whole program is an important issue. Human interface guidelines for all modern operating systems prescript the application developer to avoid any delay, or "freezing", in the program's workflow (known as the Application Not Responding (ANR) error in Android). Android program packages are simply archive files with an .apk extension, compressed with a familiar ZIP algorithm. To allow reading the application's resource files directly from .apk, we have to decompress the .zip format using the zlib library. Another important topic covered is the virtual filesystem concept, which allows us to abstract the underlying OS files and folders structure, and share resources between Android and PC versions of our application.

Chapter 5, Cross-platform Audio Streaming, starts with organizing an audio stream using the OpenAL library. After this, we proceed to the RIFF WAVE file format reading, and OGG Vorbis streams decoding. Finally, we learn how to play some tracker music using libmodplug. Recent Android NDK includes an OpenSL ES API implementation. However, we are looking for a fully portable implementation between the desktop PC and other mobile platforms to allow seamless game debugging capabilities. To do this, we precompile an OpenAL implementation into a static library, and then organize a small multithreaded sound streaming library on top of libogg and libvorbis.

Chapter 6, Unifying OpenGL ES 3 and OpenGL 3, presents the basic rendering loop for the desktop OpenGL 3 and mobile OpenGL ES 3.0. Redeploying the application to a mobile device is a lengthy operation that prevents the developer from quick feature testing and debugging. In order to allow the development and debugging of game logic on the PC, we provide a technique to use desktop GLSL shaders in mobile OpenGL ES.

Chapter 7, Cross-platform UI and Input System, will teach you how to implement multi-touch event handling and gesture recognition in a portable way. A mobile is now almost synonymous with gesture-based touch input. No modern user-oriented application can exist without a graphical user interface (GUI). There are two basic issues to organize the interaction: input and text rendering. To ease the testing and debugging, we also show you how to simulate the multi-touch input on a Windows 7 PC equipped with multiple mouse devices. Since we are aiming at the development of interactive gaming applications, we have to implement user input in a familiar way. We will show you systematically how to create an on-screen gamepad UI. In a global multicultural environment, it is very desirable to have a multi-language text renderer for any application. We will show you how to use the FreeType library to render Latin, Cyrillic, and left-to-right texts. The organization of a multi-language UTF-8 localized interface will be presented as a dictionary-based approach.

Chapter 8, Writing a Match-3 Game, will put all the techniques we have introduced together, and write a simple Match-3 game, including rendering using OpenGL ES, input handling, resources packing, and PC-side debugging. The game is also runnable and debuggable on a Windows desktop PC and can be easily ported to other mobile platforms.

Chapter 9, Writing a Picture Puzzle Game, will provide a more complicated example, integrating all of the things mentioned above. All of the above elements regarding graphics and input will use native network libraries and APIs to download images from the Picasa online service.

What you need for this book

This book is centered on a Windows-based PC. An Android smartphone or tablet is advisable due to the limitations of the emulator in 3D graphics and native audio.

Note

The source code in this book is based on open-source Linderdaum Engine and is a hard squeezing of some approaches and techniques used in the engine. You can get at http://www.linderdaum.com.

Basic knowledge of C or C++, including pointer manipulation, multithreading, and basic object-oriented programming concepts is assumed. The reader should be familiar with advanced programming concepts such as threading and synchronization primitives, and have some basic understanding of GCC toolchains. We also hope the reader is not afraid to develop without an IDE (yes, developing without an autocomplete-capable IDE definitely IS a skill) from a terminal/FarManager/Notepad/SublimeText, for example.

Android Java development is not covered in this book. You will have to read something else to get familiar with it.

Some working knowledge of linear algebra and affine transformations in 3D space is useful for the understanding of OpenGL programming and gesture recognition.

Who is this book for

Do you want to port your existing C/C++ application to Android? Are you an experienced C++ developer who wants to jump into a modern mobile development? Do you want to increase the performance of your Java-based Android application? Do you want to use great libraries written in C++ in your Android application? Do you want to boost your productivity by debugging your mobile games on a PC?

If you say yes to any of these questions, then this book is for you.

Building the source code

The examples from the code bundle of this book can be compiled using the following commands:

  • For Windows: make all

  • For Android: ndk-buildant copy-common-media debug

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are show as follows: "JAVA_HOME variable should point to the Java Development Kit folder."

A block of code is typeset as follows:

package com.packtpub.ndkcookbook.app1;
import android.app.Activity;
public class App1Activity extends Activity
{
};

When we wish to draw your attention to a particular line of code, the relevant lines are emphasized like so:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">App1</string>
</resources>

All command-line input or output is written as follows:

>adb.exe logcat -v time > 1.txt

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "install this device software or not, you should click on the Install button".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code for this book

You can download the example source code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you. We worked hard to write and debug the source code for this book. The truth is, in real life there are always bugs lurking in the code, which need to be fixed after the release.

We established a GitHub repository, so everyone can download the most recent source code bundle, and open pull requests to submit bugfixes and improvements. The repository can be cloned from: https://github.com/corporateshark/Android-NDK-Game-Development-Cookbook. The latest snapshot of our source code bundle is available at: http://www.linderdaum.com/Android-NDK-Game-Development-Cookbook-SourceCodeBungle.zip.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.