0
\$\begingroup\$

I am working on a desktop game in C#, with Monogame framework. I am storing the tile instances in a 2D Array, for the sake of easy collision detection. But the levels are big (often 100x100 or bigger) and there are lots of tiles out of the screen bounds all the time which I should not draw. At the moment I'm running through the arrays and checking whether they are in the screen bounds or outside, but that may not be too efficient due to the big count of the tiles.

Is there any better way to hide out of bounds tiles?

\$\endgroup\$
1

1 Answer 1

0
\$\begingroup\$

You can determine the range of files you need to draw at first. It will be much faster than check every tile.

Rectangle screenRect = new Rectangle(100, 100, 400, 300);
int tiles[,] = new int[1000, 1000];
int tileSize = 16;

int startCol = (int)(screenRect.X / tileSize);
int endCol = (int)((screenRect.X + screenRect.Width) / tileSize);

int startRow = (int)(screenRect.Y / tileSize);
int endRow = (int)((screenRect.Y + screenRect.Height) / tileSize);

for (int row = startRow; row <= endRow; row++)
{
    for (int col = startCol; col <= endCol; col++)
    {
        // draw
    }
}

Also, if you have realy big world you can split world on chunks (64x64 tiles, for example) and predraw tiles to the render targets. Then refresh it only when tiles changed and check collision of screen bounds with chunk.

\$\endgroup\$

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