Game Development Reference
In-Depth Information
The
log()
method mimics the Android
Java Log.logd()
method. It takes a tag and a message
that will get printed to LogCat.
The
copy()
method is actually useful. In Chapter 8, we investigated the performance problems
for the
FloatBuffer.put()
method. We resorted to using a pure Java implementation that
used
IntBuffer
and some nasty tricks so we could speed up the copying of a
float array
to
a direct
ByteBuffer
in the
Vertices
class. We'll now implement a method that takes a direct
ByteBuffer
and a
float
array and copies the array to the buffer. This is a lot faster than using the
corresponding Java APIs. We can later modify our
Vertices
and
Vertices3
classes to use this
new functionality.
Note that both methods are static methods instead of instance methods. This means we can
call them without having an instance of the class
JniTest
! This also has minor effects on our
C signatures, as we'll see in a bit.
Creating the C/C++ Header and Implementation
The first thing we do when we start to write the C/C++ code is generate the header file via the
javah
JDK command-line tool. It takes a few parameters that are useful to us:
The name of the output file, which in our case is
jni/jniutils.h
. The
javah
tool will create the
jni/
folder for us if it doesn't exist yet.
The path containing the
ï?®
ï?®
.class
file of the Java class for which it should
generate a C header. This will be
bin/classes
if we invoke
javah
from the
root directory of our project. It's the output path for the Eclipse compiler
when it compiles any of our Android project's source files.
ï?®
com.badlogic.
androidgames.ndk.JniUtils
in our case.
Open the terminal or command prompt and navigate to the root folder of the Android project.
Make sure that the NDK and JDK are in your
$PATH
as described earlier. Now execute the
following command:
The fully qualified name of the class, which is
javah -o jni/jniutils.h -classpath bin/classes com.badlogic.androidgames.ndk.JniUtils
This will create a file called
jniutils.h
in the
jni/
folder of our Android project. Listing 13-2
shows its content.
Listing 13-2. jniutils.h, Containing the C Functions That Implement Our Native Methods
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
<jni.h>
/* Header for class com_badlogic_androidgames_ndk_JniUtils */
#ifndef
_Included_com_badlogic_androidgames_ndk_JniUtils
#define
_Included_com_badlogic_androidgames_ndk_JniUtils
#ifdef __cplusplus
extern
"C" {