I'm trying to make a simple 2d tile engine here, and I've got a problem with getting a proper cells from a tile grid map when I'm trying to cull off tiles which aren't needed to be drawn.
I've got a tile which is 16 by 16 pixels and a tilemap represented as a grid of 0, 1 etc
For example
{1,1,1,1}
{1,1,1,1}
{1,1,1,1}
{1,1,1,1}
Then I translate it to a Tile[,] matrix like this
public void SetMap(Int32[,] tileCodes, int sizeX, int sizeY, TileTextures tileTextures) {
//A simple check that sizes are equal
if (sizeX == mapBounds.width && sizeY == mapBounds.height) {
for (Int32 i = 0; i < mapBounds.width; i++) {
for (Int32 j = 0; j < mapBounds.height; j++) {
if ((TileType) tileCodes[j, i] != 0)
map[j, i] = new Tile(new Vector2(i, j), (TileType) tileCodes[j, i], tileTextures.GetTexture((TileType) tileCodes[j, i]));
}
}
} else
Console.WriteLine("Map.SetMap: Unable to set map of diffent size than defined one");
}
So it's basically tile grid now (tile[x,y]), but I can't figure out, which tile I should draw. I've already got a vissible area as a rectangle (va):
public void Draw(SpriteBatch spriteBatch) {
// How to get positions in tile[x,y]?
int startX = va.area.Left/ 16;
int endX = va.area.Right/16;
int startY = va.area.Top/16;
int endY = va.area.Bottom/16;
for (Int32 i = startX; i < endX; i++) {
for (Int32 j = startY; j < endY; j++) {
Tile tile = map[i, j];
tile.Draw(spriteBatch);
}
}
}
Any ideas?