Game Development Reference
In-Depth Information
3.
When asked to quit, the loops terminates, and the OpenGL resources are
released.
■
Note
A
semaphore
is an object often used to restrict the number of threads than can access the OpenGL
context. When the Android framework launches a second instance of an activity, the new instance's
onCreate()
method may be called before the first instance returns from
onDestroy()
. A semaphore ensures that only one
instance at a time accesses the GL API. We must do this because OpenGL is a single-threaded API (which means
that only one thread can access the
GLContext
at a time).
Listing 5-4 shows a fragment of the
GLThread
class taken from the GL cubes sample. When the thread
starts, the
run()
method will be invoked, and a semaphore used to ensure that
guardedRun()
can be
accessed by one thread only.
guardedRun()
performs other important steps, such as the following:
•
Initialize the Embedded OpenGL (EGL) for a given configuration specification.
The configuration specification defines information, such as pixel format and
image depth.
•
Create the OpenGL surface and tell the renderer about it.
•
Check if the size of the surface has changed and tell the renderer about it.
•
Queue and get events to be run on the GL rendering thread.
Listing 5-4.
Rendering Thread for the GL Cubes Sample
package opengl.scenes;
// …
/**
* A generic GL Thread. Takes care of initializing EGL and GL.
* Delegates to a Renderer instance to do the actual drawing.
*/
public class GLThread extends Thread
{
public GLThread(Renderer renderer, SurfaceHolder holder) {
super();
mDone = false;
mWidth = 0;
mHeight = 0;
mRenderer = renderer;
mHolder = holder;
setName("GLThread");
}