Introduction

Pro Android Games will help you to create the best games for the Android platform. There are plenty of books out there that tackle this subject, but only this book gives you a unique perspective by showing you how easy it is to bring native PC games to the platform with minimum effort. This is done using real-world examples and source code on each chapter. Keep in mind that, before you dig into this book, you will need a solid foundation in Java and ANSI C. I have made a great effort to explain the most complicated concepts as clearly and as simply as possible with a combination of graphics and sample code. The source code provided for each chapter will help you understand the concepts in detail and make the most of your time as a mobile game developer.

What Software Will You Need?

To make the most of this book, you will need the following tools:

A Windows or Linux PC with a Java SDK Properly Installed

I guess this is kind of obvious, as most development for Android is done in Java. Note that I mentioned a Java SDK, not JRE. The SDK is required because of the JNI header files and command line tools used throughout the latter chapters.

Eclipse IDE and Android SDK Properly Installed

Eclipse is the de facto IDE for Android development. I have used Eclipse Galileo to create the workspace for the book; nevertheless, Eclipse Ganymede should work as well.

Android SDK properly installed means two things:

  1. You must install the Android SDK plug-ins for Eclipse:

    1. From the IDE main menu click Help Install New Software.

    2. Click the Add button to add a new Site and enter:

      • A name: Android SDK

      • A location: https://dl-ssl.google.com/android/eclipse/. Click OK.

    3. Select the Android SK from the Available Software dialog and follow the easy installation instructions from the wizard.

  2. You must install the Android SDK: It can be downloaded from the Android site above. Keep in mind that Eclipse must be told about the location of the Android SDK. From the main IDE menu click Window Preferences. On the left navigation menu select Android and enter the SDK location (see Figure 1). I have used SDK 1.5 because that was the latest available by the time of this writing; however, the code in this book has been tested with SDK 1.6 and 2.0 (see the SDK compatibility section for details).

Android SDK configuration dialog in Eclipse Galileo

Figure 1. Android SDK configuration dialog in Eclipse Galileo

GNU C Compiler for ARM Processors

AGNU C compiler is required for the hybrid games in the book. These games combine Java code with a native core library, hence the need for a C compiler that targets the mobile ARM processor. The compiler used in this book and detailed in Chapter 1 is the Code Sourcery GNU G++ Toolkit. You are not bound, however, to use this compiler; as a matter of fact, you can use any C compiler capable of generating ARM code, including the Android Native Development Kit (NDK).

Chapter Source

This is an optional tool but it will help you greatly to understand the concepts as you move along. I have made my best effort to describe each chapter as simply as possible. Nevertheless, some of the games (especially Wolf 3D and Doom) have very large core engines written in C (100K lines for Doom), which are poorly commented and very hard to understand. All in all you will see how easily these great languages (Java and C) can be combined with minimal effort. Get the companion source for the book from the publisher at http://www.apress.com. It was built using Eclipse Galileo.

What Makes This Book Unique?

I think it is important for the reader to understand my goal with this manuscript and what I believe sets this book apart. Even though Java is the primary development language for Android, Google has realized the need for hybrid Java/C development if Android is to succeed as a gaming platform, so much so that they released the Native Development Kit (NDK). Google is realizing the need to support C development to catch up with the overwhelming number of native games written for other mobile platforms like the iPhone. PC games have been around for decades (mostly written in C), and by using a simple ARM C compiler, you could potentially bring thousands of PC games to the Android Platform. This is what makes this book unique. Why translate 100k lines of painfully complicated code from C to Java if you can just combine both languages in an elegant manner with significant savings on time and money? My book will help you to do just that. This is my goal and what makes this book stand out. In the other hand, this book also includes chapters of pure Java games in a well-balanced layout to satisfy both the Java purist and the C lover in you.

Android SDK Compatibility

As a developer you may ask yourself about the SDK compatibility of the code in this book. This is an important question as new versions of the Android SDK come out frequently. By the time of this writing, Google released the Android SDK version 2.0. The code in this chapter has been tested with the following versions of the Android SDK:

  • SDK version 2.0

  • SDK version 1.6

  • SDK version 1.5

  • SDK version 1.0

The bottom line is that the code in this book will run in any version of the SDK from 2.0 to 1.0, and that was my intention all along.

This book has a well-balanced layout of pure Java and hybrid games, divided as follows:

This chapter provides the first step to set up a Linux system for hybrid game compilation, including fetching the Android source, extracting device system libraries, setting up a custom compilation toolchain, custom compilation scripts, plus details on setting up the Eclipse IDE for use throughout the rest of the book.

In this chapter you will learn how to combine Java and C code in an elegant manner by building a simple Java application on top of a native library. You will learn exciting concepts about the Java Native Interface (JNI) and the API used to combine Java and C in a single unit, including how to load native libraries, how to use the native keyword, how to generate the JNI headers, plus all about method signatures, Java arrays vs. C arrays, invoking Java methods, compiling and packing the product, and more.

This chapter is the first in a series to explore pure Java gaming with a real-world game dubbed Space Blaster. The goal of this game is to maneuver (using your finger tips or the keyboard) a space ship through a field of meteors. This chapter will teach you how to build custom XML-based linear layout, how to use an abstract class and timer tasks to simulate a simple game loop, how to invalidate views within a non-UI thread, how to load sprites and sounds from the project resources, plus drawing techniques such as sprite animations, drawing simple objects, and setting style and color using the Paint object.

Pure Java games continue in Chapter 4 with the arcade classic Asteroids. This chapter will teach you all about drawing polygon sprites on the Android canvas. This is a somewhat difficult technique due to the lack of polygon support in the Android API. The chapter relies on the high portability of the Java language to bring polygon code from the J2SE API into the Android API to ultimately create Asteroids. As you may have noticed, this is a mostly Polygon-based game. Other interesting topics include game life cycle steps, initialization, draw, and update physics, responding to key and touch events, plus testing on the device emulator.

This chapter deals with 3D graphics with OpenGL. It presents a neat trick I stumbled on by coincidence that allows for mixing OpenGL API calls in both Java and C. This concept is illustrated by using the 3D cubes sample provided by Google to demonstrate OpenGL in pure Java and hybrid modes. This trick could open a new frontier of 3D development for Android with the potential to bring a large number of 3D PC games to the platform with enormous savings in development costs and time.

This is the first chapter in a series of two, which are my personal favorites of this book. Here it brings the godfather of all PC 3D shooters to the Android platform—Wolfenstein 3D. Who would have thought that a PC game like this one could be brought to an Android mobile device with minimal effort? This chapter demonstrates how Java and C can coexist harmoniously and includes topics such as the basic game architecture (showing how the Java and C components fit together), resource handlers for sound, music, key and touch events, how to use JNI to cascade graphics information, video buffers, and sound/music requests back to Java, plus compilation and testing.

Chapter 7 takes things to the next level with the ground-breaking game for the PC—Doom. Doom is arguably the greatest 3D game ever created and opened new frontiers in 3D graphics. The ultimate goal of this chapter is not to describe the game itself, but to show you how easy it is to bring a complex PC game like Doom to the Android platform. The proof? Doom is 100+K lines of C code and brought to Android with less than 200 lines of extra JNI API calls, plus the Java code required to build the mobile UI. This chapter shows that you don't have to translate 100K lines of C into Java but simply marry these two powerful languages in an elegant application. Consider the potential savings in development time and costs! This chapter is a must-read.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181