Game Development Reference
In-Depth Information
/**
* Queue an "event" to be run on the GL rendering thread.
*
* @param r
* the runnable to be run on the GL rendering thread.
*/
public void queueEvent(Runnable r) {
mGLThread.queueEvent(r);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mGLThread.requestExitAndWait();
}
private SurfaceHolder mHolder;
private GLThread mGLThread;
}
Other important methods in the surface view include the following:
•
setRenderer()
: This method creates the inner thread that does all the work and
starts it. The thread keeps a reference to the surface holder available by calling
getHolder()
.
public void setRenderer(Renderer renderer) {
mGLThread = new GLThread(renderer, mHolder);
mGLThread.start();
}
queueEvent(Runnable r)
: This method sends an event to be run by the inner
thread.
•
onDetachedFromWindow()
: This method is called when the view is detached from a
window. At this point, it no longer has a surface for drawing.
•
GL Thread
The main loop of the animation is performed by
GLThread
. When started, this thread performs the
following steps:
1.
It creates a semaphore:
sEglSemaphore.acquire();
guardedRun(); // Only 1 thread can access this code
sEglSemaphore.release();
2.
It runs the critical animation loop. Within the loop, the actual drawing is
delegated to the
CubeRenderer
.