Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Android NDK Game Development Cookbook
Android NDK Game Development Cookbook

Android NDK Game Development Cookbook: For C++ developers, this is the book that can swiftly propel you into the potentially profitable world of Android games. The 70+ step-by-step recipes using Android NDK will give you the wide-ranging knowledge you need.

Arrow left icon
Profile Icon Sergey Kosarevsky Profile Icon Viktor Latypov
Arrow right icon
$9.99 $32.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (5 Ratings)
eBook Nov 2013 320 pages 1st Edition
eBook
$9.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Sergey Kosarevsky Profile Icon Viktor Latypov
Arrow right icon
$9.99 $32.99
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (5 Ratings)
eBook Nov 2013 320 pages 1st Edition
eBook
$9.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Android NDK Game Development Cookbook

Chapter 2. Porting Common Libraries

In this chapter, we will cover:

  • Compiling the native static libraries for Windows
  • Compiling the native static libraries for Android
  • Compiling the libcurl networking library
  • Compiling the OpenAL library
  • Compiling libvorbis, libmodplug, and libtheora
  • Using the FreeImage graphics library
  • Using FreeType library for text rendering
  • Implementing timing in physics
  • Rendering graphics in 2D
  • Setting up Box2D simulations
  • Building the ODE physical library

Introduction

This chapter explains how to port existing popular C/C++ libraries to Android using Android NDK. These libraries are widely used to implement feature-rich applications with graphics, sounds, and physical simulations entirely in C++. There is not much fun in simply compiling libraries. So, the parts related to FreeImage, FreeType, and Box2D provide minimal samples to demonstrate the functionality of each library. Audio and networking libraries are discussed in greater detail in the subsequent chapters. We will show you how to compile libraries and, of course, give some short samples and hints on how to start using them.

Typical caveats for porting libraries across different processors and operating systems are memory access (structure alignment/padding), byte-order (endianness), calling conventions, and floating-point issues. All the libraries described below address these issues quite well, and even if some of them do not officially support Android NDK, fixing this is just...

Compiling the native static libraries for Windows

To build the Windows version of libraries, we need a C++ compiler. We use MinGW with the GCC toolchain described in Chapter 1, Establishing a Build Environment. For each library, we have a collection of source-code files, and we need to get the static library, a file with the .a extension.

Getting ready

Let us assume the src directory contains the source code of a library we need to build for Android.

How to do it...

  1. Let us start with writing a makefile:
    CFLAGS = -I src

    This line defines a variable with a list of compiler command-line parameters. In our case, we instruct the compiler to search the src directory for header files. If the library source code spans across many directories, we need to add the –I switch for each of the directories.

  2. Next, we add the following lines for each source file:
    <SourceFile>.o:
      gcc $(CFLAGS) –c <SourceFile>.cpp –o <SourceFile>.o

    <SourceFile> should be replaced by the...

Compiling the native static libraries for Android

Android NDK includes a number of GCC and Clang toolchains for each kind of the supported processors.

Getting ready

When building a static library from the source code, we follow the steps similar to the Windows version.

How to do it...

  1. Create a folder named jni and create the Application.mk file with the appropriate compiler switches, and set the name of the library accordingly. For example, one for the FreeImage library should look like the following:
    APP_OPTIM := release
    APP_PLATFORM := android-8
    APP_STL := gnustl_static
    APP_CPPFLAGS += -frtti 
    APP_CPPFLAGS += -fexceptions
    APP_CPPFLAGS += -DANDROID
    APP_ABI := armeabi-v7a x86
    APP_MODULES := FreeImage
  2. The Android.mk file is similar to the ones we have written for the sample applications in the previous chapter, yet with a few exceptions. At the top of the file, some required variables must be defined. Let us see what the Android.mk file for the FreeImage library may look like:
    # Android API level...

Compiling the libcurl networking library

The libcurl library is a de facto standard for native applications, which deal with numerous networking protocols. The libcurl compilation for Android on a Windows host requires some additional steps to be done. We explain them in this recipe.

Getting ready

Download the libcurl source code from the library homepage: http://curl.haxx.se/libcurl/.

How to do it...

  1. Since the libcurl library build process is based on Autoconf, we will need to generate a curl_config.h file before actually building the library. Run the configure script from the folder containing the unpacked libcurl distribution package. Cross-compilation command-line flags should be set to:
    --host=arm-linux CC=arm-eabi-gcc
  2. The -I parameter of the CPPFLAGS variable should point to the /system/core/include subfolder of your NDK folder, in our case:
    CPPFLAGS=”-I D:/NDK/system/core/include”
  3. The libcurl library can be customized in many ways. We use this set of parameters (disable all...

Compiling the OpenAL library

OpenAL is a cross-platform audio library used in many gaming engines. Here are some notes on how to build it for Android.

Getting ready

Download the source code of the Martins Mozeiko port from his page: http://pielot.org/2010/12/14/openal-on-android/.

The home page of the library is as follows: http://github.com/AerialX/openal-soft-android.

How to do it...

  1. To render the generated, or saved, audio stream we use the OpenAL library, which is compiled using the standard Android.mk and Application.mk configuration files included in the accompanying materials.
  2. The Android port of the library is actually a wrapper made by Martins Mozeiko for the Android Java class android.media.AudioTrack using the JNI. The code is licensed under the GNU Library General Public License and is included in the book’s supplementary materials.

How it works…

The minimalistic source code to initialize and deinitialize OpenAL looks as follows:

ALCdevice* Device = alcOpenDevice( NULL...

Compiling libvorbis, libmodplug, and libtheora

For the loading of audio streams, we use libogg, libvorbis, and libmodplug. Video streams are handled in a similar way with the libtheora library. Here, we only give general hints on how to build the libraries from their sources, since the actual build process is straightforward once you have our typical Android.mk and Application.mk files in place.

Getting ready

Download the sources of libvorbis and libtheora codecs from http://www.xiph.org/downloads and the libmodplug library from http://modplug-xmms.sourceforge.net.

How to do it...

  1. libvorbis and libtheora both depend on libogg. The compilation of these libraries is straightforward with the provided makefiles and a standard Android.mk file with the list of source files.

    Note

    Makefiles for libvorbis and libtheora libraries must refer to the include directories of libogg.

  2. libmodplug is an open source tracker music decoder by Olivier Lapicque. We provide a shortened version of his library, with loaders...

Introduction


This chapter explains how to port existing popular C/C++ libraries to Android using Android NDK. These libraries are widely used to implement feature-rich applications with graphics, sounds, and physical simulations entirely in C++. There is not much fun in simply compiling libraries. So, the parts related to FreeImage, FreeType, and Box2D provide minimal samples to demonstrate the functionality of each library. Audio and networking libraries are discussed in greater detail in the subsequent chapters. We will show you how to compile libraries and, of course, give some short samples and hints on how to start using them.

Typical caveats for porting libraries across different processors and operating systems are memory access (structure alignment/padding), byte-order (endianness), calling conventions, and floating-point issues. All the libraries described below address these issues quite well, and even if some of them do not officially support Android NDK, fixing this is just a...

Compiling the native static libraries for Windows


To build the Windows version of libraries, we need a C++ compiler. We use MinGW with the GCC toolchain described in Chapter 1, Establishing a Build Environment. For each library, we have a collection of source-code files, and we need to get the static library, a file with the .a extension.

Getting ready

Let us assume the src directory contains the source code of a library we need to build for Android.

How to do it...

  1. Let us start with writing a makefile:

    CFLAGS = -I src

    This line defines a variable with a list of compiler command-line parameters. In our case, we instruct the compiler to search the src directory for header files. If the library source code spans across many directories, we need to add the –I switch for each of the directories.

  2. Next, we add the following lines for each source file:

    <SourceFile>.o:
      gcc $(CFLAGS) –c <SourceFile>.cpp –o <SourceFile>.o

    <SourceFile> should be replaced by the actual name of the ...

Compiling the native static libraries for Android


Android NDK includes a number of GCC and Clang toolchains for each kind of the supported processors.

Getting ready

When building a static library from the source code, we follow the steps similar to the Windows version.

How to do it...

  1. Create a folder named jni and create the Application.mk file with the appropriate compiler switches, and set the name of the library accordingly. For example, one for the FreeImage library should look like the following:

    APP_OPTIM := release
    APP_PLATFORM := android-8
    APP_STL := gnustl_static
    APP_CPPFLAGS += -frtti 
    APP_CPPFLAGS += -fexceptions
    APP_CPPFLAGS += -DANDROID
    APP_ABI := armeabi-v7a x86
    APP_MODULES := FreeImage
  2. The Android.mk file is similar to the ones we have written for the sample applications in the previous chapter, yet with a few exceptions. At the top of the file, some required variables must be defined. Let us see what the Android.mk file for the FreeImage library may look like:

    # Android API level...

Compiling the libcurl networking library


The libcurl library is a de facto standard for native applications, which deal with numerous networking protocols. The libcurl compilation for Android on a Windows host requires some additional steps to be done. We explain them in this recipe.

Getting ready

Download the libcurl source code from the library homepage: http://curl.haxx.se/libcurl/.

How to do it...

  1. Since the libcurl library build process is based on Autoconf, we will need to generate a curl_config.h file before actually building the library. Run the configure script from the folder containing the unpacked libcurl distribution package. Cross-compilation command-line flags should be set to:

    --host=arm-linux CC=arm-eabi-gcc
  2. The -I parameter of the CPPFLAGS variable should point to the /system/core/include subfolder of your NDK folder, in our case:

    CPPFLAGS=”-I D:/NDK/system/core/include”
  3. The libcurl library can be customized in many ways. We use this set of parameters (disable all protocols except...

Compiling the OpenAL library


OpenAL is a cross-platform audio library used in many gaming engines. Here are some notes on how to build it for Android.

Getting ready

Download the source code of the Martins Mozeiko port from his page: http://pielot.org/2010/12/14/openal-on-android/.

The home page of the library is as follows: http://github.com/AerialX/openal-soft-android.

How to do it...

  1. To render the generated, or saved, audio stream we use the OpenAL library, which is compiled using the standard Android.mk and Application.mk configuration files included in the accompanying materials.

  2. The Android port of the library is actually a wrapper made by Martins Mozeiko for the Android Java class android.media.AudioTrack using the JNI. The code is licensed under the GNU Library General Public License and is included in the book’s supplementary materials.

How it works…

The minimalistic source code to initialize and deinitialize OpenAL looks as follows:

ALCdevice* Device = alcOpenDevice( NULL );
ALCcontext*...

Compiling libvorbis, libmodplug, and libtheora


For the loading of audio streams, we use libogg, libvorbis, and libmodplug. Video streams are handled in a similar way with the libtheora library. Here, we only give general hints on how to build the libraries from their sources, since the actual build process is straightforward once you have our typical Android.mk and Application.mk files in place.

Getting ready

Download the sources of libvorbis and libtheora codecs from http://www.xiph.org/downloads and the libmodplug library from http://modplug-xmms.sourceforge.net.

How to do it...

  1. libvorbis and libtheora both depend on libogg. The compilation of these libraries is straightforward with the provided makefiles and a standard Android.mk file with the list of source files.

    Note

    Makefiles for libvorbis and libtheora libraries must refer to the include directories of libogg.

  2. libmodplug is an open source tracker music decoder by Olivier Lapicque. We provide a shortened version of his library, with loaders...

Left arrow icon Right arrow icon

Key benefits

  • Tips and tricks for developing and debugging mobile games on your desktop
  • Enhance your applications by writing multithreaded code for audio playback, network access, and asynchronous resource loading
  • Enhance your game development skills by using modern OpenGL ES and develop applications without using an IDE
  • Features two ready-to-run Android games

Description

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.

Who is this book for?

If you are a C++ developer who wants to jump into the world of Android game development and who wants to use the power of existing C++ libraries in your existing Android Java applications, then this book is for you. You need to have basic knowledge of C or C++ including pointer manipulation, multithreading, and object-oriented programming concepts as well as some experience developing applications without using an IDE.

What you will learn

  • Port popular C++ libraries to Android
  • Write portable multithreaded code
  • Play audio with OpenAL
  • Implement gesture recognition
  • Render text with FreeType
  • Use OpenGL ES to port and abstract APIs from the game code to develop games on a desktop PC
  • Debug mobile applications on your desktop
  • Access Flickr and Picasa web services from C++
  • Extract resources from APK archives
  • Develop Android applications without an IDE

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 25, 2013
Length: 320 pages
Edition : 1st
Language : English
ISBN-13 : 9781782167792
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 25, 2013
Length: 320 pages
Edition : 1st
Language : English
ISBN-13 : 9781782167792
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 158.97
Android Native Development Kit Cookbook
$54.99
Android NDK Game Development Cookbook
$54.99
Android Security Cookbook
$48.99
Total $ 158.97 Stars icon
Banner background image

Table of Contents

10 Chapters
1. Establishing a Build Environment Chevron down icon Chevron up icon
2. Porting Common Libraries Chevron down icon Chevron up icon
3. Networking Chevron down icon Chevron up icon
4. Organizing a Virtual Filesystem Chevron down icon Chevron up icon
5. Cross-platform Audio Streaming Chevron down icon Chevron up icon
6. Unifying OpenGL ES 3 and OpenGL 3 Chevron down icon Chevron up icon
7. Cross-platform UI and Input Systems Chevron down icon Chevron up icon
8. Writing a Match-3 Game Chevron down icon Chevron up icon
9. Writing a Picture Puzzle Game Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(5 Ratings)
5 star 60%
4 star 20%
3 star 0%
2 star 0%
1 star 20%
User Jan 04, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is my first book on Android Native Development Kit. I hope you will find it useful and write great games for Android.
Amazon Verified review Amazon
C. Moeller May 01, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you’d like to try making games on Android using C++, or want to use a C++ library with your current game, this book would be very helpful for you. The last two chapters especially bring it all together, creating two simpler games, just to demonstrate all the basic techniques needed to make any game (A tetris type game, and a picture puzzle that fetches pictures from the internet, and creates a cool little 3d carousel to display them).This book would be most helpful to someone who either has a C++ background, and wants to create Android games, while trying to avoid Java, or for a current Android Java programmer, who needs or wants higher performance for cpu intensive tasks. The target audience isn’t someone new to programming.Don’t expect it to teach you C++, or in depth game programming, but it will teach you how to create a game using C++ on Android (and on desktop, which is much more time efficient to debug on, and can really help you get your games out faster). From there you could extrapolate, and use the techniques to create any kind of game you like.
Amazon Verified review Amazon
Johnny Aug 23, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
good service
Amazon Verified review Amazon
Ralf Apr 25, 2014
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The book is as the title suggests a cookbook for developing games using the Android NDK.While I have only skimmed through the chapters, my initial impression is that it covers thebasic building blocks of game development: toolchain setup, compilation of existing libraries,audio, video, user interaction, and sums it up in two chapters where the authors discuss 2basic games.One criticism I have is that personally I found the Windows-specific code to be off-topic,perhaps it would have been more suitable for an appendix chapter.While the book does not seem to go into as much depth as the "Android NDK Beginner's Guide"it does seem like a nice way to jump into NDK game development. The authors certainlyhave a lot of experience to share with someone new to game development of Android.
Amazon Verified review Amazon
xuechjbj Dec 31, 2013
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
accumulating some libraries which want to integate into android. useless.can i refund it? it is first book i regretted.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.