Game Development Reference
In-Depth Information
With these two variables, the array region is set by calling:
(*env)->
SetIntArrayRegion(env, jImage, 0, iSize, (jint *) data)
, where
data
is an
array of 32 bit integers packed in Android format (ARGB).
In the second file of Listing 6-17, two important functions are called:
•
VL_SetPalette (const byte *palette)
: This is the function that sets the palette
that will be used to pack the pixels in Android format. Wolf 3D uses a 256-color
palette of sequential red, green, and blue (RGB) bytes, (256
•
×
3 for a total of 768
bytes).
•
VW_UpdateScreen()
: With the palette in place, this game callback will fire every
time there is a video update. Here, we simply make a palette lookup using the
global graphics buffer (
gfxbuf
), and the video width and height (
vwidth
and
vheight
) for each byte in
gfxbuf
. Note that
gfxbuf
is an array of bytes that
represent color indexes in the palette:
int size = vwidth * vheight;
int pixels[size], i;
for ( i = 0 ; i < size ; i ++) {
byte colIdx = gfxbuf[i];
pixels[i] = (0xFF << 24)
| (pal[colIdx].red << 16)
| (pal[colIdx].green << 8)
| (pal[colIdx].blue);
}
Finally, the array of pixels is sent to Java along with the video width and height:
jni_send_pixels(pixels,0,0, vwidth, vheight).
Listing 6-17.
Cascading the Video Buffer
// In jni_wolf.c
// g_VM is the global Java VM
void jni_send_pixels(int * data, int x, int y, int w, int h)
{
JNIEnv *env;
if ( !g_VM) {
return;
}
(*g_VM)->AttachCurrentThread (g_VM, (void **) &env, NULL);
// Send img back to java.
if (jSendImageMethod) {
(*env)->SetIntArrayRegion(env, jImage, 0, iSize, (jint *) data);
// Call Java method