SpriteBatch.java
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    //--Constants--//
00008    final static int VERTEX_SIZE = 4;                  // Vertex Size (in Components) ie. (X,Y,U,V)
00009    final static int VERTICES_PER_SPRITE = 4;          // Vertices Per Sprite
00010    final static int INDICES_PER_SPRITE = 6;           // Indices Per Sprite
00011 
00012    //--Members--//
00013    GL10 gl;                                           // GL Instance
00014    Vertices vertices;                                 // Vertices Instance Used for Rendering
00015    float[] vertexBuffer;                              // Vertex Buffer
00016    int bufferIndex;                                   // Vertex Buffer Start Index
00017    int maxSprites;                                    // Maximum Sprites Allowed in Buffer
00018    int numSprites;                                    // Number of Sprites Currently in Buffer
00019 
00020    //--Constructor--//
00021    // D: prepare the sprite batcher for specified maximum number of sprites
00022    // A: gl - the gl instance to use for rendering
00023    //    maxSprites - the maximum allowed sprites per batch
00024    public SpriteBatch(GL10 gl, int maxSprites)  {
00025       this.gl = gl;                                   // Save GL Instance
00026       this.vertexBuffer = new float[maxSprites * VERTICES_PER_SPRITE * VERTEX_SIZE];  // Create Vertex Buffer
00027       this.vertices = new Vertices( gl, maxSprites * VERTICES_PER_SPRITE, maxSprites * INDICES_PER_SPRITE, false, true, false );  // Create Rendering Vertices
00028       this.bufferIndex = 0;                           // Reset Buffer Index
00029       this.maxSprites = maxSprites;                   // Save Maximum Sprites
00030       this.numSprites = 0;                            // Clear Sprite Counter
00031 
00032       short[] indices = new short[maxSprites * INDICES_PER_SPRITE];  // Create Temp Index Buffer
00033       int len = indices.length;                       // Get Index Buffer Length
00034       short j = 0;                                    // Counter
00035       for ( int i = 0; i < len; i+= INDICES_PER_SPRITE, j += VERTICES_PER_SPRITE )  {  // FOR Each Index Set (Per Sprite)
00036          indices[i + 0] = (short)( j + 0 );           // Calculate Index 0
00037          indices[i + 1] = (short)( j + 1 );           // Calculate Index 1
00038          indices[i + 2] = (short)( j + 2 );           // Calculate Index 2
00039          indices[i + 3] = (short)( j + 2 );           // Calculate Index 3
00040          indices[i + 4] = (short)( j + 3 );           // Calculate Index 4
00041          indices[i + 5] = (short)( j + 0 );           // Calculate Index 5
00042       }
00043       vertices.setIndices( indices, 0, len );         // Set Index Buffer for Rendering
00044    }
00045 
00046    //--Begin Batch--//
00047    // D: signal the start of a batch. set the texture and clear buffer
00048    //    NOTE: the overloaded (non-texture) version assumes that the texture is already bound!
00049    // R: [none]
00050    public void beginBatch()  {
00051       numSprites = 0;                                 // Empty Sprite Counter
00052       bufferIndex = 0;                                // Reset Buffer Index (Empty)
00053    }
00054 
00055    //--End Batch--//
00056    // D: signal the end of a batch. render the batched sprites
00057    // A: [none]
00058    // R: [none]
00059    public void endBatch()  {
00060       if ( numSprites > 0 )  {                        // IF Any Sprites to Render
00061          vertices.setVertices( vertexBuffer, 0, bufferIndex );  // Set Vertices from Buffer
00062          vertices.bind();                             // Bind Vertices
00063          vertices.draw( GL10.GL_TRIANGLES, 0, numSprites * INDICES_PER_SPRITE );  // Render Batched Sprites
00064          vertices.unbind();                           // Unbind Vertices
00065       }
00066    }
00067 
00068    //--Draw Sprite to Batch--//
00069    // D: batch specified sprite to batch. adds vertices for sprite to vertex buffer
00070    //    NOTE: MUST be called after beginBatch(), and before endBatch()!
00071    //    NOTE: if the batch overflows, this will render the current batch, restart it,
00072    //          and then batch this sprite.
00073    // A: x, y - the x,y position of the sprite (center)
00074    //    width, height - the width and height of the sprite
00075    //    region - the texture region to use for sprite
00076    // R: [none]
00077    public void drawSprite(float x, float y, float width, float height, TextureRegion region)  {
00078       if ( numSprites == maxSprites )  {              // IF Sprite Buffer is Full
00079          endBatch();                                  // End Batch
00080          // NOTE: leave current texture bound!!
00081          numSprites = 0;                              // Empty Sprite Counter
00082          bufferIndex = 0;                             // Reset Buffer Index (Empty)
00083       }
00084 
00085       float halfWidth = width / 2.0f;                 // Calculate Half Width
00086       float halfHeight = height / 2.0f;               // Calculate Half Height
00087       float x1 = x - halfWidth;                       // Calculate Left X
00088       float y1 = y - halfHeight;                      // Calculate Bottom Y
00089       float x2 = x + halfWidth;                       // Calculate Right X
00090       float y2 = y + halfHeight;                      // Calculate Top Y
00091 
00092       vertexBuffer[bufferIndex++] = x1;               // Add X for Vertex 0
00093       vertexBuffer[bufferIndex++] = y1;               // Add Y for Vertex 0
00094       vertexBuffer[bufferIndex++] = region.u1;        // Add U for Vertex 0
00095       vertexBuffer[bufferIndex++] = region.v2;        // Add V for Vertex 0
00096 
00097       vertexBuffer[bufferIndex++] = x2;               // Add X for Vertex 1
00098       vertexBuffer[bufferIndex++] = y1;               // Add Y for Vertex 1
00099       vertexBuffer[bufferIndex++] = region.u2;        // Add U for Vertex 1
00100       vertexBuffer[bufferIndex++] = region.v2;        // Add V for Vertex 1
00101 
00102       vertexBuffer[bufferIndex++] = x2;               // Add X for Vertex 2
00103       vertexBuffer[bufferIndex++] = y2;               // Add Y for Vertex 2
00104       vertexBuffer[bufferIndex++] = region.u2;        // Add U for Vertex 2
00105       vertexBuffer[bufferIndex++] = region.v1;        // Add V for Vertex 2
00106 
00107       vertexBuffer[bufferIndex++] = x1;               // Add X for Vertex 3
00108       vertexBuffer[bufferIndex++] = y2;               // Add Y for Vertex 3
00109       vertexBuffer[bufferIndex++] = region.u1;        // Add U for Vertex 3
00110       vertexBuffer[bufferIndex++] = region.v1;        // Add V for Vertex 3
00111 
00112       numSprites++;                                   // Increment Sprite Count
00113    }
00114 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Jun 6 2019 21:20:07