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 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. Virtually nothing has...

Testing the Dynamic Library on the Device

To test the library, upload the files to the device using make pushlib, which expands to the following commands adb push libcg02.so data adb push testlib data Then log in to the device, change to the data folder, and execute the test program Here, you have run the program in the version 1.5 of the emulator. As you see, the library fails to load. The next section provides tips to figure out what is going on. If you try this process in the SDK version 1.0 R2, it will work. Note that this doesn't...

Introducing Wolf 3D

When Wolf 3D came to the game scene of the 1990s, it revolutionized PC gaming forever. It was the first game of its kind a game with immersive 3D graphics where the player navigates the Nazi underground picking up weapons, opening secret doors, and shooting up rabid dogs and evil Nazis (see Figure 6-1). Figure 6-1. Wolf 3D running on the emulator Figure 6-1. Wolf 3D running on the emulator Even though the game's environment appears to be 3D, it really is not. In reality, the game uses a...

Setting Up Your Machine

To set up for building Android games, you need to install three software components on your Linux desktop Android source This contains the entire Android source code, plus C C++ JNI header files used to build custom shared libraries. Android native libraries These include things like the C runtime, math, XML, sound, and other libraries. GNU C C++ Toolchain for ARM processors This toolchain provides a C C++ compiler and linker required to build native libraries, plus other useful tools, such as...

N

Native activity, 139 native callback handlers, 227 native callbacks, in Wolfenstein 3D, 177178 native code compiling and testing shared library compiling statically, 51-52 debugging with strace, 50-51 testing dynamic library on device, 49 troubleshooting missing symbols, 4849 creating native Android app, 27, 45. See also application architecture creating Android project, 30 creating AVD, 27-29 testing native application, 52-54 native cube (cube.c), 139, 151 native cube renderer...

Signing the Application

Prior to the Android SDK 1.5, the application signature had to be done manually using the Java SDK jarsigner command (see Listing A-1). Listing A-1. Windows Batch Script to Sign the Wolf3D Application Package (APK) echo off set JAVA_HOME c Program Files Java jdk1.6.0_07 set PKG c tmp APK Wolf3D.apk -verbose -keystore ar-release-key.keystore PKG android_radio rem To verify that your .apk is signed, you can use a command like this JAVA_HOME bin jarsigner -verbose -verify PKG Listing A-1 uses the...

Main Activity Class Wolf Launcher

When this class starts, the method onCreate executes performs several actions. First, it sets the window manager to full screen with Next, it sets the content view XML for the application using R.layout.wolf. This represents the application layout XML containing the game image view and the controller components. It also keeps a reference to the image view representing the video buffer (R.id.wolf_iv) mView (ImageView) findViewById(R.id.wolf_iv) It then...

Android Gaming vs Java ME Gaming

If you have written many Android applications, perhaps you have noticed how different the Android Java API is from the Java SE and Java ME APIs, the standards for Java and mobile development respectively. As a matter of fact, according to Wikipedia, this is a major source of criticism about Android Android does not use a conventional Linux kernel. It does not have a native X Window system, nor does it support the standard GNU libraries, making reusing existing Linux applications or libraries...

Adding Support for OpenGL to the NDK

One of the limitations of the NDK 1.5 is the lack of support for OpenGL. Although Google discourages the use of this native API due to its unstable nature, you can manually add support for it within the NDK by following two simple steps. In this section, you will learn how to add support for OpenGL to the NDK. This is a critical tool if you are bringing games to the platform that use native OpenGL calls. In my case, I used this technique to get the native code from the great game Quake to...

Game Layout

GUIs in Android are defined by XML layouts, where visual components are placed in a variety of layout schemes. Doom's layout doom.xml is a relative layout, which has widgets placed relative to each other meaning they can overlap depending on the widget size . The master layout contains an image view and two table layouts. In Android, an image view encapsulates an array of pixels representing an image. The great thing about image views is that they have efficient automatic resize capabilities....

Opengl The Java Way On Android Example

Let's look at how OpenGL graphics are done within Java. For this exploration, you need to create a project to hold the GL tumbling cubes application from the Android samples. Here is how 1. Click the New Android Project button. 2. In the New Android Project dialog box, enter a project name, such as ch05.OpenGL. 3. Specify the build target as Android 1.5. 4. Enter an application name, such as OpenGL Java. 5. Enter a package name, such as opengl.test. 6. Select Create Activity and enter...

Hybrid 3D Graphics with OpenGL and JNI

The classic Asteroids arcade game presented in the previous chapter provided a great introduction to drawing techniques in Android, using polygons and user-defined layouts. Now it's time to ramp things up a notch. In this chapter, you will learn a neat trick to mix OpenGL code in Java and C. This is a key step in reusing large portions of OpenGL C code along with Java code, thus using the best features of each language for maximum savings in time and costs. Any game developer knows that OpenGL...

Setting Up Your Development Environment

Now you are ready to get your IDE up and running with the Android development kit. Let's go through the installation of the latest available Android SDK 1.6 at the time of this writing available from over Eclipse 3.5 Galileo, available from http www.eclipse.org . 1. Start Eclipse Galileo and select Help gt Check for Updates, as shown in Figure 1-3. Figure 1-3. Choosing Check for Updates from the Eclipse 3.5 Galileo workbench's Help menu Figure 1-3. Choosing Check for Updates from the Eclipse...

OpenGL the Native

In the previous section, you saw how a pure Java OpenGL application works from the ground up. This applies if you write an application from scratch in Java. However, if you already have a C OpenGL renderer and wish to interface with Android, you probably don't want to rewrite your application especially if it has thousands of lines of code . This would consume significant time and resources, and more than likely, give you terrible headache. To understand how you can maximize the return on your...

Caveats of Porting OpenGL Games to Android

Today's smart phones have become pretty powerful. They feature a GPU capable of advanced graphics. Nevertheless, when it comes to writing advanced 3D games for embedded devices using OpenGL, several issues should be considered. Consider a game like Quake, which has been ported to multiple smart phones. This game uses immediate mode drawing for specifying geometry. For example, consider the following snippet to render an arbitrary polygon and corresponding texture Bind some texture glBegin...

Compiling Wolf 3D with the NDK

In this section, you will learn how to use the NDK to compile the native code in Chapter 6 in a Windows system running Cygwin. Take a look at the NDK folder structure. There are two folders that any application must use app This folder contains all the modules to be compiled. sources This folder contains the code for every module in the app folder. Let's compile the Wolf3D library using the NDK 1. Create an application module folder in NDK_HOME apps Wolf3D. 2. Within the folder above you need...

Whats Next

In this chapter, you have taken the first steps in writing a simple Java game and looked at the different issues that affect Java game development for Android, such as the significant differences between the Android API and the Java ME standard and how that influences code reuse and multiplatform support. You also learned gaming tricks, including creating an XML user-defined linear layout, using a timer task to simulate a game loop, invalidating a view layout within a user-defined non-UI...

Testing on the Emulator

We can finally start gaming Start your emulator, and run the game like so 1. From the Eclipse main menu, click Run Run Configurations. 2. On the left side, right-click Android Application New. 3. Enter a name SpaceBlaster , and select the project ch03.SpaceBlaster see Figure 3-8 . Figure 3-6 shows how the game should look on the emulator. To play, tap the screen and use the arrows to move the ship around. Press the space bar to fire the gun. Figure 3-8. The Run Configurations dialog for...

Loading Bitmap Sprites

Bitmap sprites are loaded by SpaceBlasterGame.initialize from the drawables section of the project using the getImage RESOURCEID method. getImage is defined in the base class ArcadeGame, so it'll be easy for child classes to reuse. For example, the game's explosion is simply an array of bitmaps loaded from the drawables folder shown in Figure 3-5 . To create an explosion, the game manipulates the explosion frame number when the display is drawn. The tricky part is keeping track of many...

Creating the Game Layout

As mentioned before, the class SpaceBlasterGame extends ArcadeGame, which in turn extends the Android layout LinearLayout. In this way, we can define a game thread to update the state of the game and simply draw sprites in the onDraw method of LinearLayout, thus gaining a finer control over the drawing process. SpaceBlasterGame performs the drawing, and it has all the game logic. This class is relatively complex compared to the others, but before we take a look at it, let's see how the game...

Creating a Key Store

A key store is a password-protected file that contains public private key pairs used for JAR signatures. You can create a key store with the following command keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -validity 10000 -storepass lt password1 gt -keypass lt password2 gt Table A-1 lists the possible arguments for the keytool command. Table A-1. Arguments for the Keytool Command Generate a public and private key pair. Specify the validity period in days. Tip...

Creating the Movement Controller

The movement controller is designed to provide navigation and other actions for phones with no keyboard. It is modeled after the popular SNES controller see Figure 6-5 , and provides the following buttons Navigation buttons For up, down, left, and right rotation Selection buttons For the select and start actions Extra buttons for miscellaneous actions In Wolf 3D, they are mapped as follows Y to strafe left, X to strafe right, B to fire, and A to enable running.

An Android Linker Script

Listing 1-3 shows the Android linker helper script, called ald. Listing 1-3. Linker Helper Script ald bin bash Set these values to match your system System image location SYS_ROOT HOME tmp android system arm-eabi 4.2.1 libgcc.a Linker libraries C runtime, Math, and extra symbols LIBRARIES -lc -lm LIBGCC Library locations LIB_PATHS -rpath system lib -rpath SYS_ROOT lib -L SYS_ROOT lib -L JAVA_HOME jre lib i386 -L. -nostdlib Linker command echo GCC LD_FLAGS LIB_PATHS LIBRARIES GCC LD_FLAGS...

Native Method Implementations

Table 7-4 shows the Java native signatures and their C counterparts in jni_doom.c. Table 7-4. Java Native Methods and Their Native Counterparts Java Method C Method static native int DoomMain String argv JNIEXPORT jint JNICALL env, jclass class, jobjectArray jargv static native int keyEvent int type, int key JNIEXPORT jint JNICALL env, jclass cls, jint type, jint key static native int motionEvent int btn, int x, JNIEXPORT jint JNICALL jclass cls, jint btn, jint x, jint y Before you can proceed...

Testing Doom for Android in the Emulator

To test the game in the emulator, create a launch configuration within your Eclipse IDE, as follows 1. From the main menu, select Run gt Run Configurations. 2. Enter a name for the configuration Doom and select the project ch07.Android.Doom. 3. Set the Launch Action as Launch Default Activity. Figure 7-4 shows the completed Run Configurations dialog box for this example. Figure 7-4. Android run configuration for Doom Figure 7-4. Android run configuration for Doom Now let's play some Doom. From...

Creating Space Blaster Your First Java Game

This section presents the pure Java game Space Blaster. Even though this is an Android game, some code has been reused from a Java applet. Figure 3-2 shows the game in action. The objective of the game is to navigate a space ship through a field of meteors, shooting them up as you go. The ship has a laser weapon and a defensive shield. You can choose to shoot the meteor, dodge it by dragging the ship with your fingertips, or let the shield protect you, but whatever...

Debugging with strace

For some reason, the native test program for the library runs in version 1.0 R2 of the SDK, but fails to load in 1.5 R2. The output gives a clue the file bionic linker linker.c 1581 fails to load the library. There is a simple Linux tool called strace that can help in this situation. The strace tool runs the specified command until it exits. It intercepts and records the system calls that are called by a process and the signals that are received by a process. The name of each system call, its...

Testing the Scripts

Save the scripts in Listings 1-2 and 1-3 as agcc and ald in your HOME bin directory. Then issue the following commands to test them arm-none-linux-gnueabi-gcc Sourcery G Lite 2008q3-72 4.3.2 Copyright C 2008 Free Software Foundation, Inc. This is free software see the source for copying conditions. There is NO warranty not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. arm-none-linux-gnueabi-ld warning library search path is unsafe for cross-compilation GNU ld Sourcery G Lite...

Doom Library DSO Compilation

Listing 7-25 shows the Makefile for the DSO. It uses the compiler and linker helper scripts agcc and ald, created in Chapter 1. The default optimization level is set to 2. Other compilation flags include the following -ffast-math Use fast math. -Wall Display all warnings. -nostdinc Do not include standard header files. lib builds the DSO libdoom_jni.so. jni creates the JNI headers for doom.jni.Natives.java and places them in the include folder. deploy-lib deploys the DSO to the device using the...

Initializing Sprites and Sounds

Game initialization occurs in the initialize method overloaded from the parent abstract class ArcadeGame see Listing 3-6 . The call sequence goes as follows LinearLayout.onLayout calls ArcadeGame.initilize, which calls SpaceBlasterGame.initilize. This last method performs the following tasks Set the style and color attributes for the Paint objects text, laser bar, and shield bar. Load the game bitmap sprites ship, meteor, laser bullet, and explosion sequence. mTextPaint.setARGB 255, 255, 255,...

Implementing the Game

The previous sections showed you the foundation for the main class, SpaceBlasterGame. Here is where all the meat resides. Let's take a closer look at the most important sections of SpaceBlaster.java. Note that the class has been stripped for simplicity in Listing 3-5, but the chapter source contains the full implementation. import ch03.common.AudioClip import ch03.common.Tools import android.content.Context import android.graphics.Bitmap import android.graphics.Canvas import...

Understanding Game Architecture

Space Blaster is a relatively simple game. It has three main classes SpaceBlaster.java This is the main activity that bonds the game code with the Android platform. Its job is to load the game layout and process Activity events. SpaceBlasterGame.java This is where all the meat resides. This class has all the game logic, and it processes key and touch events. SpaceBlasterGame extends ArcadeGame. ArcadeGame.java This abstract class encapsulates common functionality. It also uses a standard Java...

Compiling Doom with NDK

If you read this book carefully, you'll get the sense that that I don't like the NDK 1.5 when I started in this project the NDK didn't even exist . I think the NDK 1.5 is cumbersome to use, because of the lack of integration with the Eclipse workbench. Plus, version 1.5 has only the bare bones to compile a native library that is, the C runtime, Compression library, and basic C support . Just when this book was being finished up, Google released the NDK 1.6, a nice improvement over 1.5. Here are...

Creating a Polygon Sprite Class for Asteroids

PolygonSprite is the final foundation class of the game see Listing 4-3 . It is used to describe all game objects including the ship, asteroids, and flying saucer. Furthermore, it tracks information such as the following for all of these game objects In this class, two polygons are used one to keep the basic shape, and the other to apply the final translation and rotation and to paint on screen. Listing 4-3. The PolygonSprite Class Used by Asteroids import ch04.common.Polygon import...