I've optimized this to the best of my abilities and the speed has greatly improved. I tested this same code in DBPro and it works but only in the 32bit mode and it runs a bit slower. The default text size are different too
.
How do we get the equivelant of "color = r|g|b" in DBPro as this line doesn't seem to work?
REM ******************************************************************
REM * *
REM * DBClassic FloodFill Using Memblock *
REM * *
REM ******************************************************************
REM * - Ashingda27 *
REM ******************************************************************
set display mode 640,480,16
create bitmap 1,640,480
sync on
sync rate 0
REM Global Variables for FloodFill Function
dim FloodWidth(0)
dim FloodHeight(0)
dim FloodBytes(0)
dim FloodTargetColor(0)
dim FloodFillColor(0)
dim ScanLeftU(0)
dim ScanRightU(0)
dim ScanLeftD(screen height())
dim ScanRightD(screen height())
REM Setting up for the Memblock command.
FloodWidth(0) = screen width()
FloodHeight(0) = screen height()
FloodBytes(0) = screen depth()/8
REM Draws a Box with Text to be filled
ink rgb(255,255,255),0
box 320-25,240-5,320+25,240+10
ink 0,0
center text 320,240-5,"FloodFill"
ink rgb(255,255,255),0
text 0,0,"Click mouse to fill!"
line 320,10,320-100,10+100
line 320-100,10+100,320+100,10+100
line 320,10,320+100,10+100
circle 100,300,50
circle 150,350,50
box 400,300,450,350
box 500,300,550,350
line 400,325,500,325
set text opaque
REM **********************************************************************
REM Main Loop
REM **********************************************************************
do
mc = mouseclick()
mx = mousex()-1
my = mousey()-1
if mc = 1
if KeyM = 0
REM Calling the Function
T = Timer()
c = rgb(rnd(255),rnd(255),rnd(255))
FloodFill(mx,my,c)
T = Timer()-T
endif
KeyM = 1
else
KeyM = 0
endif
ink rgb(255,255,255),0
text 200,0,"Speed: "+str$(T)+" "
copy bitmap 1,0
sync
loop
REM **********************************************************************
REM Function FloodFill
REM **********************************************************************
Function FloodFill(x,y,FillColor)
ScanLeftU(0) = 0
ScanLeftD(y) = 0
ScanRightU(0) = 0
ScanRightD(y) = 0
REM Creating the Memblock from the second Bitmap.
make memblock from bitmap 1,1
REM 16Bit Mode or 32bit mode
if FloodBytes(0) = 2
REM Getting the FillColor
r = (rgbr(FillColor)*63488)/255
g = (rgbg(FillColor)*2016)/255
b = (rgbb(FillColor)*31)/255
FloodFillColor(0) = r | g | b
REM Getting Target Color
pos = 12+(x+y*FloodWidth(0))*FloodBytes(0)
FloodTargetColor(0) = memblock word(1,pos)
REM Fills the first Dot
write memblock word 1,pos,FloodFillColor(0)
else
REM Getting the FillColor
FloodFillColor(0) = FillColor
REM Getting Target Color
pos = 12+(x+y*FloodWidth(0))*FloodBytes(0)
FloodTargetColor(0) = memblock dword(1,pos)
REM Fills the first Dot
write memblock dword 1,pos,FloodFillColor(0)
endif
REM Calling the FloodLoop
if FloodTargetColor(0) <> FloodFillColor(0) then FloodLoop(x,y)
REM Convert to Image and paste onto screen
make image from memblock 1,1
paste image 1,0,0
for y = 0 to FloodHeight(0)
ScanLeftD(y) = 0
ScanRightD(y) = 0
next y
EndFunction
REM **********************************************************************
REM Function FloodLoop
REM **********************************************************************
Function FloodLoop(x,y)
posY = y*FloodWidth(0)*FloodBytes(0)
REM Left
for Left = x-1 to 0 step -1
pos = 12+Left*FloodBytes(0)+posY
REM Check 16bit or 32bit
if FloodBytes(0) = 2
if memblock word(1,pos) <> FloodTargetColor(0) then exit
write memblock word 1,pos,FloodFillColor(0)
else
if memblock dword(1,pos) <> FloodTargetColor(0) then exit
write memblock dword 1,pos,FloodFillColor(0)
endif
next Left
inc Left
REM Right
for Right = x+1 to FloodWidth(0)-1
pos = 12+Right*FloodBytes(0)+posY
REM Check 16bit or 32bit
if FloodBytes(0) = 2
if memblock word(1,pos) <> FloodTargetColor(0) then exit
write memblock word 1,pos,FloodFillColor(0)
else
if memblock dword(1,pos) <> FloodTargetColor(0) then exit
write memblock dword 1,pos,FloodFillColor(0)
endif
next Right
dec Right
REM Move Up
dec y
posY = y*FloodWidth(0)*FloodBytes(0)
if y > 0
for x = Left to Right
if x < ScanLeftU(0) or x > ScanRightU(0)
pos = 12+x*FloodBytes(0)+posY
REM Check 16bit or 32bit
if FloodBytes(0) = 2
if memblock word(1,pos) = FloodTargetColor(0)
ScanLeftD(y) = Left
ScanRightD(y) = Right
write memblock word 1,pos,FloodFillColor(0)
FloodLoop(x,y)
endif
else
if memblock dword(1,12+x*FloodBytes(0)+posY) = FloodTargetColor(0)
ScanLeftD(y) = Left
ScanRightD(y) = Right
write memblock dword 1,pos,FloodFillColor(0)
FloodLoop(x,y)
endif
endif
else
x = ScanRightU(0)+1
endif
next x
endif
inc y
ScanLeftU(0) = Left
ScanRightU(0) = Right
REM Move Down
ScanY = y
inc y
posY = y*FloodWidth(0)*FloodBytes(0)
if y < FloodHeight(0)-1
for x = Left to Right
if x < ScanLeftD(ScanY) or x > ScanRightD(ScanY)
pos = 12+x*FloodBytes(0)+posY
REM Check 16bit or 32bit
if FloodBytes(0) = 2
if memblock word(1,pos) = FloodTargetColor(0)
write memblock word 1,pos,FloodFillColor(0)
FloodLoop(x,y)
endif
else
if memblock dword(1,pos) = FloodTargetColor(0)
write memblock dword 1,pos,FloodFillColor(0)
FloodLoop(x,y)
endif
endif
else
x = ScanRightD(ScanY)+1
endif
next x
endif
dec y
if y > 0
ScanLeftD(y-1) = Left
ScanRightD(y-1) = Right
endif
EndFunction
The slow testing version has also been greatly improved.
sync on
sync rate 60
REM Global Variables for FloodFill Function
dim FloodWidth(0)
dim FloodHeight(0)
dim FloodBytes(0)
dim FloodTargetColor(0)
dim FloodFillColor(0)
dim ScanLeftU(1000)
dim ScanLeftD(1000)
dim ScanRightU(1000)
dim ScanRightD(1000)
dim key(640,480)
dim key2(640,480)
ink rgb(255,255,255),0
text 320,0, "Press key with ready"
x = 320
y = 240
REM Shape 1
if This = 1
ink rgb(255,255,255),0
box 320-5,240-5,320+5,240+5
box 320-10,240-10,320-5,240+10
box 320+5,240-10,320+10,240+10
endif
REM Shape 2 Circle
if This = 0
circle 320,240,10
y = y + 5
endif
REM Shape 3
if This = 1
box 320-10,240-5,320+10,240+5
ink 0,0
line 320-9,240-4,320-6,240+4
line 320-6,240-4,320-3,240+4
line 320-3,240-4,320-0,240+4
endif
REM Shape 4
if This = 1
ink rgb(255,255,255),0
box 320-15,240-7,320+10,240+7
ink 0,0
box 320-9,240-5,320-5,240+5
box 320+5,240-5,320+9,240+5
line 320-9,240,320+9,240
x = 320-15
y = 240-5
endif
`wait key
`print "Start!"
T = Timer()
FloodFill(x,y,rgb(255,0,0))
`ink rgb(255,255,255),0
`print "End! ";Timer()-T
sync
END
REM **********************************************************************
REM Function FloodFill
REM **********************************************************************
Function FloodFill(x,y,FillColor)
FloodTargetColor(0) = point(x,y)
FloodFillColor(0) = FillColor
REM First Filled dot
ink FloodFillColor(0),0
dot x,y
REM First Scan dot
ink rgb(0,255,0),0
dot x,y+100
key(x,y) = key(x,y)+1
key2(x,y) = 1
ScanLeftU(0) = x
ScanLeftD(y) = x
ScanRightU(0) = x
ScanRightD(y) = x
ink FloodFillColor(0),0
if FloodTargetColor(0) <> FloodFillColor(0) then FloodLoop(x,y)
ink rgb(255,255,255),0
x1 = 0
y1 = 0
for y = 240-12 to 240+12
for x = 320-12 to 320+12
x1 = x1 + 13
c = c + key(x,y)
ink rgb(100,100,100),0
box x1,y1,x1+12,y1+12
if key(x,y) = 1 then ink rgb(0,255,0),0 else ink rgb(255,150,150),0
if key2(x,y) = 0 then ink rgb(255,255,255),0
if key(x,y) = 0 then ink rgb(200,200,200),0
box x1,y1,x1+11,y1+11
ink 0,0
if key(x,y) = 1 then ink 0,0
if key(x,y) > 1 then ink rgb(255,0,0),0
text x1+3,y1-2,str$(key(x,y))
next x
y1 = y1 + 13
x1 = 0
next y
ink rgb(255,255,255),0
text 420,15,"Count: "+str$(c)
EndFunction
REM **********************************************************************
REM Function FloodLoop
REM **********************************************************************
Function FloodLoop(x,y)
REM Left
for Left = x-1 to 0 step -1
REM Scan Dot
ink rgb(0,255,0),0
dot Left,y+100
key(Left,y) = key(Left,y)+1
if point(Left,y) <> FloodTargetColor(0) then exit
ink FloodFillColor(0),0
dot Left,y
key2(Left,y) = 1
next Left
inc Left
REM Right
for Right = x+1 to 640
REM Scan Dot
ink rgb(0,255,0),0
dot Right,y+100
key(Right,y) = key(Right,y)+1
if point(Right,y) <> FloodTargetColor(0) then exit
ink FloodFillColor(0),0
dot Right,y
key2(Right,y) = 1
next Right
dec Right
i$ = inkey$()
if i$ = "1" then end
REM Move Up - scan RED dots
for x = Left to Right
REM Up
if x < ScanLeftU(0) or x > ScanRightU(0)
if point(x,y-1) = FloodTargetColor(0)
ink rgb(0,255,0),0
dot x,y+100-1
key(x,y-1) = key(x,y-1)+1
ScanLeftD(y-1) = Left
ScanRightD(y-1) = Right
ink FloodFillColor(0),0
dot x,y-1
key2(x,y-1) = 1
FloodLoop(x,y-1)
else
ink rgb(255,0,0),0
dot x,y+100-1
key(x,y-1) = key(x,y-1)+1
endif
else
x = ScanRightU(0)+1
endif
next x
ScanLeftU(0) = Left
ScanRightU(0) = Right
REM Move Down - scan BLUE dots
for x = Left to Right
REM Down
if x < ScanLeftD(y) or x > ScanRightD(y)
if point(x,y+1) = FloodTargetColor(0)
ink rgb(0,255,0),0
dot x,y+100+1
key(x,y+1) = key(x,y+1)+1
ink FloodFillColor(0),0
dot x,y+1
key2(x,y+1) = 1
ScanLeftU(0) = Left
ScanRightU(0) = Right
ScanLeftD(y-1) = Left
ScanRightD(y-1) = Right
FloodLoop(x,y+1)
else
ink rgb(0,0,255),0
dot x,y+100+1
key(x,y+1) = key(x,y+1)+1
endif
else
x = ScanRightD(y)+1
endif
next x
ScanLeftD(y-1) = Left
ScanRightD(y-1) = Right
EndFunction