Skip to content
DrJonki edited this page Sep 10, 2014 · 7 revisions

A sprite batch is used to group multiple sprites together and draw them all in one setting. The main motivation for such a system is efficiency. Drawing each object individually can be really slow, depending on the situation.


Initialization

You create a batch just like you'd create a normal game object. SpriteBatch derives from GameObject, so it will offer much of the same functionality. For example, you'll be able to draw a batch using the same Draw call or you can add it onto a layer.

Before you can draw anything with a batch, you need to give it either a Texture or a TextureAtlas. Only one can be active at a time. After supplying the batch with either, you can start adding objects to be drawn.


Usage

SpriteBatch has only one function you need to call to add a sprite: AddSprite. It only has one required argument, which is a transform for an object you want to draw. You can keep a pointer to the transform you added to modify it while it's being used by the batch. All changes will be automatically reflected.

AddSprite also has a few optional arguments:

  1. atlasName - If you're using a TextureAtlas, you can specify the name of the texture to be used.
  2. color - To be used for tinting the texture
  3. texCoords - Custom texture coordinates. The coordinates will be automatically fetched from the atlas if atlasName has been supplied.

Notes

  • By default SpriteBatch adopts the Transform pointers passed to it and deletes them when appropriate. If you want to take care of the pointers yourself, you need to pass false to the SpriteBatch constructor.
Clone this wiki locally