tags:

views:

822

answers:

3

In XNA, when is it appropriate to render your game content using more than one spritebatch? So far I've never used more than one, and have never noticed any downsides to this approach.

In what situations would using multiple spritebatches be recommended or even necessary?

+4  A: 

There are very few cases in which using multiple SpriteBatches is necessary. One example that I see thrown around a lot is a situation in which you want to apply different post-processing/renderstates to different sets of sprites, which requires a separate SpriteBatch for each set.

It is usually considered good practice to use only one batch if at all possible, since drawing as many things as possible in one batch is much more performant than spreading the work across multiple batches. Additionally, since you can only control sort order within a single SpriteBatch, it may make it harder (or impossible) to control depth between sprites from different batches.

In short: there are conceivable situations in which you may want to do this, but it's not all that common and in general you should stick to using one SpriteBatch unless you know that you can't accomplish what you want without using more than one.

rmz
A: 

It also may be easier in cases with DrawableGameComponents to have a SpriteBatch per DrawableGameComponent instead of trying to pass this around.

Bill Reiss
+1  A: 

Here is a good example of when to use more than 1 SpriteBatch (Shawn's blog is--as usual--the definitive source).

SpriteBatch doesn't actually do any drawing until you call the End method. That means you can begin several overlapping batches with different settings, draw sprites in any order using any combination of the batch instances, and control what order the entire contents of each batch get drawn by the ordering of your End calls.

Note that this example is about optimizing performance for a scene with alpha-blending and sprites sorted by depth. When (or rather, if) you run into performance problems, there might be something to gain by using multiple SpriteBatches. Until then, you're probably better off keeping it simple.

zakvdm