Game Development Reference
In-Depth Information
OpenGL ES are almost always RGBA, where each component has a range between 0 and 1.
There are ways to define a color in, say, RGB565, but for now, let's stick to the floating-point
representation. We could set the color used for clearing only once and OpenGL ES would
remember it. The color we set with
glClearColor()
is one of OpenGL ES's states.
The next call actually clears the screen with the clear color we just specified. The method
glClear()
takes a single argument that specifies which buffer to clear. Besides the framebuffer,
OpenGL has a few more buffers it works with . You'll get to know them in Chapter 10, but
for now, all we care about is the framebuffer that holds our pixels, which OpenGL ES calls the
color buffer
. To tell OpenGL ES that we want to clear that exact buffer, we specify the constant
GL10.GL_COLOR_BUFFER_BIT
.
OpenGL ES has a lot of constants, which are all defined as static public members of the
GL10
interface. Like the methods, each constant has the prefix
GL_
.
So, that was our first OpenGL ES application. We'll spare you the impressive screenshot, since
you probably know what it looks like.
Note
Thou shalt never call OpenGL ES from another thread! First and last commandment! The
reason is that OpenGL ES is designed to be used in single-threaded environments only, and it is
not thread-safe. It can be made to work somewhat on multiple threads, but many drivers have
problems with this, and there's no real benefit to doing so.
GLGame: Implementing the Game Interface
In the previous chapter, we implemented the
AndroidGame
class, which ties together all the
submodules for audio, file I/O, graphics, and user input handling. We want to reuse most of
this for our upcoming 2D OpenGL ES game, so let's implement a new class called
GLGame
that
implements the
Game
interface we defined earlier.
The first thing you will notice is that you can't possibly implement the
Graphics
interface with
your current knowledge of OpenGL ES. Here's a surprise: you won't implement it. OpenGL
does not lend itself well to the programming model of your
Graphics
interface; instead, we'll
implement a new class,
GLGraphics
, which will keep track of the
GL10
instance we get from the
GLSurfaceView
. Listing 7-2 shows the code.
Listing 7-2. GLGraphics.java; Keeping Track of the GLSurfaceView and the GL10 Instance
package
com.badlogic.androidgames.framework.impl;
import
javax.microedition.khronos.opengles.GL10;
import
android.opengl.GLSurfaceView;
public class
GLGraphics {
GLSurfaceView glView;
GL10 gl;