kelthar

Better performance with XNATouch

Posted in iPhone Development by kelthar on January 12, 2011

Well, as most ppl know, there are some performance issues with XNATouch right now. These can be circumvented somewhat.

SpriteFonts

SpriteFonts load very slow, this is due to the format the Texture2D is in when it’s loaded. You can circumvent this with this post.

Merge textures

Since the SpriteBatch has been rewritten, it’s much faster. To improve loading speeds, you should merge you textures (.png/.jpg/etc) together in bigger files. Up to 1024*1024 pixels. This will also improve drawing speed since the object/vertex-buffer will be drawn whenever the framework wants to use a new texture.
So … If your Draw() looks like something like this

public pseudo Draw()
{
spriteBatch.Draw(alot of background sprites);
spriteBatch.Draw(alot of different platforms);
spriteBatch.Draw(alot of enemies);
spriteBatch.Draw(player sprites);
spriteBatch.Draw(more foreground sprites);
}

Then you should merge the textures in that order. First all background, if there’s more space in it, merge in the platforms and so on. You shouldn’t split a set (like all platforms OR all enemies) into separate textures since the SpriteBatch will have to load Texture1 when it needs to draw the platform of type A and B, and then jump to Texture2 for type C and D, and then jump back to draw one of type A again. Yeah, you get my drift.

Save resources, avoid garbage collection

Instead of storing all data and game logic inside objects which you create and throw away after they have been used you can create fixed size arrays which holds data for your elements/sprites/game objects.

I used to new Platform() every time I put out a new platform on screen. Now I have a dictionary of the different types of Platforms and it only contains the code needed to Update and Draw the platforms. All the data have been moved out to an array. Look at this pseudo-example:

object PlatformData
{
	bool Active;
	Vector2 Position;
	Type PlatformType;
	bool HasBeenHit;
	bool DeadlyToHit;
	int LowerPlayerHealthBy;
	Color DrawColor;
}

class SomePlatform : Platform
{
	override void Update(GameTime gameTime, PlatformData data)
	{
		// update data, calculate new position, etc
	}

	override void Draw(SpriteBatch spritebatch, PlatformData data)
	{
		// draw the sexy platform
	}
}

class SomeGame : Game
{
	Dictionary PlatformLogic = new Dictionary();
	PlatformData[] PlatformData = new PlatformData[40];

	override Update(GameTime gameTime)
	{
		for (int i=0;i<PlatformData.Length;i++)
		{
			var data = PlatformData[i];
			if (data.Active)
			{
				var logic = Platform[data.PlatformType];
				logic.Update(gameTime, data);
			}
		}
	}
}

PlatformData hold all information about the element. Platform holds all the logic about Update and Draw. And the SomeGame contains references to both.

Tagged with: , , , , ,

Leave a comment