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?
Share
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().