Go to the documentation of this file.00001 package uk.co.blogspot.fractiousg.texample;
00002
00003 import javax.microedition.khronos.opengles.GL10;
00004
00005 public class SpriteBatch {
00006
00007
00008 final static int VERTEX_SIZE = 4;
00009 final static int VERTICES_PER_SPRITE = 4;
00010 final static int INDICES_PER_SPRITE = 6;
00011
00012
00013 GL10 gl;
00014 Vertices vertices;
00015 float[] vertexBuffer;
00016 int bufferIndex;
00017 int maxSprites;
00018 int numSprites;
00019
00020
00021
00022
00023
00024 public SpriteBatch(GL10 gl, int maxSprites) {
00025 this.gl = gl;
00026 this.vertexBuffer = new float[maxSprites * VERTICES_PER_SPRITE * VERTEX_SIZE];
00027 this.vertices = new Vertices( gl, maxSprites * VERTICES_PER_SPRITE, maxSprites * INDICES_PER_SPRITE, false, true, false );
00028 this.bufferIndex = 0;
00029 this.maxSprites = maxSprites;
00030 this.numSprites = 0;
00031
00032 short[] indices = new short[maxSprites * INDICES_PER_SPRITE];
00033 int len = indices.length;
00034 short j = 0;
00035 for ( int i = 0; i < len; i+= INDICES_PER_SPRITE, j += VERTICES_PER_SPRITE ) {
00036 indices[i + 0] = (short)( j + 0 );
00037 indices[i + 1] = (short)( j + 1 );
00038 indices[i + 2] = (short)( j + 2 );
00039 indices[i + 3] = (short)( j + 2 );
00040 indices[i + 4] = (short)( j + 3 );
00041 indices[i + 5] = (short)( j + 0 );
00042 }
00043 vertices.setIndices( indices, 0, len );
00044 }
00045
00046
00047
00048
00049
00050 public void beginBatch() {
00051 numSprites = 0;
00052 bufferIndex = 0;
00053 }
00054
00055
00056
00057
00058
00059 public void endBatch() {
00060 if ( numSprites > 0 ) {
00061 vertices.setVertices( vertexBuffer, 0, bufferIndex );
00062 vertices.bind();
00063 vertices.draw( GL10.GL_TRIANGLES, 0, numSprites * INDICES_PER_SPRITE );
00064 vertices.unbind();
00065 }
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 public void drawSprite(float x, float y, float width, float height, TextureRegion region) {
00078 if ( numSprites == maxSprites ) {
00079 endBatch();
00080
00081 numSprites = 0;
00082 bufferIndex = 0;
00083 }
00084
00085 float halfWidth = width / 2.0f;
00086 float halfHeight = height / 2.0f;
00087 float x1 = x - halfWidth;
00088 float y1 = y - halfHeight;
00089 float x2 = x + halfWidth;
00090 float y2 = y + halfHeight;
00091
00092 vertexBuffer[bufferIndex++] = x1;
00093 vertexBuffer[bufferIndex++] = y1;
00094 vertexBuffer[bufferIndex++] = region.u1;
00095 vertexBuffer[bufferIndex++] = region.v2;
00096
00097 vertexBuffer[bufferIndex++] = x2;
00098 vertexBuffer[bufferIndex++] = y1;
00099 vertexBuffer[bufferIndex++] = region.u2;
00100 vertexBuffer[bufferIndex++] = region.v2;
00101
00102 vertexBuffer[bufferIndex++] = x2;
00103 vertexBuffer[bufferIndex++] = y2;
00104 vertexBuffer[bufferIndex++] = region.u2;
00105 vertexBuffer[bufferIndex++] = region.v1;
00106
00107 vertexBuffer[bufferIndex++] = x1;
00108 vertexBuffer[bufferIndex++] = y2;
00109 vertexBuffer[bufferIndex++] = region.u1;
00110 vertexBuffer[bufferIndex++] = region.v1;
00111
00112 numSprites++;
00113 }
00114 }