SFML community forums

General => General discussions => Topic started by: Sirt on May 12, 2010, 06:11:50 am

Title: getting colour from a sprite?
Post by: Sirt on May 12, 2010, 06:11:50 am
Hi fella's.

Still using v1.5.
I need to iterate through a large (1024x640) sprite (it's a map)
and test the colour of each pixel.
If it's green I insert FOREST into a matrix, black is MOUNTAIN etc.

So...I have found a GetPixel and GetColor method but I am at a loss how to use them.

Could someone give me a helping hand please?
Title: getting colour from a sprite?
Post by: Walker on May 12, 2010, 06:57:34 am
I was doing something similar recently trying to get some destructible terrain happening, although I was reading directly from an image.

Code: [Select]
for (int x = 0; x < 1024; x++)
    {
        for (int y = 0; y < 768; y++)
        {
            if (terrainImage.GetPixel(x, y) == sf::Color::White)//if pixel is white
            {
                terrainArray[x][y] = 0;//acts as nothing
            }
            else if (terrainImage.GetPixel(x, y) == sf::Color::Green)
            {
                terrainArray[x][y] = 1;//acts as normal land

            }
            else if (terrainImage.GetPixel(x, y) == sf::Color::Black)
            {
                terrainArray[x][y] = 2;//indestructible pixel
            }
        }
}


I then later used the array as a reference to build an image/sprite pair that would be displayed. I used this method so I could make each separate area (sky, normal ground and rocks for indestructible bits) look a little more interesting by colouring each pixel semi randomly.

I think that should help. Feel free to ask for more if you want.
Title: getting colour from a sprite?
Post by: Sirt on May 12, 2010, 07:35:12 am
Ahh,thats just what I am after.
Thank you very much.