Game Development Reference
In-Depth Information
verticesBuffer[bufferIndex++] = x3;
verticesBuffer[bufferIndex++] = y3;
verticesBuffer[bufferIndex++] = region.u2;
verticesBuffer[bufferIndex++] = region.v1;
verticesBuffer[bufferIndex++] = x4;
verticesBuffer[bufferIndex++] = y4;
verticesBuffer[bufferIndex++] = region.u1;
verticesBuffer[bufferIndex++] = region.v1;
numSprites++;
}
}
We do the same as in the simpler drawing method, except that we construct all four corner
points instead of just the two opposite ones. This is needed for the rotation. The rest is the same
as before.
What about scaling? We do not explicitly need another method, since scaling a sprite only
requires scaling its width and height. We can do that outside the two drawing methods, so
there's no need to have another bunch of methods for the scaled drawing of sprites.
And that's the big secret behind lightning-fast sprite rendering with OpenGL ES.
Using the SpriteBatcher Class
Now we can incorporate the
TextureRegion
and
SpriteBatcher
classes in our cannon example.
Copy the
TextureAtlas
example and rename it
SpriteBatcherTest
. The classes contained in it
can be called
SpriteBatcherTest
and
SpriteBatcherScreen
.
We get rid of the
Vertices
members in the screen class. We don't need them anymore, since the
SpriteBatcher
will do all the dirty work for us. Instead, we add the following members:
TextureRegion cannonRegion;
TextureRegion ballRegion;
TextureRegion bobRegion;
SpriteBatcher batcher;
We now have a
TextureRegion
for each of the three objects in our atlas, as well as a
SpriteBatcher
.
Next, modify the constructor of the screen. Get rid of all the
Vertices
instantiation and
initialization code, and replace it with a single line of code:
batcher =
new
SpriteBatcher(glGraphics, 100);
That will set our
batcher
member to a fresh
SpriteBatcher
instance that can render 100 sprites
in one batch.