DynamicTileBatch is a system for efficient rendering of tens of thousands textured tiles. Each registered texture holds its own vertex buffer that it uses for rendering. The cost of switching a texture is mitigated by rendering all tiles that use the same texture at once. Only when all tiles are rendered with the first texture, the texture is switched and the system continues with the second batch, and so on.

The system doesn't utilize any custom classes for adding tiles, so there is no problem with excessive class creation and subsequent garbage collections that would hider the game's performance.

To begin, create a DynamicTileBatch object and register it with the SpriteManager.

var dynamicTileBatch = new DynamicTileBatch();

The next step is registering the textures you wish to use and specify how big their vertex buffers are. Registering the texture gives you an Int32 - that is the index of the registered texture. To make sure your performance is as good as possible, avoid needlessly registering a large amount of textures. Instead, it is advised you create a texture atlas - like this one: http://images.wikia.com/pocketgod/images/6/6a/TextureAtlas1.png

You register a texture like this:

// Load a texture (make sure that you change the path of the picture!)
Texture2D ballTexture = FlatRedBallServices.Load(@"redball.png");
// Register the texture.
dynamicTileBatch.RegisterTexture(ballTexture, 255);

This has just registered a texture with which you can draw up to 255 tiles. The index of the first texture is 0, so if you're only using one texture, you don't need to keep the return value.

Now we can finally add a tile. The DynamicTileBatch system probably works differently than what you're used to. To create a default tile do this:


That's not very exciting though, so let's liven it up a little bit. And here's where it gets strange:

dynamicTileBatch.SetPosition(new Vector2(-3, 2));

To avoid having too many method arguments, the system works by setting a state. You basically prepare the tile you want to draw and then simply create it. The AddTile() method also returns an Int32 - the index of the tile you just created. Keep it somewhere, because you can modify an already created tile with it - or delete a specific tile.

To change the tile's position, do this:

dynamicTileBatch.ChangePosition(new Vector3(0, 4, 0));

As you can see, changing existing tiles follows the same pattern as adding new ones. The 0 passed to the UpdateTile method is the tile's index. Or you could always do this, which is the preferred way:

... some settings for the new tile ...
Int32 tileIndex = dynamicTileBatch.AddTile();
// And should you wish to update it later, you can use the index

There is not that much left to say - the other functions have descriptive names and every function is commented and documented, so you should be able to figure it out.

If you have any questions or problems, you can tell me on the forums ("Nokk") or in the chat, where I spend a lot of time.

Product File: 
Syndicate content