Game Development Reference
In-Depth Information
C to Java Callbacks
C to Java callbacks are used to delegate engine messages to the listener activity. To do so,
the native interface class uses a private listener and a static setter method, like so:
private static EventListener listener;
public static void setListener(EventListener l) {
listener = l;
}
Note that there can be only one listener. When the Doom engine sends a message (such as
“have some text”), the native interface class simply delegates to the listener, which deals
with the event.
private static void OnMessage(String text, int level) {
if (listener != null)
listener.OnMessage(text, level);
}
In this code, the engine is saying “have some text,” along with an integer log level. The rest
of callbacks are shown in Listing 5-12.
Listing 5-12. Native Interface Class (Natives.java)
package doom.jni;
import android.util.Log;
public class Natives {
public static final String TAG = "Natives";
private static EventListener listener;
public static final int EV_KEYDOWN = 0;
public static final int EV_KEYUP = 1;
public static final int EV_MOUSE = 2;
public static interface EventListener {
void OnMessage(String text, int level);
void OnInitGraphics(int w, int h);
void OnImageUpdate(int[] pixels);
void OnFatalError(String text);
void OnQuit(int code);
void OnStartSound(String name, int vol);
void OnStartMusic(String name, int loop);
void OnStopMusic(String name);
void OnSetMusicVolume(int volume);
}
 
Search WWH ::




Custom Search