Book Image

Android NDK Game Development Cookbook

Book Image

Android NDK Game Development Cookbook

Overview of this book

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

Synchronizing native cross-platform threads


Synchronization is required to prevent different threads from accessing shared resources simultaneously. A piece of code that accesses a shared resource—that must not be concurrently accessed by more than one thread—is called a critical section (http://en.wikipedia.org/wiki/Critical_section). To avoid race conditions, a mechanism is required at the entry and exit of the critical section. In Windows applications, critical sections are part of the WinAPI and in Android, we use mutexes from the pthread library, which serve the same purpose.

Getting ready

Android native synchronization primitives are POSIX-based. They include thread's management functions, mutexes, conditional variables, and barriers. Take a look at the header file platforms\android-14\arch-arm\usr\include\pthread.h in your NDK folder.

How to do it...

  1. Let's create an API-independent abstraction to synchronize threads:

    class Mutex
    {
    public:
      Mutex()
      {
    #if defined( _WIN32 )
        InitializeCriticalSection...