After the surprising success of converting my old filled polygon routine over to PF, I decided to take a crack at another routine I wrote a year ago that I thought might be of interest to us all: flood fill. Here it is:
And here's a test GameLoop for it:
As before, it would be nice to integrate this directly with PF, maybe Thierry will do it (hint-hint!). I certainly don't mind doing it myself, but time won't allow it right now. As with the filled poly routine, I'm sure this can be made immensely better, but it works, and that's good enough for me!
Simply pass it a starting coordinate and the RGB values to fill with and off it goes, filling every pixel that matches the RGB of the starting pixel. This example shows a growing and expanding circle that fills with a different color in each direction. Nothing fancy....
BUT IT WORKS!
(Hmm, I wonder what else I can pull out of my old library... After all the trouble it gave me back then, it's nice to know all the effort might not go to waste after all!)
- Code:
#define PFE_STACK_MAX 10000
#define PFE_STACK_PUSH(Y, XL, XR, DY) \
if (sp < stack + PFE_STACK_MAX && Y + (DY) >= 0 && Y + (DY) <= 320) \
{sp->y = Y; sp->xl = XL; sp->xr = XR; sp->dy = DY; sp++;}
#define PFE_STACK_POP(Y, XL, XR, DY) \
{sp--; Y = sp->y+(DY = sp->dy); XL = sp->xl; XR = sp->xr;}
struct PFE_FFSeg {short y, xl, xr, dy;};
struct PFE_FFSeg stack[PFE_STACK_MAX], *sp = stack;
void PFE_FloodFill(DisplayDevice* display, int PFEI_x, int PFEI_y, int PFEI_colR, int PFEI_colG, int PFEI_colB) {
int l;
int x1;
int x2;
int dy;
Pixel nv = Color(PFEI_colR, PFEI_colG, PFEI_colB);
Pixel ov = display->GetPixel(PFEI_x, PFEI_y);
if (ov == nv || PFEI_x < 0 || PFEI_x > 240 || PFEI_y < 0 || PFEI_y > 320) { return; }
PFE_STACK_PUSH(PFEI_y, PFEI_x, PFEI_x, 1);
PFE_STACK_PUSH(PFEI_y + 1, PFEI_x, PFEI_x, -1);
while (sp > stack) {
PFE_STACK_POP(PFEI_y, x1, x2, dy);
for (PFEI_x = x1; PFEI_x >= 0 && display->GetPixel(PFEI_x, PFEI_y) == ov; PFEI_x--)
display->SetPixel(PFEI_x, PFEI_y, Color(PFEI_colR, PFEI_colG, PFEI_colB));
if (PFEI_x >= x1) { goto skip; }
l = PFEI_x + 1;
if (l < x1)
PFE_STACK_PUSH(PFEI_y, l, x1 - 1, -dy);
PFEI_x = x1 + 1;
do {
for (; PFEI_x <= 240 && display->GetPixel(PFEI_x, PFEI_y) == ov; PFEI_x++)
display->SetPixel(PFEI_x, PFEI_y, Color(PFEI_colR, PFEI_colG, PFEI_colB));
PFE_STACK_PUSH(PFEI_y, l, PFEI_x - 1, dy);
if (PFEI_x > x2 + 1)
PFE_STACK_PUSH(PFEI_y, x2 + 1, PFEI_x - 1, -dy);
skip: for (PFEI_x++; PFEI_x <= x2 && display->GetPixel(PFEI_x, PFEI_y) != ov; PFEI_x++);
l = PFEI_x;
} while (PFEI_x <= x2);
}
}
And here's a test GameLoop for it:
- Code:
void InvasionTrivia::GameLoop() {
DisplayDevice* display = GetDisplay();
display->Clear(Color(0, 0, 0));
static unsigned short int crad = 10;
static unsigned short int craddir = 1;
unsigned short int cR1 = 0;
unsigned short int cG1 = 255;
unsigned short int cB1 = 0;
static unsigned short int cR2 = 255;
static unsigned short int cG2 = 0;
static unsigned short int cB2 = 0;
display->DrawCircle(120, 160, crad, Color(cR1, cG1, cB1));
PFE_FloodFill(display, 122, 162, cR2, cG2, cB2);
if (craddir == 1) { crad++; } else { crad--; }
if (crad > 100) { craddir = 0; cR2 = 0; cG2 = 0, cB2 = 255; }
if (crad < 10 ) { craddir = 1; cR2 = 255; cG2 = 0, cB2 = 0; }
display->Update();
}
As before, it would be nice to integrate this directly with PF, maybe Thierry will do it (hint-hint!). I certainly don't mind doing it myself, but time won't allow it right now. As with the filled poly routine, I'm sure this can be made immensely better, but it works, and that's good enough for me!
Simply pass it a starting coordinate and the RGB values to fill with and off it goes, filling every pixel that matches the RGB of the starting pixel. This example shows a growing and expanding circle that fills with a different color in each direction. Nothing fancy....
BUT IT WORKS!
(Hmm, I wonder what else I can pull out of my old library... After all the trouble it gave me back then, it's nice to know all the effort might not go to waste after all!)