How do I determine if a tile is a slope based on the tile image?

In my game, every tile is a 32x32 texture. All the slopes are a 0 - 45 degree angle. I would like to determine, at the time I load the tile, if the is sloped by examining its texture/bitmap data. How can I do this?

• Can you describe exactly where you would use this? Are you trying to do this at runtime or at load time only? – David Gouveia Jan 2 '12 at 2:13
• It also depends on the nature of your tiles, so describe them in more detail too. What's the range of variation on the content of non-slope tiles? Either way, most solutions I can think of consist of running through each pixel column of the tile finding the first solid pixel starting from the top. That will be the contour of the tile. Then analyse how the Y value of the contour varies within the tile. If the minimum and maximum Y values found are considerably apart, it's probably a slope. – David Gouveia Jan 2 '12 at 2:18
• Every tile is a 32x32 texture. All the slopes are a 0 - 45 degree angle. And yes I'm determining the slope at load time only. – Raj Jan 2 '12 at 2:22
• Alright, with those constraints it's really simple to determine. I'll write an answer based on that specific case. – David Gouveia Jan 2 '12 at 2:23
• I'll do the obligatory "don't do that" comment. This is something you should precompute when preparing the data for distribution, not at load time. And probably not in OpenGL: before uploading the textures you probably load pictures in a pixel buffer; that's a good place to analyse the data. – sam hocevar Jan 2 '12 at 10:14

Every tile is a 32x32 texture. All the slopes are a 0 - 45 degree angle. And yes I'm determining the slope at load time only.

Under this assumption, and that other non-slope tiles won't have strange variations, the simplest solution I can think of is the following.

• If blue pixel is empty and purple pixel is filled, the tile is a right slope.
• If green pixel is filled and yellow pixel is empty, the tile is a left slope.

If you're using alpha transparency on your tiles, you can check if a pixel is empty by seeing if its alpha value is 0.

In pseudocode:

s = image size
if(image[0,s-2].alpha == 0 && image[s-1,0].alpha != 0)
{
// Right Slope
}
else if(image[0,0].alpha != 0 && image[s-1,s-2].alpha == 0)
{
// Left Slope
}


As for getting the color at specific pixel on a texture, check the OpenGL API.

• A way more robust against fancy textures would be to, for a given possible slope's ideal shape, average the alpha of all pixels in each of the "above" and "below" regions - the higher the difference between them, the better that slope is a match. – Kevin Reid Jan 2 '12 at 3:34