Game Development Reference
In-Depth Information
■
Note
As you should know, with JNI, you can invoke subroutines both ways: from Java to C (using the
native
keyword) or from C to Java, as you'll see once we get to the native stuff.
Listing 2-2.
Native Interface Class
package jni;
public class Natives
{
/**
* Native Main Loop
*
* @param argv
* @return
*/
public static native int LibMain(String[] argv);
/**
* This fires on messages from the C layer
*
* @param text
*/
@SuppressWarnings("unused")
private static void OnMessage(String text, int level) {
System.out.println("OnMessage text:" + text + " level=" + level);
}
}
Natives.LibMain
requires a native implementation. On the other hand,
OnMessage
(which is invoked
from C) simply prints the message to standard output. With this in mind, let's take a look at the native
code.
Native Library
Here 11.330is where all the work should take place. We start with the implementation of the actual
library
lib.c
(see Listing 2-3). This file lives in the native folder within the project folder.
■
Note
Native libraries in Linux (also known as shared objects) are the equivalents of dynamic link libraries (DLLs)
in Windows. By convention, shared objects are named as
lib<NAME><VERSION>.so
.