Game Development Reference
In-Depth Information
class FirstTriangleScreen extends Screen {
GLGraphics glGraphics;
FloatBuffer vertices;
public FirstTriangleScreen(Game game) {
super (game);
glGraphics = ((GLGame)game).getGLGraphics();
ByteBuffer byteBuffer = ByteBuffer. allocateDirect (3 * 2 * 4);
byteBuffer.order(ByteOrder. nativeOrder ());
vertices = byteBuffer.asFloatBuffer();
vertices.put( new float [] { 0.0f, 0.0f,
319.0f, 0.0f,
160.0f, 479.0f});
vertices.flip();
}
The FirstTriangleScreen class holds two members: a GLGraphics instance and our
trusty FloatBuffer , which stores the 2D positions of the three vertices of our triangle. In
the constructor, we fetch the GLGraphics instance from the GLGame and create and fill the
FloatBuffer according to our previous code snippet. Since the Screen constructor gets a Game
instance, we have to cast it to a GLGame instance so that we can use the GLGame.getGLGraphics()
method.
@Override
public void present( float deltaTime) {
GL10 gl = glGraphics.getGL();
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glClear(GL10. GL_COLOR_BUFFER_BIT );
gl.glMatrixMode(GL10. GL_PROJECTION );
gl.glLoadIdentity();
gl.glOrthof(0, 320, 0, 480, 1, -1);
gl.glColor4f(1, 0, 0, 1);
gl.glEnableClientState(GL10. GL_VERTEX_ARRAY );
gl.glVertexPointer( 2, GL10. GL_FLOAT , 0, vertices);
gl.glDrawArrays(GL10. GL_TRIANGLES , 0, 3);
}
The present() method reflects what we just discussed: we set the viewport, clear the screen,
set the projection matrix so that we can work in our custom coordinate system, set the default
vertex color (red in this case), specify that our vertices will have positions, tell OpenGL ES where
it can find those vertex positions, and finally, render our awesome little red triangle.
@Override
public void update( float deltaTime) {
game.getInput().getTouchEvents();
game.getInput().getKeyEvents();
}
Search WWH ::




Custom Search