I'm searching a performance problem in the Drawing part of my xna code for windows phone 7 that occures sometimes after some seconds. Is there a best practice how/when to call the SpriteBatch.Begin to draw something? Should it be called for each couple of sprites (in each class when it draws the player, the background, background objects,...) or would a calling in the beginning -> draw everything in all subclasses - be better for the performance?
You should not use them more than necessary, because Begin() means preparing the device for sprite rendering and End() means to restore to its previous state. This may vary through some flags in Begin() telling you want no state changes, and may complicate your code because you would have to manually set some states.
Device state changes are often slow and you should prevent doing them if not required, they also may interfere with anything you're doing, so if you want to render something not in SpriteBatch you should call End(), but if you are rendering sprites you should call it once for all sprites.
To prevent calling End() for modifying states that don't show on the previous sprite, you should call Flush() instead of End(). This renders the queued sprites with the current transformations and device states, but leaves the states intact. What the End() function does is to call Flush() and reset particular states set by Begin().
The best thing performance-wise is to call begin/end only once per Draw(), this causes all of the sprites in a batch to get processed by the graphics card at the same time. You should only use multiple sprite batches if you need to. Examples of when you might need to are:
Can anyone think of an other reason for multiple sprite batches (begin->end)?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With