5
\$\begingroup\$

Does SpriteBatch in XNA draw to the screen only when a sprite would appear on the screen?

Say I have 5 identical sprites placed exactly on top of each other, is SpriteBatch able to determine that it only needs to draw one?

Also, say I have a large map that I've tiled off the screen in every direction. Does SpriteBatch handle that it does not need to draw these sprites that are off the screen.

(Note: Yes my code should handle this, I am just curious)

\$\endgroup\$
1

2 Answers 2

4
\$\begingroup\$

Say I have 5 identical sprites placed exactly on top of each other, is SpriteBatch able to determine that it only needs to draw one?

Is it able to? Sure -- it has all the information that would allow it to make that determination. However, it won't do so, because such a computation is non-trivial in general and would actually drastically degrade the performance of the SpriteBatch interface.

If enabled, the depth test will kill off fragments that shouldn't be seen or overdraw will hide them, but in both cases the sprite vertices will have been transformed completely through the pipeline already.

Also, say I have a large map that I've tiled off the screen in every direction. Does SpriteBatch handle that it does not need to draw these sprites that are off the screen.

It won't attempt to cull enqueued sprites against the view at all, either. They'll be clipped out entirely during the transformation stage like any other geometry, but it's vastly less expensive to simply never enqueue a sprite you know will not be drawn because it is out of view.

SpriteBatch is really just a wrapper around a dynamic vertex buffer that may or may not do some sorting for you to minimize texture stage changes and such. If your curious you can use Reflector to poke around at the internals and see that it does very little besides manage the buffer for you.

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

No, sprite batch does not handle the first instance (drawing sprites over one another). Anyway, this isn't always the desired result. Sometimes you'd want the images to merge because of transparency with alpha blending.

I'm pretty sure sprite batch doesn't clip images that aren't in the view port. I'm not sure if they are actually drawn, but you shouldn't batch them up in the sprite batch. It takes more time and from your question I can tell you've got some kind of tile engine.

If you told sprite batch to draw every tile on the map instead of the ones on the screen, your game would run a lot slower (major frame rate hit) just because of the sheer amount of tiles there could be.

It's not a good idea to tell the sprite batch to draw things that won't be displayed. However, I wouldn't really differentiate between if a sprite is completely covered by other sprites, especially if your using transparency and alpha blending. I think it's just a tad over kill.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .