Search code examples
c#visual-studiospritebatching

Sprite Batching in Non-OpenGL/XNA Environment


I've been working on a game in Visual C# (Not the best platform, I know), and, as would perhaps be expected, it has started to run rather slowly. Running some tests has shown that the main hold up is in drawing images. I've been told that Sprite Batching is a good fix for that.

Problem is, I can't find anything on sprite batching that isn't specific to XNA or OpenGL. I know little to nothing about the process, and I was hoping to get some information on whether such a thing can be implemented using Visual Studio's Visual C#, and (if so) where I can go to learn more about it. If not, are there any other useful methods of speeding the process up a bit? Thanks!


Solution

  • It basically comes down to batching together calls to save on state switches (textures, fill rate) and draw calls (sending a draw call 50,000 times isn't as efficient as sending a single draw call, surprisingly enough). You're going to have to check, when calling the equivalent of a SpriteBatch.Draw(...), the following:

    • An internal 'max size' of your batch
    • If the texture switches, flush your buffer (i.e. draw whatever you have)
    • If SpriteBatch.End(...) has been called (you're done; flush the buffer and draw)

    If you're still having trouble, feel free to check out MonoGame's implementation.

    Edit: found a great gamedev question about this.