Android NDK Game Development Cookbook
By Kosarevsky Sergey and Latypov Viktor
()
About this ebook
Related to Android NDK Game Development Cookbook
Related ebooks
Android Native Development Kit Cookbook Rating: 0 out of 5 stars0 ratingsFlash Development for Android Cookbook Rating: 3 out of 5 stars3/5Qt5 C++ GUI Programming Cookbook Rating: 0 out of 5 stars0 ratingsOpenGL Development Cookbook Rating: 5 out of 5 stars5/5D Cookbook Rating: 0 out of 5 stars0 ratingsSwift Cookbook Rating: 5 out of 5 stars5/5Multithreading in C# 5.0 Cookbook Rating: 0 out of 5 stars0 ratingsAndroid Studio Cookbook Rating: 4 out of 5 stars4/5Direct3D Rendering Cookbook Rating: 0 out of 5 stars0 ratingsRedmine Cookbook Rating: 0 out of 5 stars0 ratingsBoost.Asio C++ Network Programming Cookbook Rating: 0 out of 5 stars0 ratingsUnity 5.x Shaders and Effects Cookbook Rating: 0 out of 5 stars0 ratingsWindows Application Development Cookbook Rating: 0 out of 5 stars0 ratingsLearning Embedded Android N Programming Rating: 0 out of 5 stars0 ratingsHaxe Game Development Essentials Rating: 0 out of 5 stars0 ratingsSwift 2 Design Patterns Rating: 0 out of 5 stars0 ratingsGame Programming Using Qt: Beginner's Guide Rating: 0 out of 5 stars0 ratingsMastering Android NDK Rating: 0 out of 5 stars0 ratingsGradle for Android Rating: 0 out of 5 stars0 ratingsAndroid System Programming Rating: 5 out of 5 stars5/5Instant MinGW Starter Rating: 0 out of 5 stars0 ratingsRaspberry Pi IoT Projects: Prototyping Experiments for Makers Rating: 0 out of 5 stars0 ratingsAndroid NDK: Beginner's Guide - Second Edition Rating: 0 out of 5 stars0 ratingsiOS 8 App Development Essentials Rating: 0 out of 5 stars0 ratingsPHP Programming Solutions Rating: 0 out of 5 stars0 ratingsLearning Vulkan Rating: 0 out of 5 stars0 ratingsInstant Java Password and Authentication Security Rating: 0 out of 5 stars0 ratingsEmbedded Linux Development Using Eclipse Rating: 5 out of 5 stars5/5Application Development with Qt Creator - Second Edition Rating: 4 out of 5 stars4/5Build 6 Games In Unreal Rating: 0 out of 5 stars0 ratings
Programming For You
Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5101 Amazing Nintendo NES Facts: Includes facts about the Famicom Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Teach Yourself C++ Rating: 4 out of 5 stars4/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratings
Reviews for Android NDK Game Development Cookbook
0 ratings0 reviews
Book preview
Android NDK Game Development Cookbook - Kosarevsky Sergey
Table of Contents
Android NDK Game Development Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who is this book for
Building the source code
Conventions
Reader feedback
Customer support
Downloading the example code for this book
Errata
Piracy
Questions
1. Establishing a Build Environment
Introduction
Installing Android development tools on Windows
Getting ready
How to do it...
There's more...
Installing Android development tools on Linux
Getting ready
How to do it...
There's more...
Creating an application template manually
Getting ready
How to do it...
How it works...
There's more...
See also…
Adding native C++ code to your application
Getting ready
How to do it...
How it works...
There's more...
Switching NDK toolchains
Getting ready
How to do it...
There's more...
Supporting multiple CPU architectures
Getting ready
How to do it...
There's more...
Basic rendering with OpenGL ES
Getting ready
How to do it…
How it works…
Going cross platform
Getting ready
How to do it...
How it works…
There's more…
See also
Unifying the cross-platform code
Getting ready
How to do it...
There's more...
Linking and source code organization
Getting ready
How to do it...
How it works...
Signing release Android applications
Getting ready
How to do it...
See also
2. Porting Common Libraries
Introduction
Compiling the native static libraries for Windows
Getting ready
How to do it...
How it works...
There’s more...
Compiling the native static libraries for Android
Getting ready
How to do it...
How it works...
Compiling the libcurl networking library
Getting ready
How to do it...
How it works…
There’s more…
See also
Compiling the OpenAL library
Getting ready
How to do it...
How it works…
See also
Compiling libvorbis, libmodplug, and libtheora
Getting ready
How to do it...
Using the FreeImage graphics library
Getting ready
How to do it...
How it works...
There’s more…
See also
Using the FreeType library for text rendering
Getting ready
How to do it...
How it works…
There’s more...
Implementing timing in physics
Getting ready
How to do it...
How it works...
See also…
Rendering graphics in 2D
Getting ready
How to do it…
How it works…
See also …
Setting up Box2D simulations
Getting ready
How to do it...
How it works...
There’s more...
See also
Building the ODE physical library
Getting ready
How to do it...
How it works...
There’s more...
See also
3. Networking
Introduction
Fetching list of photos from Flickr and Picasa
Getting ready
How to do it…
How it works…
There's more…
See also
Downloading images from Flickr and Picasa
Getting ready
How to do it…
How it works…
There's more…
Performing cross-platform multithreading
Getting ready
How to do it...
How it works...
Synchronizing native cross-platform threads
Getting ready
How to do it...
How it works...
See also
Managing memory using reference counting
Getting ready
How to do it...
How it works...
There's more...
See also
Implementing asynchronous task queues
Getting ready
How to do it...
How it works...
Handling asynchronous callbacks invocation
Getting ready
How to do it…
How it works…
Working with the network asynchronously
Getting ready
How to do it…
How it works…
See also
Detecting a network address
Getting ready
How to do it...
How it works...
There's more...
Writing the HTTP server
Getting ready
How to do it…
How it works…
There's more…
See also
4. Organizing a Virtual Filesystem
Introduction
Abstracting file streams
Getting ready
How to do it...
How it works...
There's more...
See also
Implementing portable memory-mapped files
Getting ready
How to do it...
How it works...
Implementing file writers
How to do it...
How it works…
See also
Working with in-memory files
Getting ready
How to do it...
How it works...
See also
Implementing mount points
Getting ready
How to do it...
How it works...
There's more...
See also
Enumerating files in the .zip archives
Getting ready
How to do it...
How it works...
See also
Decompressing files from the .zip archives
Getting ready
How to do it...
How it works...
Loading resources asynchronously
Getting ready
How to do it...
There's more
See also
Storing application data
Getting ready
How to do it...
How it works...
There's more...
See also
5. Cross-platform Audio Streaming
Introduction
Initializing OpenAL and playing the .wav files
Getting ready
How to do it…
How it works...
See also
Abstracting basic audio components
Getting ready
How to do it…
How it works…
There's more…
See also
Streaming sounds
Getting ready
How to do it…
How it works…
There's more…
Decoding Ogg Vorbis files
Getting ready
How to do it...
How it works…
Decoding tracker music using ModPlug
Getting ready
How to do it...
How it works...
See also
6. Unifying OpenGL ES 3 and OpenGL 3
Introduction
Unifying the OpenGL 3 core profile and OpenGL ES 2
Getting ready
How to do it…
How it works…
There's more…
See also
Initializing the OpenGL 3 core profile on Windows
Getting ready
How to do it…
How it works…
There's more…
See also
Initializing OpenGL ES 2 on Android
Getting ready
How to do it…
How it works…
There's more…
See also
Unifying the GLSL 3 and GLSL ES 2 shaders
Getting ready
How to do it…
How it works…
There's more…
See also
Manipulating geometry
Getting ready
How to do it…
How it works…
There's more…
See also
Unifying vertex arrays
Getting ready
How to do it…
How it works…
There's more…
See also
Creating a wrapper for textures
Getting ready
How to do it…
How it works…
There's more…
See also
Creating a canvas for immediate rendering
Getting ready
How to do it…
How it works…
There's more…
See also
7. Cross-platform UI and Input Systems
Introduction
Processing multi-touch events on Android
Getting ready
How to do it...
How it works...
See also
Setting up multi-touch emulation on Windows
Getting ready
How to do it...
How it works...
See also
Handling multi-touch events on Windows
Getting ready
How to do it...
How it works...
There's more...
See also
Recognizing gestures
Getting ready
How to do it...
How it works...
Implementing an on-screen joypad
Getting ready
How to do it...
How it works...
Using FreeType for text rendering
Getting ready
How to do it...
How it works...
There's more…
Localization of in-game strings
Getting ready
How to do it...
How it works...
There's more...
8. Writing a Match-3 Game
Introduction
Handling asynchronous multi-touch input
Getting ready
How to do it…
How it works…
There's more…
See also
Improving the audio playback mechanism
Getting ready
How to do it…
How it works…
There's more…
See also
Shutting down the application
Getting ready
How to do it…
How it works…
There's more…
See also
Implementing the main loop
Getting ready
How to do it…
How it works…
There's more…
See also
Creating a multiplatform gaming engine
Getting ready
How to do it…
How it works…
See also
Writing the match-3 game
Getting ready
How to do it…
How it works…
There's more…
See also
Managing shapes
Getting ready
How to do it…
How it works…
See also
Managing the game field logic
Getting ready
How to do it…
How it works…
There's more…
Implementing user interaction within a game loop
Getting ready
How to do it…
How it works…
There's more…
See also…
9. Writing a Picture Puzzle Game
Introduction
Implementing picture puzzle game logic
Getting ready
How to do it...
How it works...
Implementing the animated 3D image selector
Getting ready
How to do it...
How it works...
There's more...
See also
Page-based user interface
Getting ready
How to do it...
How it works…
There's more...
See also
Image gallery with Picasa downloader
How to do it…
How it works…
See also
Implementing the complete picture-puzzle game
Getting ready
How to do it…
How it works…
There's more...
See also
Index
Android NDK Game Development Cookbook
Android NDK Game Development Cookbook
Copyright © 2013 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: November 2013
Production Reference: 1191113
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78216-778-5
www.packtpub.com
Cover Image by Aniket Sawant (<aniket_sawant_photography@hotmail.com>)
Credits
Authors
Sergey Kosarevsky
Viktor Latypov
Reviewers
Mootez Billeh Chaabani
Guy Cole
Maya Posch
Acquisition Editor
Rebecca Youe
Lead Technical Editor
Azharuddin Sheikh
Technical Editors
Adrian Raposo
Gaurav Thingalaya
Project Coordinator
Apeksha Chitnis
Proofreaders
Simran Bhogal
Ameesha Green
Paul Hindle
Indexer
Priya Subramani
Graphics
Abhinash Sahu
Sheetal Aute
Production Coordinator
Conidon Miranda
Cover Work
Conidon Miranda
About the Authors
Sergey Kosarevsky is a software engineer with experience in C++ and 3D graphics. He has worked for mobile industry companies and was involved in mobile projects at SPB Software and Yandex. He has more than 10 years of software development experience, and more than four years of Android NDK experience. Sergey got his PhD in the field of Mechanical Engineering from the St. Petersburg Institute of Machine Building in Saint Petersburg, Russia. In his spare time Sergey maintains and develops an open source multiplatform 3D gaming engine, Linderdaum Engine (http://www.linderdaum.com). He is online at http://blog.linderdaum.com and can be contacted by email at
I would like to thank Alexander Pavlov, a Google engineer, for the time and effort he put into carefully reviewing our initial drafts and helping us to improve this book. Also I would like to thank Igor Demura (Google) for valuable criticism on our chapter 6, as well as Dmitry Ovcharov (Yandex), and other friends and colleagues who helped this book happen.
Viktor Latypov is a software engineer and a mathematician with experience in compiler development, device drivers, robotics, high-performance computing, and a personal interest in 3D graphics and mobile technology. Surrounded by computers for almost 20 years, he enjoys every bit of developing and designing software for anything with a CPU inside. Viktor holds a PhD in Applied Mathematics from Saint Petersburg State University.
I would like to thank my mother, Galina Fedyushina, for all of the support and the innate thirst for knowledge.
About the Reviewers
Mootez Chaabani works as a software engineer R&D at a French company. He has recently graduated from studies in Graphical Programming, and Virtual and Augmented Reality. He has published two apps: Quiz game in the Windows Market Place, and an Android app in the local app shop. He is currently working on Android/C++ projects based on 3D in SpacEyes.
He is currently working with SpacEyes as a Software Engineer R&D. He has also worked as an Android developer at Orange, Tunisia in 2012. In 2011, he was an intern at Microsoft, Tunisia.
He has also reviewed Unity Android Game Development, Beginner’s Guide, Packt Publishing, by Thomas James Moffitt-Finnegan.
I would like to thank my family, my soul mate, and all my of friends including the Bardo Boys (my neighborhood friends).
Guy Cole is a freelance silicon valley contractor working on mobile devices (Android and iOS), Java/J2EE, relational databases, TCP/IP networks, and UNIX/LINUX hosted enterprise solutions. Guy has designed and fielded applications for B2B, banking, health care, e-commerce, shipping, mass transit, national defense, enterprise/network management, and cable/broadcast industries. His customers include Northrop Grumman, Wells Fargo, Barclay Global Investments, Hewlett Packard, DHL Worldwide Express, Motorola, Cisco Systems, Cray Research, Tandem Computers, NCR, and many smaller (but equally interesting) companies.
Maya Posch has been involved with programming and technology in general from a young age. She has endeavored to expand her programming skills mostly on low-level, embedded, and game-related programming. She currently runs her own company—Nyanko—which is involved in these aforementioned fields, in addition to doing general development work for other companies.
www.PacktPub.com
Support files, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt’s online digital book library. Here, you can access, read and search across Packt’s entire library of books.
Why Subscribe?
Fully searchable across every book published by Packt
Copy and paste, print and bookmark content
On demand and accessible via web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
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