Search code examples
c#xnaspritedrawdrawablegamecomponent

Drawing all DrawableGameComponents in a single batch


This is an XNA question...

I have a large number of objects that are DrawableGameComponents in my game and they all share the same SpriteBatch. However, I have to call SpriteBatch.Begin() and .End() everytime to draw each component. I want to draw everything in a single batch for better performance but I don't know where .Begin() and .End() can go since the .Draw() is being automatically called (as they are DrawableGameComponents)

Does anyone know what I can do to make them all be drawn between a single .Begin() and .End() call whilst keeping them as DrawableGameComponents?

Edit:

Alright from this website http://www.progware.org/Blog/post/XNA-Game-Development-(Decentralize).aspx I figured that it's called at the main game loop's base.Draw() call... so I can just wrap that with the Begin and End methods

protected override void Draw(GameTime gameTime) {
    if (Cell.Cell.CellSpriteBatch != null) {
        Cell.Cell.CellSpriteBatch.Begin(
            SpriteSortMode.BackToFront, BlendState.AlphaBlend, 
            SamplerState.AnisotropicClamp, null, null, null,
            gamePlay.Camera.GetTransformation()
        );
    }

    base.Draw(gameTime);

    if (Cell.Cell.CellSpriteBatch != null) {
        Cell.Cell.CellSpriteBatch.End();
    }
}

and it works now but doing it this way seems to mean that everything else that I want drawn in a different SpriteBatch will always be drawn below this (as they are drawn first) because the .End() of this SpriteBatch is always going to be called last.

Any solution to this?


Solution

  • Firstly, unless you already know that you are batch limited its probably not worth doing this. If every single .Draw call is pulling from the same texture then you might see some speed up. If they are different textures then switching to a single Begin/End will have no difference. SpriteBatch will break up your draw calls anyway.

    As you found doing this makes your code pretty confusing too - you have to keep track between components.

    The only solution to your current problem is to make the SpriteBatch Immediate so that it draws as you call the draw calls instead of drawing at the End call. However this will kill your perf as no batching is done at all.

    Personally I would stick to a Begin/End per component until you hit a perf problem. Assuming you don't have 5000 components that is in which case you probably need to architect things a little differently.