Game Development Reference
In-Depth Information
Changes to the Original Sample
The class
CubeRenderer
has been modified to accept a Boolean argument in its constructor to request a
native draw (see Listing 5-9).
Listing 5-9.
Changes for CubeRenderer Class
public class CubeRenderer implements Renderer
{
private boolean mNativeDraw = false;
public CubeRenderer(boolean useTranslucentBackground,
boolean nativeDraw)
{
mTranslucentBackground = useTranslucentBackground;
mNativeDraw = nativeDraw;
mCube = new Cube();
}
public void drawFrame(GL10 gl) {
if (mNativeDraw)
doNativeDraw();
else
doJavaDraw(gl);
}
private void doJavaDraw(GL10 gl) {
// Same as before
// ...
}
public void doNativeDraw() {
Natives.NativeRender();
}
// ...
}
When
drawFrame()
is invoked and
mNativeDraw
is true, the cube will be rendered from C (by calling
Natives.NativeRender()
). Otherwise, the Java implementation will be used.
When the surface is created, and a renderer is set for that surface using
GLSurfaceView.setRenderer(Renderer renderer)
, you must tell the native interface class (
Natives.java
)
that you wish to listen for messages by sending a reference to the loop thread:
public void setRenderer(Renderer renderer) {
mGLThread = new GLThread(renderer, mHolder);
mGLThread.start();
Natives.setListener(mGLThread);
}
Note that
GLThread
must implement
Natives.EventListener
for this to work.