Game Development Reference
In-Depth Information
Listing 11-8. Modifying the onDrawFrame() Function
String TestJNIString = RobsstringFromJNI();
Log.e("RENDERER" , "JNI STRING = " + TestJNIString);
Run the program. You should see debug statements in your Log window. The final output of the log
statement is shown in Figure 11-10 .
Figure 11-10. JNI test results in Log window
Hands-on Example: Adding Native Functions to the Drone
Grid Game Case Study
This hands-on example will demonstrate how to convert parts of our Drone Grid game from Java
code to native C code and how to call Java functions from native C code.
Calculating Gravity in Native Code
The gravity calculations can be modified in our Drone Grid game so that the actual gravity
calculation is compiled in C and executed in native code on the Android. To do this, we have to
modify the hello-jni.c file and the Physics class.
Modifying the hello-jni.c File
The hello-jni.c file must be modified to include C source code.
The Gravity variable holds the value of the acceleration of gravity for our 3D game world.
float Gravity = 0.010f;
The ApplyGravityToObjectNative() function calculates the new acceleration of an object after the
acceleration from gravity in our game is applied. This new acceleration is then returned. (See Listing 11-9.)
Listing 11-9. Calculating the New Y Acceleration of an Object
jfloat
Java_com_robsexample_glhelloworld_Physics_ApplyGravityToObjectNative(JNIEnv* env,
jobject thiz,
jfloat YAccel)
{
YAccel = YAccel - Gravity;
return YAccel;
}
 
Search WWH ::




Custom Search