Game Development Reference
In-Depth Information
of this by calling our listener's
onSurfaceCreated()
method. This would all be well and good if
not for a single problem: all the OpenGL ES states that we've set so far will be lost. This also
includes things such as textures, which we'll have to reload. This problem is known as a
context
loss
. The word
context
stems from the fact that OpenGL ES associates a context with each
surface we create, which holds the current states. When we destroy that surface, the context is
lost as well. It's not all that bad though, given that we design our games properly to handle this
context loss.
Note
Actually, EGL is responsible for context and surface creation and destruction. EGL is another
Khronos Group standard; it defines how an operating system's UI works together with OpenGL ES,
and how the operating system grants OpenGL ES access to the underlying graphics hardware. This
includes surface creation as well as context management. Since
GLSurfaceView
handles all the
EGL stuff for us, we can safely ignore it in almost all cases.
Following tradition, let's write a small example that will clear the screen with a random color each
frame. Listing 7-1 shows the code. The listing is split up, with comments intermingled.
Listing 7-1. GLSurfaceViewTest.java; Screen-Clearing Madness
package
com.badlogic.androidgames.glbasics;
import
java.util.Random;
import
javax.microedition.khronos.egl.EGLConfig;
import
javax.microedition.khronos.opengles.GL10;
import
android.app.Activity;
import
android.opengl.GLSurfaceView;
import
android.opengl.GLSurfaceView.Renderer;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.Window;
import
android.view.WindowManager;
public class
GLSurfaceViewTest
extends
Activity {
GLSurfaceView glView;
public void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.
FEATURE_NO_TITLE
);
getWindow().setFlags(WindowManager.LayoutParams.
FLAG_FULLSCREEN
,
WindowManager.LayoutParams.
FLAG_FULLSCREEN
);
glView =
new
GLSurfaceView(
this
);
glView.setRenderer(
new
SimpleRenderer());
setContentView(glView);
}