This
flood fill code is adapted from a
Tetris clone called "Tetanus On Drugs" by
Damian Yerrick.
It's licensed under the
GNU General Public License and comes with ABSOLUTELY NO WARRANTY.
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 20
typedef struct MAP
{
unsigned char b[BOARD_HEIGHT][BOARD_WIDTH];
} MAP;
static void flood_loop(MAP *map, int x, int y, unsigned int dst_c, unsigned int src_c)
{
int fillL, fillR, i;
int in_line = 1;
/* find left side, filling along the way */
fillL = fillR = x;
while(in_line)
{
map->b[y][fillL] = dst_c;
fillL--;
in_line = (fillL < 0) ? 0 : (map->b[y][fillL] == src_c);
}
fillL++;
/* find right side, filling along the way */
in_line = 1;
while(in_line)
{
map->b[y][fillR] = c;
fillR++;
in_line = (fillR > 9) ? 0 : (map->b[y][fillR] == fillC);
}
fillR--;
/* search top and bottom */
for(i = fillL; i <= fillR; i++)
{
if(y > 0 && map->b[y - 1][i] == fillC)
flood_loop(map, i, y - 1, c, fillC);
if(y < BOARD_HEIGHT && map->b[y + 1][i] == fillC)
flood_loop(map, i, y + 1, c, fillC);
}
}
void flood_fill(MAP *map, int x, int y, unsigned int c)
{
flood_loop(map, x, y, c, map->b[y][x]);
map->b[y][x] = c; /* some buggy optimizers needed this line */
}