03-12-2024, 02:55 AM
Is there a limit to the number of recursions that QB64PE can handle? The code below works well with a circle the radius of 50, but increase the radius to 100 and the program will crash part way through.
Also, I figured out why the IDE was using so much RAM (256MB as I reported in another thread). Every crash of this program increases the IDE RAM usage.
Keep in mind the code below is just me playing around. It's not optimized at all or debugged in any way. Just me experimenting.
Also, I figured out why the IDE was using so much RAM (256MB as I reported in another thread). Every crash of this program increases the IDE RAM usage.
Keep in mind the code below is just me playing around. It's not optimized at all or debugged in any way. Just me experimenting.
Code: (Select All)
OPTION _EXPLICIT
DIM Image AS LONG
DIM x AS INTEGER
DIM y AS INTEGER
Image = _NEWIMAGE(800, 600, 32)
SCREEN Image
MemCLS Image, _RGB32(255, 255, 255)
CIRCLE (399, 299), 50, _RGB32(0, 0, 0) ' recursion works at radius 50, fails at radius 100
FloodPaint 399, 299, _RGB32(255, 0, 255), _RGB32(0, 0, 0) ' recursive paint
SUB FloodPaint (x AS INTEGER, y AS INTEGER, c AS _UNSIGNED LONG, b AS _UNSIGNED LONG)
' x,y - paint location
' c - paint color
' b - border color
DIM p AS _UNSIGNED LONG ' pixel color
STATIC Iterations AS LONG ' the number of iterations
IF x = -1 OR y = -1 OR x = _WIDTH(_DEST) OR y = _HEIGHT(_DEST) THEN EXIT SUB
p = POINT(x, y)
IF p = b OR p = c THEN EXIT SUB
PSET (x, y), c
Iterations = Iterations + 1
' Just some code to show what's going on and slow things down a bit
LOCATE 1, 1
PRINT x, y, Iterations
_DELAY .001
' Check neighboring pixels
FloodPaint x - 1, y, c, b
FloodPaint x + 1, y, c, b
FloodPaint x, y - 1, c, b
FloodPaint x, y + 1, c, b
Iterations = Iterations - 1
END SUB
FUNCTION xyOFFSET%& (i AS LONG, x AS INTEGER, y AS INTEGER)
' Calculates the pixel memory address offset location within an image given the x,y coordinate pair
'
' i - image handle
' x,y - pixel coordinate
$CHECKING:OFF
IF (i = 0 OR i < -1) AND _PIXELSIZE(i) = 4 THEN ' valid 32bit image?
xyOFFSET%& = y * _WIDTH(i) * 4 + x * 4 ' yes, calculate pixel offset location
END IF
$CHECKING:ON
END FUNCTION
SUB MemCLS (i AS LONG, c AS _UNSIGNED LONG)
' Clears an image with provided color
'
' i - image handle
' c - 32bit color
DIM m AS _MEM
$CHECKING:OFF
IF (i = 0 OR i < -1) AND _PIXELSIZE(i) = 4 THEN ' valid 32bit image?
m = _MEMIMAGE(i)
_MEMFILL m, m.OFFSET, m.SIZE, c
_MEMFREE m
$CHECKING:ON
END IF
END SUB