Book Image

Mastering Android NDK

Book Image

Mastering Android NDK

Overview of this book

Android NDK is used for multimedia applications that require direct access to system resources. 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. This is a hands-on guide to extending your game development skills with Android NDK. The book takes you through many clear, step-by-step example applications to help you further explore the features of Android NDK and some popular C++ libraries and boost your productivity by debugging the development process. Through the course of this book, you will learn how to write portable multi-threaded native code, use HTTP networking in C++, play audio files, use OpenGL ES 3, and render high-quality text. Each chapter aims to take you one step closer to building your application. By the end of this book, you will be able to create an engaging, complete gaming application.
Table of Contents (17 chapters)
Mastering Android NDK
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

This book is a sequel to Android NDK Game Development Cookbook, Packt Publishing, published in 2013. It covers NDK development from quite an unusual point of view: building your mobile C++ applications in a portable way so that they can be developed and debugged on a desktop computer. This approach greatly reduces iteration and content integration time and is essential in the world of professional mobile software development.

What this book covers

Chapter 1, Using Command-line Tools, shows you how to install and configure the essential tools for Android native development using the command line and how to write basic Android application configuration files manually from scratch without falling back on the graphical IDEs.

Chapter 2, Native Libraries, shows you how to build popular C/C++ libraries and link them against your applications using the Android NDK. These libraries are the building blocks to implement feature-rich applications with images, videos, sounds, and networking entirely in C++. We will show you how to compile libraries and, of course, give some examples and hints on how to start using them. Some of the libraries are discussed in greater detail in the subsequent chapters.

Chapter 3, Networking, focuses on how to deal with network-related functionality from the native C/C++ code. Networking tasks are asynchronous by nature and unpredictable in terms of timing. Even when the underlying connection is established using the TCP protocol, there is no guarantee on the delivery time, and nothing prevents the applications from freezing while waiting for the data. We will take a closer look at implementing basic asynchronous mechanisms in a portable way.

Chapter 4, Organizing a Virtual Filesystem, implements low-level abstractions to deal with the OS-independent access to files and filesystems. We will show how to implement portable and transparent access to the Android assets packed inside the .apk files without falling back on any built-in APIs. This approach is necessary when building multiplatform applications that are debuggable in a desktop environment.

Chapter 5, Cross-platform Audio Streaming, implements a truly portable audio subsystem for Android and desktop PCs based on the OpenAL library. The code makes use of the multithreading material from Chapter 3, Networking.

Chapter 6, OpenGL ES 3.1 and Cross-platform Rendering, focuses on how to implement an abstraction layer on top of OpenGL 4 and OpenGL ES 3 to make our C++ graphics applications runnable on Android and desktop machines.

Chapter 7, Cross-platform UI and Input System, details the description of a mechanism to render geometric primitives and Unicode text. The second part of the chapter describes a multi-page graphical user interface suitable for being the cornerstone for building the interfaces of multiplatform applications. This chapter concludes with an SDL application, which demonstrates the capabilities of our UI system in action.

Chapter 8, Writing a Rendering Engine, will move you into the actual rendering territory and use the thin abstraction layer, which is discussed in Chapter 6, OpenGL ES 3.1 and Cross-platform Rendering, to implement a 3D rendering framework capable of rendering geometry loaded from files using materials, lights, and shadows.

Chapter 9, Implementing Game Logic, introduces a common approach to organize interactions between gaming code and the user interface part of the application. The chapter begins with an implementation of the Boids algorithm and then proceeds with the extension of our user interface implemented in the previous chapters.

Chapter 10, Writing Asteroids Game, continues to put together the material from previous chapters. We will implement an Asteroids game with 3D graphics, shadows, particles, and sounds using techniques and code fragments introduced in the previous chapters.

What you need for this book

This book assumes that you have 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 it at http://www.linderdaum.com.

Basic knowledge of C or C++, including pointer manipulation, multithreading, and basic object-oriented programming concepts is assumed. You should be familiar with advanced programming concepts such as threading and synchronization primitives, and have some basic understanding of GCC toolchains. 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 3D graphics-related chapters.

Who this book is for

This book is intended for existing Android developers who are familiar with the fundamentals of the Android NDK and wish to gain expertise in game development using the Android NDK. Readers must have reasonable experience of Android application development.

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, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Compilation of an Android static library requires a usual set of Android.mk and Application.mk files."

A block of code is set as follows:

std::string ExtractExtension( const std::string& FileName )
{
  size_t pos = FileName.find_last_of( '.' );
  return ( pos == std::string::npos ) ?
    FileName : FileName.substr( pos );
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

std::string ExtractExtension( const std::string& FileName )
{
  size_t pos = FileName.find_last_of( '.' );
  return ( pos == std::string::npos ) ?
    FileName : FileName.substr( pos );
}

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

>ndk-build
>ant debug
>adb install -r bin/App1-debug.apk

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: "Check the line Hello Android NDK! printed into the Android system log."

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

You can download the example 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. The source code is also available as a GitHub repository from this URL https://github.com/corporateshark/Mastering-Android-NDK. Check it to get the most up-to-date version of the sources.

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/submit-errata, 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.