SOLVED Floodfill code for my paint program

If there's a thread with the same problem, maybe one with a solution, please link me to it.

I'm making a paint-style program and I have the drawing tool and the erase tool. All I need is a floodfill tool similar to the one in this video.

I tried importing the code from this page, however it is apparently for a different purpose and draws a QR code or something (that's all I remember), not what I wanted. I tried the code from another page, and IIRC that didn't work properly either (probably because I left something out, I could try again).

I don't know if that's allowed, but you can download my project here. Just so you can put the code in and the rest of it will work, in case using the code from another program makes the floodfill tool or the rest of the program non-functional.
 

Bentley

Member
Code:
var xx, yy, targ_color, color;
xx = argument0;
yy = argument1;
targ_color = argument2;
color = argument3;

// Off canvas
if (!point_in_rectangle(xx, yy, 0, 0, ds_grid_width(grid) - 1, ds_grid_height(grid) - 1))
{
    return;
}

// Not the color you want to replace
if (grid[# xx, yy] != targ_color)
{
    return; 
}

// Set color
grid[# xx, yy] = color;

fill(xx + 1, yy, targ_color, color);
fill(xx - 1, yy, targ_color, color);
fill(xx, yy + 1, targ_color, color);
fill(xx, yy - 1, targ_color, color);
Should work, just tested it. I created a grid that was room_width, room_height and cleared it to c_white.
 
Last edited:
I compiled that into a script called "draw_floodfill" and replaced the "//help me" comment in the Step event of oTool with this code:
Code:
surface_set_target(surf);
gpu_set_blendmode(bm_normal);
draw_floodfill(x,y,c_black,brushColor);
surface_reset_target();
however it didn't run and instead gave me these errors:
Script: draw_floodfill at line 21: unknown function or script fill
Script: draw_floodfill at line 22: unknown function or script fill
Script: draw_floodfill at line 23: unknown function or script fill
Script: draw_floodfill at line 24: unknown function or script fill


I deleted that and the errors still popped up, then put them back in and added this to the Create event of oTool:
Code:
fill = 0;
and it's still no good. Could you help me with that please?
 
C

CombatCalamity

Guest
I compiled that into a script called "draw_floodfill" and replaced the "//help me" comment in the Step event of oTool with this code:
Code:
surface_set_target(surf);
gpu_set_blendmode(bm_normal);
draw_floodfill(x,y,c_black,brushColor);
surface_reset_target();
however it didn't run and instead gave me these errors:
Script: draw_floodfill at line 21: unknown function or script fill
Script: draw_floodfill at line 22: unknown function or script fill
Script: draw_floodfill at line 23: unknown function or script fill
Script: draw_floodfill at line 24: unknown function or script fill


I deleted that and the errors still popped up, then put them back in and added this to the Create event of oTool:
Code:
fill = 0;
and it's still no good. Could you help me with that please?
It's a recursive call, if you change your script name then isn't it obvious to change the names at line 21-24 also... Duh....

From line 21 to line 24, change fill to draw_floodfill
 
So I did that, the program did run, but when I clicked on the fill button, it crashed with this message:
Variable oTool.grid(10002, -2147483648) not set before reading it.
That means I have to put the grid variable in the Create event of oTool, or something. What should I set it to?
 
He tells you in his post...


grid = ds_grid_create(room_width, room_height);
Oh, OK. Tried that and it didn't crash that time, however it won't fill in the area I am on.

Here's my code:
Code:
if (mouse_check_button_pressed(mb_left))
{
    if tool = 1 //Colour
    {
        surface_set_target(surf);
        gpu_set_blendmode(bm_normal);
        draw_floodfill(x,y,c_white,brushColor);
        surface_reset_target();
    }
}
Am I missing something?
 
Also looking at the code it seems as it just replaces a specific colour. What I actually want was to replace any colour that the cursor is on, like pretty much any Paint program.
 

Bentley

Member
Oh, OK. Tried that and it didn't crash that time, however it won't fill in the area I am on.

Here's my code:
Code:
if (mouse_check_button_pressed(mb_left))
{
    if tool = 1 //Colour
    {
        surface_set_target(surf);
        gpu_set_blendmode(bm_normal);
        draw_floodfill(x,y,c_white,brushColor);
        surface_reset_target();
    }
}
Am I missing something?
You are starting the flood fill from the instance's x/y rather than the mouses x/y.
 
You are starting the flood fill from the instance's x/y rather than the mouses x/y.
Maybe draw_getpixel() will help.
I edited it to this:
Code:
draw_floodfill(mouse_x,mouse_y,draw_getpixel(mouse_x,mouse_y),brushColor);
and still nothing happened when I tried to click on the area to get the floodfill to work.
 
C

CombatCalamity

Guest
Flood algorithm is very common. If you search around there's tons of tutorials, resources and examples. If you'd just put in minimal effort I'm sure you can translate them easily into GML.
 
Flood algorithm is very common. If you search around there's tons of tutorials, resources and examples. If you'd just put in minimal effort I'm sure you can translate them easily into GML.
I only managed to find a couple which I mentioned in my original post and IIRC they didn't work properly. Thanks anyway, I'll keep looking.
 
So I decided to copy the code from here (which I already linked to in my original post) into its own program and it seemed to work there. I might have to redo my paint program if I want to use it there.
 
BUMP
So by some miracle I managed to get the floodfill working using code from a different source (to be honest, buying the program from the video I saw), but while editing it to my preferences, I ran into some other problems. If you're still reading this old thread, the few that I managed to find can be read here.
 
Last edited:
Top