Archived

This topic is now archived and is closed to further replies.

how to do floodfill ?

This topic is 6306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

well how do you do it ? in my game i want to load artwork.......and then change the color of certain areas of it...like floodfill in MS-Paint. Since I dont know any vertices and the shape could be highly irregular. I can think of two different ways to do this but i dont like either one. i can either a) use recursion like color the pixel at x, y location if the pixel x-1, y is the same color as this one then call the function recrsuively if the pixel x+1, y is the same color as this one then call the fuinction recursively and so forth and so forth but i dont like that idea and dont know how well it would work............probably pretty badly i would guess or b) use 8-bit mode and change the color of the pallete entry at that index but then that would change the color off all the other things on the screen with that color. how do i do this ? "Now go away or I shall taunt you a second time" - Monty Python and the Holy Grail

Share on other sites
Well, the Computer Graphics book (i.e. a lot of algorithms) uses recursion for every flood fill algorithm they use. So, I''ll copy a basic example that they show:
  void floodFill4(int x, int y, int fillColor, int oldColor) { if(getPixel(x, y)==oldColor) { setColor(fillColor); setPixel(x, y); floodFill4(x+1,y,fillColor,oldColor); floodFill4(x-1,y,fillColor,oldColor); floodFill4(x,y+1,fillColor,oldColor); floodFill4(x,y-1,fillColor,oldColor); }}

Change their default functions (setPixel, setColor, getPixel) to something that applies to your API. OldColor is meant to be the color of the "origin" pixel of the floodfill. There''s probably a faster method to do this, but most of the algorithms in this book are pretty fast (some are so esoteric that I would never have thought of them, but are very fast), so I don''t know exactly how good this one is .

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
http://www.gdarchive.net/druidgames/

Share on other sites
Better yet, take CSC 518 or 4something at NCSU. I don''t think Robbins teaches anymore though. Maybe Gehringer.

• 10
• 17
• 9
• 14
• 41
• Forum Statistics

• Total Topics
631070
• Total Posts
2997746
×