1
Graphics / sf::Sprite FloodFill or Masking?
« on: August 18, 2010, 03:32:04 pm »
For anyone interested:
The question still stands though. I'm not sure that it's quite fast enough to be used in the middle of a game (No other PCs to test it on atm).
Code: [Select]
#include <vector>
struct Point
{
Point(int x, int y) : X(x), Y(y) { }
int X, Y;
};
void fill(int x, int y, sf::Color fill, sf::Image* img)
{
int w = img->GetWidth(), h = img->GetHeight();
if (x < 0 || x > w-1 || y < 0 || y > h-1)
return;
sf::Color seedColor = img->GetPixel(x, y);
std::vector<Point> points;
points.push_back(Point(x, y));
while (points.size() > 0)
{
Point p = points.back();
int tx = p.X, ty = p.Y;
points.pop_back();
if (ty < 0 || ty > h-1 || tx < 0 || tx > w-1)
continue;
sf::Color col = img->GetPixel(tx, ty);
if (col == seedColor)
{
img->SetPixel(tx, ty, fill);
points.push_back(Point(tx+1, ty));
points.push_back(Point(tx-1, ty));
points.push_back(Point(tx, ty+1));
points.push_back(Point(tx, ty-1));
}
}
}
fill(75, 75, sf::Color(255, 0, 0), ImgManager::Instance()->getResource("test.bmp"));
The question still stands though. I'm not sure that it's quite fast enough to be used in the middle of a game (No other PCs to test it on atm).