Here's a little experiment you can do yourself to see why using a texture atlas (or tileset) is a good idea.
The goal is to draw a checker pattern using a white.png
texture and a black.png
texture. The result should turn out something like this:
First, load your two textures into the game:
_black = Content.Load<Texture2D>("black");
_white = Content.Load<Texture2D>("white");
Then write the code to draw the checkerboard pattern. Something like this will do nicely:
var isWhite = true;
_spriteBatch.Begin(samplerState: SamplerState.PointClamp);
for (var x = 0; x < 450; x++)
{
for (var y = 0; y < 450; y++)
{
if(isWhite)
_spriteBatch.Draw(_white, new Vector2(x * _white.Width, y * _white.Height), Color.White);
else
_spriteBatch.Draw(_black, new Vector2(x * _black.Width, y * _black.Height), Color.White);
isWhite = !isWhite;
}
isWhite = !isWhite;
}
_spriteBatch.End();
The point of using a checkerboard pattern like this is to force the SpriteBatch
to continually switch textures during rendering. This simulates what's going to happen if you draw all your tiles on different .png
files.
Next you need a way to measure the frame rate. You can make a quick and dirty FPS counter by declaring a couple of member variables in your game class like this:
private float _clock;
private int _frameCounter;
And then throw this code in your Draw
method**:
_clock += (float)gameTime.ElapsedGameTime.TotalSeconds;
_frameCounter++;
if (_clock >= 1)
{
Console.WriteLine(_frameCounter);
_frameCounter = 0;
_clock = 0;
}
** Note: this really is a quick and dirty FPS counter. Technically some of that code should be in your Update
method but it'll do for this purpose.
When you run the game you should start seeing the frame rate spew out into the console (in Visual Studio this will be in the "Output" window).
However, all this texture switching will cause the frame rate to drop dramatically. On my computer I could only draw a 150x150 (22,500 tiles) checkerboard before my frame rate suffered. Depending on your graphics card, you might have to fiddle with the numbers to see the same effect.
Finally, you can simulate what it would be like if you used a tileset instead. Simply change the code to only use the white.png
texture like this:
if(isWhite)
_spriteBatch.Draw(_white, new Vector2(x * _white.Width, y * _white.Height), Color.White);
else
_spriteBatch.Draw(_white, new Vector2(x * _white.Width, y * _white.Height), Color.Black);
Now, on my computer I can easily draw a 450x450 (202,500 tiles) checkerboard without breaking a frame rate sweat. That's a pretty significant difference.
Now that we know there's an advantage to using a tileset keep in mind that I could still render 22,500 tiles using 2 textures instead of one. Sometimes if you're only making a small game this will do just fine.
Sometimes it's okay to take some development shortcuts if it makes your life easier. It's always good to know these things but don't bend yourself backwards trying to do something if it's not really achieving anything in your game. At the end of the day, if your game runs at 60 FPS you're all good.