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
Free Trial
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (5 Ratings)
Paperback Nov 2013 320 pages 1st Edition
eBook
₱579.99 ₱2245.99
Paperback
₱2806.99
Subscription
Free Trial
Arrow left icon
Profile Icon Sergey Kosarevsky Profile Icon Viktor Latypov
Arrow right icon
Free Trial
Full star icon Full star icon Full star icon Full star icon Empty star icon 4 (5 Ratings)
Paperback Nov 2013 320 pages 1st Edition
eBook
₱579.99 ₱2245.99
Paperback
₱2806.99
Subscription
Free Trial
eBook
₱579.99 ₱2245.99
Paperback
₱2806.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781782167785
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Nov 25, 2013
Length: 320 pages
Edition : 1st
Language : English
ISBN-13 : 9781782167785
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 ₱260 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 ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 8,114.97
Android Native Development Kit Cookbook
₱2806.99
Android NDK Game Development Cookbook
₱2806.99
Android Security Cookbook
₱2500.99
Total 8,114.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.