Game Development Reference
In-Depth Information
Note Every time you modify the C/C++ code, you'll have to rebuild the shared libraries with a
call to ndk-build . If you have more than one source file, and only modified a subset of files, the
build tool will only recompile the files that changed, thereby reducing the compile time. If you need
to make sure all source files are recompiled, just invoke ndk-build clean .
Putting It All Together
We now have everything in place to test our native methods. Let's create a test that invokes
both of the JniUtils methods. We call the class JniUtilsTest , and it extends the GLGame class
and contains a GLScreen implementation, as usual. It simply copies a float[] array into a direct
ByteBuffer and then outputs the contents of the ByteBuffer to LogCat via the other native
method. Listing 13-6 shows the entire code. Don't forget to add it to the NdkStarter class and
the manifest file.
Listing 13-6. JniUtilsTest.java, Testing Our Native Methods
package com.badlogic.androidgames.ndk;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.badlogic.androidgames.framework.impl.GLScreen;
public class JniUtilsTest extends GLGame {
public Screen getStartScreen() {
return new JniUtilsScreen(this);
}
class JniUtilsScreen extends GLScreen {
public JniUtilsScreen(Game game) {
super (game);
float[] values = { 1.231f, 554.3f, 348.6f, 499.3f };
ByteBuffer buffer = ByteBuffer.allocateDirect(3 * 4);
buffer.order(ByteOrder.nativeOrder());
JniUtils.copy(buffer, values, 1, 3);
FloatBuffer floatBuffer = buffer.asFloatBuffer();
for ( int i = 0; i < 3; i++) {
JniUtils.log("JniUtilsTest", Float.toString(floatBuffer.get(i)));
}
}
 
Search WWH ::




Custom Search