Game Development Reference
In-Depth Information
What you have done is simply extract whatever is inside the while loop and put it in
RenderFrame() to render a single frame. Also, notice that you use conditional compilation,
like so:
#ifndef ANDROID
while (1) {
...
}
#endif
Here #ifndef ANDROID tells the compiler to include the enclosed code only if the flag ANDROID
has not been defined at compilation time. This creates portability and allows the same code
to work in multiple Linux flavors. Therefore, this tiny change allows the following sequence of
events to occur seamlessly:
1.
When the application starts, the main activity will start (Java).
2.
The activity creates an OpenGL surface and starts a separate
rendering thread (Java).
When the surface is first created, the QuakeMain native method is
invoked only once, which in turn calls the Quake II main function
(Java/C), passing game startup arguments.
3.
4.
The rendering thread loops continuously, firing the render frame
event, which will invoke the native function RenderFrame to draw a
single frame of the game.
5.
After the single frame rendering completes, Android invokes the
OpenGL swap buffers operation to display the graphics on the
device; the process will resume back from step 4 until the user
decides to terminate the program.
Now that you have the rendering smoothed, let's tackle key events.
Pumping Key Events
Keys are sent from the Java wrappers to the C engine via the native functions of keyPress
and keyRelease , which are declared in quake.jni.Natives.java . Both functions have as
an argument the ASCII code of the key, which must be translated from the Android key
format. The translation is identical to that in Chapter 6 under the “Handling Key Events”
section, where you used an array of key mappings between Android codes and ASCII
codes. The tricky part is dealing with all the different keyboard layouts of the dozens of
keyboard-equipped phones out there. Nevertheless, the C implementations of keyPress
and keyRelease need a tiny change to feed the key to the Quake II engine handler, as
shown in Listing 7-3.
 
Search WWH ::




Custom Search