3
\$\begingroup\$

I'm using libGDX and I'm confused about the difference between the Sprite and SpriteBatch.

Anybody can explain it to me in a simple way?

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

A Sprite is simply a data structure that contains information on how and where to draw a texture (i.e. position, color, texture handle, rotation, scale).

Because of the way that OpenGL works, it is best to group or "batch" draw calls together to get the optimum performance. This grouping is why you will get an error if you try to draw to a Batch without first calling batch.begin() as this signals the Batch to start the work of grouping draw calls. When you call draw() on a batch, it isn't actually doing the work of drawing yet, it is only organizing the data you give it. It will wait to draw to the screen until batch.end() is called.

A SpriteBatch is a Batch which is used for drawing Sprites, more specifically, it deals with drawing a bunch of textures on Quads in OpenGL. In a SpriteBatch there is a lot of interaction behind the scenes with OpenGL that, thankfully, we don't have to do manually. There are a few parameters that SpriteBatch shares among all the sprites that are drawn to it, namely: projection and transformation matrices, tint color, shaders and blend settings.

You don't actually have to use the Sprite class to use a SpriteBatch. You can just give your own parameters to any of the draw calls that SpriteBatch provides. For convenience, the Sprite class has a draw(Batch batch) function that will forward all of the data in the Sprite to the approriate draw calls in the Batch

\$\endgroup\$
-1
\$\begingroup\$

A Sprite is a single sprite, whereas a SpriteBatch is a collection of multiple sprites, all drawn using the same settings.

\$\endgroup\$

You must log in to answer this question.