When I first created my code, there were quite a few cases where I would get horizontal and vertical gaps between tiles when displayed. I put this down to rounding issues with float
s and just moved on to other things. Now that I've revisited the issue, I can't seem to get it working properly, even after trying my hardest to eliminate places where this sort of issue might happen. It seems to only happen when I place the camera at certain positions, which is why I think it's likely to be a float
issue. If anyone could help me with the correct way to draw the tile layout, it'd be much appreciated.
This is my drawing method for the map:
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
var topLeft = Camera.ScreenCoordinateToWorld(new Vector2(0, 0));
// Readjust the position of the topLeft element so we get the actual
// drawing position for the tile here
topLeft = new WorldCoordinate(topLeft.X - 1, topLeft.Y - 1,
-TileSize / 2, -TileSize / 2);
var topLeftDrawPosition = topLeft.DrawPosition;
var screenWidthInTiles = Game.GraphicsDevice.Viewport.Width / TileSize + 1;
var screenHeightInTiles = Game.GraphicsDevice.Viewport.Height / TileSize + 1;
for (var y = topLeft.Y; y <= topLeft.Y + screenHeightInTiles; y++)
{
for (var x = topLeft.X; x <= topLeft.X + screenWidthInTiles; x++)
{
if (x < 0 || y < 0 || y >= MapData.Count || x >= MapData[y].Count)
{
// Ignore out of range tiles to avoid exceptions
continue;
}
var tile = MapData[y][x];
var drawPosition = topLeftDrawPosition +
new Vector2((x - topLeft.X)*TileSize,
(y - topLeft.Y)*TileSize);
Tileset[tile].Draw(spriteBatch, drawPosition);
}
}
base.Draw(gameTime, spriteBatch);
}
Broken:
Semi-working: