[ Pobierz całość w formacie PDF ]

imagestream.close();
imagestream = null;
} catch (IOException e) {
}
}
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL TEXTURE 2D, textures[0]);
gl.glTexParameterf(GL10.GL TEXTURE 2D, GL10.GL TEXTURE MIN FILTER,
GL10.GL NEAREST);
gl.glTexParameterf(GL10.GL TEXTURE 2D, GL10.GL TEXTURE MAG FILTER,
GL10.GL LINEAR);
GLUtils.texImage2D(GL10.GL TEXTURE 2D, 0, bitmap, 0);
bitmap.recycle();
}
}
In the next section, you will add the class for the bricks. Later in the chapter, you
add the code that instantiates and calls all of the classes together.
Creating the Brick Class
Just like the player paddle and the background, you need a class that will
represent your bricks. The class for the bricks is going to be a little different,
however. Because you are going to use a spritesheet with the bricks class, you
44 CHAPTER 5: Creating the Player Character and Obstacles
will not call loadTexture() the same way that you would with the background
and the player paddle. Just to give you some perspective on how flexible the
code can be, you are going to load all of the spritesheets into an array and pass
them together. Therefore, we are going to remove the loadTexture() method
and create a new class to handle spritesheet textures.
The code doesn t have to be written this way; rather, because this book is a
teaching tool, I am trying to show you different ways to do things. This is simply
a good place to take a look at a different way of getting something done. Feel
free, after you learn the differences, to use whichever method of texture loading
you feel is best for your situation.
First, create a new class called PBBricks. The code for PBBricks is shown in
Listing 5-2.
Listing 5-2. PBBrick
PBBRick
package com.jfdimarzio;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
public class PBBrick {
public float posY = 0f;
public float posX = 0f;
public float posT = 0f;
public boolean isDestroyed = false;
public int brickType = 0;
private FloatBuffer vertexBuffer;
private FloatBuffer textureBuffer;
private ByteBuffer indexBuffer;
private float vertices[] = {
0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, .25f, 0.0f,
0.0f, .25f, 0.0f,
};
CHAPTER 5: Creating the Player Character and Obstacles 45
private float texture[] = {
0.0f, 0.0f,
0.25f, 0.0f,
0.25f, 0.25f,
0.0f, 0.25f,
};
private byte indices[] = {
0,1,2,
0,2,3,
};
public PBBrick(int type) {
brickType = type;
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);
}
public void draw(GL10 gl, int[] spriteSheet) {
gl.glBindTexture(GL10.GL TEXTURE 2D, spriteSheet[0]);
gl.glFrontFace(GL10.GL CCW);
gl.glEnable(GL10.GL CULL FACE);
gl.glCullFace(GL10.GL BACK);
gl.glEnableClientState(GL10.GL VERTEX ARRAY);
gl.glEnableClientState(GL10.GL TEXTURE COORD ARRAY);
gl.glVertexPointer(3, GL10.GL FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL FLOAT, 0, textureBuffer);
gl.glDrawElements(GL10.GL TRIANGLES, indices.length,
GL10.GL UNSIGNED BYTE, indexBuffer);
gl.glDisableClientState(GL10.GL VERTEX ARRAY);
gl.glDisableClientState(GL10.GL TEXTURE COORD ARRAY);
gl.glDisable(GL10.GL CULL FACE);
}
}
46 CHAPTER 5: Creating the Player Character and Obstacles
Caution Pay close attention to the code in bold. It is different from the other
draw() methods that you have created and it is important for loading the
correct texture later in the chapter.
Since the PBBrick is using a spritesheet for its texture as is the PBBall,
which you will create later in the chapter you need to make a new class
that handles the texture loading for you.
Create a new class called PBTextures. The PBTextures class holds an array of
textures, and serves up the correct one to the proper class that needs it. You [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • thierry.pev.pl
  •