Game Development Reference
In-Depth Information
listener.OnStartSound(idx);
}
// Fires when music is played in the C layer.
private static void OnStartMusic(int idx) {
if (listener != null)
listener.OnStartMusic(idx);
}
// Sends a key event to the native layer
// type : one of Natives.EV_KEYDOWN or Natives.EV_KEYUP
// sym: PC scan code
public static void sendNativeKeyEvent(int type, int sym) {
try {
if (type == Natives.EV_KEYDOWN)
Natives.keyPress(sym);
else
Natives.keyRelease(sym);
} catch (UnsatisfiedLinkError e) {
Log.e(TAG, e.toString());
}
}
}
In order for the game activity (
WolfLauncher
) to receive native callbacks, such as when a video
update occurs or a sound must be played, it must implement the interface
Natives
.
EventListener
. This
interface provides the critical notifications at the core of the game:
OnMessage(String text)
: This event sends a message from the native layer for
information purposes.
•
•
OnInitGraphics(int w, int h)
: This event gets called when the video graphics
gets initialized. It returns the width and height of the video buffer.
OnImageUpdate(int[] pixels, int x, int y, int w, int h)
: This is perhaps the
most important method out there. It gets called when a video update occurs and
returns an Android ARGB packed array of pixels to be displayed on the device. It
also returns the X and Y coordinates of the top left pixel and the width and height
of the video buffer.
•
OnSysError(String text)
: This event fires when a fatal error occurs. The client
should display the incoming message and terminate; otherwise, the application
will certainly crash.
•
•
OnStartSound(int idx)
: This event fires when a sound must be played.
idx
represents the native sound ID.
•
OnStartMusic(int idx)
: This event fires when background music must be played.
idx
represents the native music ID.
Now, let's look at some of the native code where all the magic occurs.