Game Development Reference
In-Depth Information
Native Interface Class
The native interface class ( Natives.java ) acts as the glue between the Java and the C code (see
Listing 6-14). In Wolf 3D, this class is a two-way pipeline that handles all native access:
It describes native methods that will be called from Java code.
It implements Java callbacks, that is, java methods that will be called from C.
This class declares the following native methods implemented in C:
native int WolfMain(String[] argv) : This method starts the main game loop.
argv represents an array of arguments that will be sent to the native layer.
native int keyPress(int key) : This method can be used to send a key pressed
event to the native layer. The argument key represents a PC scan code.
native int keyRelease(int key) : This method can be used to send a key release
event where key represents PC scan code.
Tip Note that PC scan codes are not the same as Android key codes. The utility class wolf.util.ScanCodes is
used to convert these codes.
Listing 6-14. Native Interface Class Natives.java.
package wolf.jni;
import android.util.Log;
public class Natives {
public static final String TAG = "Natives";
public static final int EV_KEYDOWN = 0;
public static final int EV_KEYUP = 1;
public static final int EV_MOUSE = 2;
private static EventListener listener;
/**
* Native event listener interface
*/
public static interface EventListener {
void OnMessage(String text);
void OnInitGraphics(int w, int h);
void OnImageUpdate(int[] pixels, int x, int y, int w, int h);
void OnSysError(String text);
void OnStartSound(int idx);
void OnStartMusic(int idx);
Search WWH ::




Custom Search