Array Conundrumn

BlitzMax Forums/BlitzMax Beginners Area/Array Conundrumn

Wether this is down to a problem with arrays or not is a mystery. Which I'd love help resolving and getting it up and running properly if it's possible.

As you'll see that it doesnt clear out and goes into infinity and beyond. Where as it should gradually bring each of the ripples to an end.
'Reflection Effect
'by Thumbz - 
'50% working converted from B2D To BetaMax 

Strict

Const XRES=320
Const YRES=240

Graphics XRES,YRES,16
SeedRnd MilliSecs()
HideMouse

Local  ImageName:Timage	=CreateImage(XRES,YRES,1,DYNAMICIMAGE)
Global Screen:Timage	=CreateImage(XRES,YRES,1,DYNAMICIMAGE)
Global Locked:TPixmap

Global Background[XRES,YRES  ]
Global ReflectMap[XRES,YRES,2]

Global Spread = 40

'
' make a background
'
For Local a=0 To 300
	
	SetColor Rand(64,128),64,Rand(64,196)
	
	DrawRect Rand(XRES),Rand(YRES),Rand(128),Rand(128)
	
Next

GrabImage ImageName,0,0 
'
' store colour info.
'
Locked=LockImage(ImageName)

For Local y=0 To YRES-1
	For Local x=0 To XRES-1
		
		Background[x,y]=ReadPixel(Locked,x,y) 
		
	Next
Next
UnlockImage (ImageName)


'
' the main program loop.
'
While Not KeyHit(KEY_ESCAPE)
Cls
	
	UserControl			()
	UpdateReflections	()	
	
	DrawImage Screen,0,0
	
	SetColor 255,255,255
	Plot MouseX(),MouseY()
	
	FlushMem()
Flip
Wend



Function UserControl()

If MouseHit(1) Then 
	ReflectMap[MouseX(),MouseY(),1] = ReflectMap[MouseX(),MouseY(),1] - 100
End If

End Function



Function UpdateReflections()

Local i
Local x,y

Local rgb
Local alp
Local red
Local grn
Local blu

Local Reflect

Local nx
Local ny

Locked=LockImage(Screen)

For y = 1 To YRES - 2
	For x = 1 To XRES - 2
	
		Reflect 			= (( ReflectMap[x - 1,y,1] + ReflectMap[x + 1,y,1] + ReflectMap[x,y - 1,1] ..

							   + ReflectMap[x,y + 1,1] ) / 2) - ReflectMap[x,y,2]
          
		ReflectMap[x,y,2] 	= Reflect - (Reflect / Spread)
     
	Next
Next


For y = 1 To YRES - 2
	For x = 1 To XRES - 2
     
		Local xdis = (( ReflectMap[x, y + 1, 2] - ReflectMap[x,y - 1,2] ) - ReflectMap[x,y,2] ) / 4
    	Local ydis = (( ReflectMap[x + 1, y, 2] - ReflectMap[x - 1,y,2] ) - ReflectMap[x,y,2] ) / 4
     
		Local wx = x + xdis
     	Local wy = y + ydis
     
		If wx <= 0 		Then wx = 1
     	If wx >= XRES 	Then wx = XRES - 1
     	If wy <= 0 		Then wy = 1
     	If wy >= YRES 	Then wy = YRES - 1
     
		rgb = Background[wx,wy]
		
		alp	=255
		red	=(rgb & $FF0000)	/$FFFF
		grn =(rgb & $FF00)		/$FF
		blu	=(rgb & $FF)
		
		rgb = (alp Shl 24) | (red Shl 16) | (grn Shl 8) | (Blu Shl 0)
		
		'
		' draw to screen 
	    '
	 	If rgb<>0 Then
           		
			WritePixel (Locked,x,y,rgb)
		
		End If
		'sort / temp
		Reflect		 		= ReflectMap[x,y,2]
    	ReflectMap[x,y,2] 	= ReflectMap[x,y,1]
		ReflectMap[x,y,1] 	= Reflect
	
	Next
	'
    ' All Clear 
	'
	ReflectMap[0	   ,y,1] = 0
	ReflectMap[0	   ,y,2] = 0
	ReflectMap[XRES - 1,y,1] = 0
	ReflectMap[XRES - 1,y,2] = 0

Next

For x = 0 To XRES - 1
	
	ReflectMap[x,0		 ,1] = 0
	ReflectMap[x,0	   	 ,2] = 0
    ReflectMap[x,YRES - 1,1] = 0
    ReflectMap[x,YRES - 1,2] = 0

Next

UnlockImage (Screen)

End Function


if you could help that would be amazing and thank you,
THUMBZ

For what it's worth:
I found that Reflect= controls the ripple so by placing a limit on how many times that loop gets iterated controlled the diameter of the ripple.
However: When a new ripple point is chosen, all previous points continue to grow from where they left off.
{edit} Upon further study the ripples do expand and shrink. Hard to see.
I set the reflect loop to 20 iterations and if you pick a very narrow black region as a click point (in order to see the effect easily) it will expand 20 times.
Pick a second narrow black band and the first will shrink. But the first ripple never goes totally away. Subsequent clicks while watching the 1st ripple shows that it expands and shrinks every other click.
Thinking out loud: Is it possible to fade the ripple to a non-ripple as it expands by reducing transparency then to completely eliminate that ripple?
I know this code is way beyond my experience but as they say "No Pain - No Gain". Let me tell you, my brain is in serious pain right now.
Will be waiting for the "Real" answer to your problem.
Oh BTW, for me anyway, by using Repeat - Until KeyHit(key_escape) allows instant exit where as the way you have it now does not.

Thanks for your time and for looking Emmett.

What Reflect does is give a little averaging and blur to the proceedings to give the illussion. Try and Increase spread and the 100 value on the if mousehit line, for it to be thicker and different results. No joy though, scratching head time again.