Page 1 of 2

Cheap 2D Water ripples

Posted: Mon May 07, 2018 5:16 pm
by Fig

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)

Re: Cheap 2D Water riddles

Posted: Mon May 07, 2018 7:28 pm
by davido
@Fig,
Very nice, thank you for sharing. :D
Amazing result with so little code.

Re: Cheap 2D Water riddles

Posted: Mon May 07, 2018 9:43 pm
by Fig
Thx you Davido, means a lot to me.

It seams to be a very very old (and magic) effect.
An explanation here:
https://web.archive.org/web/20160116150 ... _water.htm

The author suggests to use it on a texture to go further...

Unfortunatly, it's not fast enough for a large picture as it is... :?

Re: Cheap 2D Water ripples

Posted: Tue May 08, 2018 8:39 am
by Kukulkan
Hm. On my Kubuntu with PB 5.46 LTS (x64) it immediately crashes on StopDrawing(). Maybe we found another PB bug for Linux?

Re: Cheap 2D Water ripples

Posted: Tue May 08, 2018 10:59 am
by wilbert
Kukulkan wrote:Hm. On my Kubuntu with PB 5.46 LTS (x64) it immediately crashes on StopDrawing(). Maybe we found another PB bug for Linux?
Maybe the Screen output is 24 bit instead of 32 bit ?
In that case it would be solved by changing

Code: Select all

*line+4
into

Code: Select all

*line+3

Re: Cheap 2D Water ripples

Posted: Tue May 08, 2018 11:23 am
by Kukulkan
Hi,

no, that does not help. Still the same crash... Maybe someone can confirm that on Linux?

Re: Cheap 2D Water ripples

Posted: Tue May 08, 2018 1:42 pm
by Fig
I am not sure but i think linux need to inverse

Code: Select all

Repeat:Until WindowEvent()=0
FlipBuffers()
order...

I changed the code, try again please... :?

Re: Cheap 2D Water ripples

Posted: Tue May 08, 2018 3:17 pm
by Kukulkan
This does not help. Even if you simply do a WindowEvent() to process the events, it still crashes on StopDrawing(). Please do not spend any more time on this.

If someone else confirms the problem with Linux, we may create a bug entry in the forum to let Fred take a look for the reasons.

Re: Cheap 2D Water ripples

Posted: Tue May 08, 2018 6:42 pm
by Erlend
I can confirm it crashes at StopDrawing()

Code: Select all

[19:43:43] [ERROR] Program aborted. (by external library)
[19:43:46] The Program was killed.

Re: Cheap 2D Water ripples

Posted: Tue May 08, 2018 6:50 pm
by Fig
I do not follow the "by the book" way i should poke screen memory, so I feel somewhat responsible...
Does the exemple from 2D drawing subsection in help file, called "DirectScreenDrawing.pb" works for you ?

Maybe it comes from Integer i used instead of Long as you are on a x64 Pb version... ??

Re: Cheap 2D Water ripples

Posted: Tue May 08, 2018 7:11 pm
by Erlend
Hmm no actually it fails too..

There seems to be a bug in the linux branch of PB.

For info I run PB 2.62(x64) on Arch Linux.

Re: Cheap 2D Water ripples

Posted: Tue May 08, 2018 8:56 pm
by HeX0R
If I change from

Code: Select all

For j=0 To #Y-1
to

Code: Select all

For j=0 To #Y-2
it seems to run fine under linux also

Re: Cheap 2D Water ripples

Posted: Tue May 08, 2018 9:40 pm
by Kuron
Does nothing for me, other than show a very tiny white square in the upper left corner of the screen.

Re: Cheap 2D Water ripples

Posted: Wed May 09, 2018 5:31 am
by Fig
Did you left click, Kuron ? I don't know if your reading system can render what's going on with ripples on screen...
It produces concentric circles with a height map in greyish color. (more than 50 grey of shade ^^)

Re: Cheap 2D Water ripples

Posted: Wed May 09, 2018 9:54 am
by wilbert
When using Plot it seems to work on Linux also (at least when I try)

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)
prev_buffer.i=0
cur_buffer.i=#X
Repeat
    Repeat:Until WindowEvent()=0
    FlipBuffers() 
    
    ;copy the current buffer to the screen memory
    StartDrawing(ScreenOutput())
    Box(0,0,#X,#Y,$000000)
    For j=1 To #Y-2
        For ii=1 To #X-2
            i=ii+cur_buffer
            
            ;propagate the wave
            k=ii+prev_buffer
            Buffer(i,j)=((Buffer(k-1,j)+Buffer(k+1,j)+Buffer(k,j-1)+Buffer(k,j+1))*0.5-Buffer(i,j))*#Dampening                
            
            ;copy to the screen
            color.i=Buffer(i,j)*10
            Plot(ii,j,$010101*color)
            
        Next ii
    Next j
    ;display mouse cursor
    DrawText(0,0,"[Escape] to Quit")
    DrawText(0,20,"[Left Click] to poke the surface")
    Box(MouseX(),MouseY(),10,10,$ffffff)
    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
    
    Swap cur_buffer, prev_buffer
    
Until KeyboardPushed(#PB_Key_Escape)