About Native Sound

Native games like Doom and Wolf 3D, shown in later chapters, pack sound resources in their own formats and manipulate the sound device at the operating system level. Even though Android runs a Linux kernel behind the scenes, it is not a standard Linux distribution, especially with dealing with low-level devices, for example The standard Linux sound device dev dsp and mixer dev mixer are not supported, making reusing sound logic very difficult for native games. For some reason, Google decided to...

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

It's time for the real thing. 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 16

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...

C to Java Callbacks 1

Table 7-5 shows the callbacks on the left side and the Java methods they invoke on the right. The callbacks can be divided into the following types Graphics initialization jni_init_graphics Table 7-5 shows the callbacks on the left side and the Java methods they invoke on the right. The callbacks can be divided into the following types Graphics initialization jni_init_graphics Sound and music jni start sound, Table 7-5. C to Java Callbacks in jni_doom.c void jni init graphics int width, int...

D Shooters Episode I Wolfenstein 3D for Android

The next two chapters are my personal favorites and the most exciting of this book. We start by looking at the real thing Wolfenstein 3D also referred to as Wolf 3D , the godfather of all 3D shooters. The main goal of this chapter is to show you how easy is to bring Wolf 3D from the PC to the Android device, but also in this chapter, you will learn how to do the following Maximize code reuse by compiling high-performance native code in a dynamic shared library. Write JNI code to connect Java...

Creating a PolygonSprite 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...

Java Callbacks

The Java callbacks are used to send messages from the native layer to the Java layer see Listing 5-13 . The cube renderer implements two callbacks jni_send_str const char text This callback sends a string message to Java mostly for debugging purposes . It does so by attaching to the current thread. This step must be done if you call JNI in a C function outside a JNI native implementation. The callback then loads the Java class opengl.jni.Natives.java. Finally, it calls the Java method...