Okay, sorry for the double post but;
I've tried a flood fill algorithm, but it's just so slow! It takes like 5 minutes to even do it!
So how the hell does paint do it so fast?
Here's my code anyway;
void FloodFill(sf::Vector2i Pixel, sf::Color SelectedColour, sf::Color TargetColour)
{
std::queue<sf::Vector2i> FillQueue;
if (Mountain.GetPixel(Pixel.x, Pixel.y) != TargetColour)
return;
FillQueue.push(Pixel);
while (!FillQueue.empty())
{
sf::Vector2i n (FillQueue.front());
if (Mountain.GetPixel(n.x, n.y) == TargetColour)
{
Mountain.SetPixel(n.x, n.y, SelectedColour);
if (n.x - 1 > 0)
FillQueue.push(sf::Vector2i(n.x-1, n.y));
if (n.x + 1 < 800) //800 being window width.
FillQueue.push(sf::Vector2i(n.x+1, n.y));
if (n.y - 1 > 0)
FillQueue.push(sf::Vector2i(n.x, n.y-1));
if (n.y + 1 < 599) //800 being window height, except 600 caused crashing.
FillQueue.push(sf::Vector2i(n.x, n.y+1));
}
FillQueue.pop();
std::cout << FillQueue.size() << std::endl;
//Draw();
}
return;
}
The reason Draw was there, was so I can see what happens when it does it, (Cool effect of it spreading out, but obviously this makes the whole thing slower, so i commented it out). cout is there just so I could see that it was doing something and how many pixels are left to change... This just keeps going up.
If I remove all 4 of these;
if (Mountain.GetPixel(n.x, n.y+1) == TargetColour
this speeds it up the cycles tremendously, but then also doubles the amount of pixels it's checking.
How can I speed up this operation so it's as fast as paint? Is it because I'm constantly using an sf::Image (Mountain) to check for pixels? I can't find any other way to check the pixels of the thing even through Sprite or Texture.
This is becoming a real pain in the backside, because I have no idea!
EDIT!
Okay, I managed to speed it up by half, but it still takes a really long time, I've updated the code above with what I have.
EDIT2!
Er... okay... I removed std::cout... and it's instant....
...anyone care to explain why...?
Also, fairly obvious, but debug takes 2-3 seconds to achive what I want to do, but release is fairly instant. It's quite cool how much difference it makes.