|
Help with Flood Fill Algorithm |
HellfireXP
Member #14,798
December 2012
|
I am trying to write a flood fill algorithm using Allegro 5.0.7. Logically, my code should be working, but for some reason I don't think the set_pixel is being applied. The only thing I can think is the Allegro functions aren't doing what I think they are doing. My IDE is Visual C++ 2010. Can anyone help? 1
2void FloodFill(Point s, ALLEGRO_COLOR newClr)
3{
4 al_lock_bitmap(FOCUS,ALLEGRO_PIXEL_FORMAT_ANY,ALLEGRO_LOCK_READWRITE);
5 ALLEGRO_COLOR oldClr = al_get_pixel(FOCUS,s.X,s.Y);
6 if (oldClr.r == newClr.r &&
7 oldClr.g == newClr.g &&
8 oldClr.b == newClr.b &&
9 oldClr.a == newClr.a) { return; }
10
11 stack<Point> ptsCollection;
12 ptsCollection.push(s);
13
14 al_draw_pixel(s.X,s.Y,newClr);
15
16 while (!ptsCollection.empty())
17 {
18 Point nPt = ptsCollection.top();
19 ptsCollection.pop();
20
21 if (nPt.X > 0) ProcessPoint(ptsCollection, nPt.X-1, nPt.Y, oldClr, newClr);
22 if (nPt.Y > 0) ProcessPoint(ptsCollection, nPt.X, nPt.Y-1, oldClr, newClr);
23 if (nPt.X < al_get_bitmap_width(FOCUS)-1) ProcessPoint(ptsCollection, nPt.X+1, nPt.Y, oldClr, newClr);
24 if (nPt.Y < al_get_bitmap_height(FOCUS)-1) ProcessPoint(ptsCollection, nPt.X,nPt.Y+1, oldClr, newClr);
25 }
26 al_unlock_bitmap(FOCUS);
27}
28
29void ProcessPoint(stack<Point>& ptsCollection, int X, int Y, ALLEGRO_COLOR oClr, ALLEGRO_COLOR nClr)
30{
31 ALLEGRO_COLOR tempClr = al_get_pixel(FOCUS,X,Y);
32 if (tempClr.r == oClr.r &&
33 tempClr.g == oClr.g &&
34 tempClr.b == oClr.b &&
35 tempClr.a == oClr.a)
36 {
37 ptsCollection.push(Point(X,Y));
38 cout << X << ", " << Y << "\n";
39 al_draw_pixel(X,Y,nClr);
40 }
41}
|
Matthew Leverton
Supreme Loser
January 1999
|
Assuming your algorithm is correct and you are seeing your cout lines... Are you sure that you have called al_set_target_bitmap() on the appropriate bitmap? Are you then drawing that bitmap to the back buffer? |
Elias
Member #358
May 2000
|
I'd also use al_put_pixel, al_draw_pixel isn't really supposed to do much on a locked bitmap I think (at least the other al_draw_* functions aren't). -- |
HellfireXP
Member #14,798
December 2012
|
Thanks guys. The solution that worked was changing al_draw_pixel to al_put_pixel. I had the cout << in there just to try and see where my error was - I didn't plan on leaving it. One thing I noticed was that all the coordinates kept being added to the stack over and over; thus none of them were applying the color to the bitmap or they'd have been removed. Good to know for future application - I'll use al_put_pixel instead. Thanks.:D |
|