# Culling tiles in a tilemap

This topic is 5113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I've created a tilemap that consists of for instance 500 tiles horizontally and 500 tiles vertically. When I render this, I got like.. 2 fps or something, so obviously something have to be done. So I figured I could increase the performance a LOT if the loop where I render the tilemap does only go through the tiles that are inside the border of the screen. If the map are 500x500 tiles, the loop would loop 250000 times, but if only for instance 10x10 tiles is visible at the time, the loop would loop just 100 times, giving the game a huge performance boost. Here's the rendering loop:

int xmin = 0;
int xmax = m_nWidth;
int ymin = 0;
int ymax = m_nHeight;

for (x = xmin; x < xmax; x++)
{
for (y = ymin; y < ymax; y++)
{
for (z = 0; z < m_nLayers; z++)
{
//render the tile
}
}
}

So the question is: How do I calculate xmin, xmax, ymin and ymax automatically, if I got the camera position and zoom value (I use orthographic projection (OpenGL), so therefore the camera has no height value, just x and y position)? Btw, here's how I set the projection matrix, if that is of any help:
//nWidth and nHeight are the width and height of the window
float p_fAspect = (float)nWidth / (float)nHeight;

if (nWidth > nHeight)
{
glOrtho(-m_dZoom * p_fAspect, m_dZoom * p_fAspect, -m_dZoom, m_dZoom, 1.0, -1.0);
}
else
{
glOrtho(-m_dZoom, m_dZoom, -m_dZoom / p_fAspect, m_dZoom / p_fAspect, 1.0, -1.0);
}

Thanks in advance for any help :)

##### Share on other sites
Ahh... I figured it out at last, after several hours.

In case anyone is interested in how I did it, I'll post the code for calculating the xmin/ymin and xmax/ymax values here:

float xmin;float ymin;float xmax;float ymax;if (aspect > 1.0f){	xmin = (camx - (zoom * aspect)) / m_nTilesize;	ymin = (-camy - zoom) / m_nTilesize;// * aspect;	xmax = xmin + (((zoom * 2) * aspect) / m_nTilesize) + 1;	ymax = ymin + ((zoom * 2) / m_nTilesize) + 1;}else{	xmin = (camx - zoom) / m_nTilesize;	ymin = (-camy - (zoom / aspect)) / m_nTilesize;	xmax = xmin + ((zoom * 2) / m_nTilesize) + 1;	ymax = ymin + (((zoom * 2) / aspect) / m_nTilesize) + 1;}if (xmin < 0.0f) xmin = 0.0f;if (xmin > (float)m_nWidth) xmin = (float)m_nWidth;if (ymin < 0.0f) ymin = 0.0f;if (ymin > (float)m_nHeight) ymin = (float)m_nHeight;if (xmax < 0.0f) xmax = 0.0f;if (xmax > (float)m_nWidth) xmax = (float)m_nWidth;if (ymax < 0.0f) ymax = 0.0f;if (ymax > (float)m_nHeight) ymax = (float)m_nHeight;

Also, if anyone at all sees anything that is... not right/got any tips to do things better, please let me know. :)

edit: the "aspect" variable is the aspect ratio of the window (width / height, for instance 640 / 480 or 1024 / 768)

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632995
• Total Posts
3009774
• ### Who's Online (See full list)

There are no registered users currently online

×