Code: Select all
DisableDebugger
#X=600:#Y=400
#Dampening=0.995 ;change this value to make the wave last longer (closer to 1)
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0 Or OpenWindow(0, 0, 0, #X, #Y, "2D waves", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)=0 Or OpenWindowedScreen(WindowID(0),0,0,#X,#Y,0,0,0,#PB_Screen_NoSynchronization )=0
MessageRequester("Error", "Can't open the sprite system", 0)
End
EndIf
Dim Buffer.f(#X*2,#Y)
Structure Pixel
Pixel.l
EndStructure
prev_buffer.i=0
cur_buffer.i=#X
Repeat
Repeat:Until WindowEvent()=0
FlipBuffers()
;copy the current buffer to the screen memory
StartDrawing(ScreenOutput())
Buffer = DrawingBuffer()
Pitch = DrawingBufferPitch()
For j=0 To #Y-1
*Line.Pixel = Buffer+Pitch*j
For ii=0 To #X-1
i=ii+cur_buffer
;propagate the wave
If ii>0 And ii<#X-1 And j>1 And j<#Y-1
k=ii+prev_buffer
Buffer(i,j)=(Buffer(k-1,j)+Buffer(k+1,j)+Buffer(k,j-1)+Buffer(k,j+1))/2-Buffer(i,j)
Buffer(i,j)*#Dampening
EndIf
;copy to the screen
color.i=Int(Buffer(i,j)*10)
*Line\Pixel=RGB(color,color,color)
*line+4
Next ii
Next j
;display mouse cursor
DrawText(0,0,"[Escape] to Quit")
DrawText(0,20,"[Left Clic] to poke the surface")
Box(MouseX(),MouseY(),10,10,RGB(255,255,255))
StopDrawing()
ExamineKeyboard()
ExamineMouse()
If MouseButton(#PB_MouseButton_Left) And MouseX()>0 And MouseX()<#X-1 And MouseY()>0 And MouseY()<#Y-1
Buffer(MouseX(),MouseY())=20
EndIf
cur_buffer!#X
prev_buffer!#X
Until KeyboardPushed(#PB_Key_Escape)