Flood Fill - Close but not working

I'm working on a flood fill routine; I think I'm close, but it's not quite working right. I tested it with a bitmap 32x32 pixels with the starting point at 16,16. It just makes a cross, so it isn't extending across the entire bitmap. I have wracked my head over this code a few hours now, tweaking this and that with no success... Perhaps some smarter minds could take a look.

A few things to clarify:
Point is just a class containing integers X and Y.
#include <stack> is needed to access stack<>
Allegro 5 is the graphics library I'm using for colors and pixel manipulation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void FloodFill(Point s, ALLEGRO_COLOR newClr)
{
	ALLEGRO_COLOR oldClr = al_get_pixel(aBMP,s.X,s.Y);
	if (oldClr.r == newClr.r &&
			oldClr.g == newClr.g &&
			oldClr.b == newClr.b &&
			oldClr.a == newClr.a) { return; }

	stack<Point> ptsCollection;
	ptsCollection.push(s);

	al_draw_pixel(s.X,s.Y,newClr);

	while (!ptsCollection.empty())
	{
		Point nPt = ptsCollection.top();
		ptsCollection.pop();

		if (nPt.X > 0) ProcessPoint(ptsCollection, nPt.X-1, nPt.Y, oldClr, newClr);
		if (nPt.Y > 0) ProcessPoint(ptsCollection, nPt.X, nPt.Y-1, oldClr, newClr);
		if (nPt.X < al_get_bitmap_width(aBMP)-1) ProcessPoint(ptsCollection, nPt.X+1, nPt.Y, oldClr, newClr);
		if (nPt.Y < al_get_bitmap_height(aBMP)-1) ProcessPoint(ptsCollection, nPt.X,nPt.Y+1, oldClr, newClr);
	}
}

void ProcessPoint(stack<Point> pts, int X, int Y, ALLEGRO_COLOR oClr, ALLEGRO_COLOR nClr)
{
	ALLEGRO_COLOR tempClr = al_get_pixel(aBMP,X,Y);
	if (tempClr.r == oClr.r &&
			tempClr.g == oClr.g &&
			tempClr.b == oClr.b &&
			tempClr.a == oClr.a)
	{
		pts.push(Point(X,Y));
		al_draw_pixel(X,Y,nClr);
	}
}
Last edited on
You're passing the stack to ProcessPoint() by copy not by reference, so modifications made to it in ProcessPoint() are not reflected in the caller.
doh! thanks, that should fix it.
Topic archived. No new replies allowed.