I’m creating a Tilemap class for my game that uses a 2D array of instances of a Tile class I created.
It works exactly as one would expect; I run a for loop inside a for loop to get the indices and pass it into a Draw method. I want to have thousands of tiles in the game at once like Terraria or similar games - not necessarily updating them, just having them in memory. Memory isn’t really a problem, the problem I’m running into is that drawing them all at once is too slow. Since I obviously don’t want to draw tiles offscreen, I check if they’re offscreen before drawing them by creating a rectangle with for the tile, and just check if that intersects with a rectangle I have representing my camera’s viewport. The problem is, checking whether or not to draw them and then only drawing the tiles onscreen is far SLOWER than just drawing them all.
I tried grouping the tiles into regions, using a 3D array, so that I didn’t have to go through every tile, just every 100 or so, but I couldn’t figure out how to implement that. Basically I am asking: What would be the fastest way to have thousands of tiles, but only draw the ones on the screen, whithout using all my cpu?
Any help would really be appreciated.