Game Development Reference
In-Depth Information
vertices.draw(GL10. GL_TRIANGLES , 0, numSprites * 6);
vertices.unbind();
}
The next method is endBatch() ; we'll call it to finalize and draw the current batch. It first transfers
the vertices defined for this batch from the float array to the Vertices instance. All that's left is
binding the Vertices instance, drawing numSprites × 2 triangles, and unbinding the Vertices
instance again. Since we use indexed rendering, we specify the number of indices to use—
which is six indices per sprite, times numSprites . That's all there is to rendering.
public void drawSprite( float x, float y, float width, float height, TextureRegion region) {
float halfWidth = width / 2;
float halfHeight = height / 2;
float x1 = x - halfWidth;
float y1 = y - halfHeight;
float x2 = x + halfWidth;
float y2 = y + halfHeight;
verticesBuffer[bufferIndex++] = x1;
verticesBuffer[bufferIndex++] = y1;
verticesBuffer[bufferIndex++] = region.u1;
verticesBuffer[bufferIndex++] = region.v2;
verticesBuffer[bufferIndex++] = x2;
verticesBuffer[bufferIndex++] = y1;
verticesBuffer[bufferIndex++] = region.u2;
verticesBuffer[bufferIndex++] = region.v2;
verticesBuffer[bufferIndex++] = x2;
verticesBuffer[bufferIndex++] = y2;
verticesBuffer[bufferIndex++] = region.u2;
verticesBuffer[bufferIndex++] = region.v1;
verticesBuffer[bufferIndex++] = x1;
verticesBuffer[bufferIndex++] = y2;
verticesBuffer[bufferIndex++] = region.u1;
verticesBuffer[bufferIndex++] = region.v1;
numSprites++;
}
The next method, drawSprite() , is the workhorse of the SpriteBatcher class. It takes the x and
y coordinates of the center of the sprite, its width and height, and the texture region to which it
maps. The method's responsibility is to add four vertices to the float array starting at the current
bufferIndex . These four vertices form a texture-mapped rectangle. We calculate the position
of the bottom-left corner ( x1 , y1 ) and the top-right corner ( x2 , y2 ) and use these four variables to
construct the vertices together with the texture coordinates from the TextureRegion . The vertices
are added in counter-clockwise order, starting at the bottom-left vertex. Once they are added to
the float array, we increment the numSprites counter and wait for another sprite to be added or
for the batch to be finalized.
 
Search WWH ::




Custom Search