9
votes

What is the difference and consequences of using SpriteSortMode as FrontToBack or BackToFront? It's that with FrontToBack the object that has the smaller layer value will be draw first and with BackToFront the object that has the smaller layer value will be draw last?

1

1 Answers

7
votes

They use the layerDepth value that is passed to the sprite draw call. A value of 0 is front, and a value of 1 is back, and everything in between those values.

All of the sorting modes in XNA change when and how the sprites are rendered. FrontToBack and BackToFront are the same as Deferred mode, which means the sprites are only rendered when SpriteBatch.End() is called. If you use FrontToBack or BackToFront then at the call to End all of the sprites are ordered based on their layerDepth, where in FrontToBack the values closer to 0 are drawn first, and vice versa for BackToFront. The implication is that in BackToFront, for instance, the sprites in the back (or, with layerDepths closest to 1) will be drawn first and therefore appear underneath the sprites closer to the front (values closer to 0).

Since it does the sorting for you, these sorting modes are a bit slower than other modes.