kelthar

My very own SpriteBatch (the concept)

Posted in iPhone Development by kelthar on November 20, 2010

Well, I finally got tired of the low perforance of XNATouch when it comes to drawing sprites so I’ve set about an own project of drawing them. The concept is as follows:

  • Each sprite I’m drawing needs to have a base class with it’s position and dimensions. It’s gonna be called Element
  • The Element should also have a notion other types of transformations on it, such as scale and rotation
  • The Element must know what texture it going to be drawn from and the part of the texture (since I’m merging all my textures for faster load times.
  • Since there’s alot of garbage created and discarded when making these calls to the draw, the Element should have a knowledge of it’s own vertices. Today, in XnaTouch, they are created in a List<> and thrown away after usage. At every draw call.
  • The vertices should be recalculated before each draw in the SpriteBatch so their values can be copied over the array in the SpriteBatch.
  • Setting a new texture every time to the shader takes some performance, so the SpriteBatch will copy vertices as long as the texture isn’t changed. Here I will benefit from having texture packs/merged textures as the Texture-parameter of the shader will change more seldom.
  • Drawing the Element should have a possibility for clipping (since I want to create scrolling panels with text and pictures in them).

The rotation itself is presenting a few problems. I finally got the rotation sorted out (I’m totally fresh on the whole Matrix business). The other problem is that the quad expands when the position of the vertices are transformed. I’ll have to look into that one.

Another great point of making the SpriteBatch on my own is that I can specify the Z and therefore easily layer the sprites in more layers than just the two. Making shadows easier w/o having to make a shader. I was forced to learn some shader techniques to create my own since BasicEffect wasn’t working, but that was due to the fact that the vertexdeclaration was shot and needed to be set before every draw call.

When I get it fully working in Xna, I will port it to OpenGLES. It’s damn fun, I feel I’ve learnt alot from this, but unfortunatly my game has been delayed even more.

I’ll post the code and some thought when I have it ready …

Check it out at Vimeo: Blow him up, 2nd prototype

2 Responses

Subscribe to comments with RSS.

  1. jdindia said, on December 9, 2010 at 8:09 pm

    I was annoyed with XnaTouch sprite performance too, so I implemented my own version. I posted a prototype SpriteBatch for XnaTouch as a patch. My code runs much faster than the old version, but I’m still somewhat disappointed.

    • kelthar said, on January 12, 2011 at 11:06 am

      Yeah, it is how it is. I have checked your code and it seems sensible. It looks like the one I made for XNA aswell. Good work!

      I found something that might be an issue though. The culling should be turned off if it isn’t already. That should give another boost!


Leave a comment