SpriteBatches - how many sprites?
Hi!
I'm trying XNA, and it seems good and easy to use, but a bit slow...!
I'm looping through tiles (currently 30x30 = 900 32x32 pixel tiles - ignoring clipping). I'm not doing anything complicated - and using raw DirectX this just works fine, but this drops to ~ 12FPS with XNA.
If I comment out the spriteBatch.Draw() line, it's fine. So I'm guessing that expecting to call this 900 times per frame is just unrealistic.
How many sprites would a normal game have (moving) on screen at a time? Most of these 900 would be 'static' - i.e. I could draw once and be done with it, but I'm now more curious than anything! How does DX do it so fast?!
Thanks,
muzz
[677 byte] By [
acemuzzy] at [2007-12-26]
It's not the number of calls to SpriteBatch.Draw that affects performance, as much as the number of calls that SpriteBatch must make to the GraphicsDevice.DrawIndexedPrimitive method that underlies it. (SpriteBatch used to be built on ID3DXSprite in beta1, but now is built right on top of XNA).
When SpriteBatch is in Immediate mode, it has to "Flush()" whatever it has queued up whenever the Texture of a sprite is different then the last sprite drawn. If you are alternating between lots of different textures for you sprites, performance will be bad.
In order to improve performance, put all of those sprites into a single big texture, and then draw them using the Draw methods that take a srcRectangle parameter. If you do this, then all of your sprites can be rendered with a single Direct3D call.