0
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ Use your camera class and only draw the tiles that are within the bounds of the camera. \$\endgroup\$ Commented Oct 18, 2014 at 2:16
  • \$\begingroup\$ I know that, I've already picked up visible area rectangle using camera, how do I decide, which tile is in bounds? \$\endgroup\$
    – Vanguard
    Commented Oct 18, 2014 at 5:38

1 Answer 1

0
\$\begingroup\$

I've found a solution:

public void Draw(SpriteBatch spriteBatch) {
        //Interpreting tiles as bounds 
        //Sub-ing and add-ing 128 just to get more tiles than visible,
        //so the world feels like more alive
        // 16 - is tile size
        // 128 / 16 = 8 more tiles
        int startX = (va.area.Left - 128) / 16;
        int endX = (va.area.Right + 128) / 16;
        int startY = (va.area.Top - 128) / 16;
        int endY = (va.area.Bottom + 128) / 16;

        //Array checks probably could've done better
        if (startX < 0) {
            startX = 0;
        }
        if (startY < 0) {
            startY = 0;
        }

        // mapBounds.width and mapBounds.Height is just array size 
        if (endX > mapBounds.width) {
            endX = mapBounds.width - 1;
        }
        if (endY > mapBounds.height) {
            endY = mapBounds.height - 1;
        }

        //Drawing tiles from array
        //Since I've got transposed matrix, it's j,i
        for (Int32 i = startX; i < endX; i++) {
            for (Int32 j = startY; j < endY; j++) {
                Tile tile = map[j, i];
                if (tile!=null)
                   tile.Draw(spriteBatch);
            }
        }
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .