Cheap 2D Water ripples

Advanced game related topics
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Cheap 2D Water ripples

Post 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)
Last edited by Fig on Tue May 08, 2018 1:43 pm, edited 9 times in total.
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Cheap 2D Water riddles

Post by davido »

@Fig,
Very nice, thank you for sharing. :D
Amazing result with so little code.
DE AA EB
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Cheap 2D Water riddles

Post 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... :?
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Cheap 2D Water ripples

Post 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?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Cheap 2D Water ripples

Post 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
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Cheap 2D Water ripples

Post by Kukulkan »

Hi,

no, that does not help. Still the same crash... Maybe someone can confirm that on Linux?
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Cheap 2D Water ripples

Post 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... :?
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Cheap 2D Water ripples

Post 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.
User avatar
Erlend
Enthusiast
Enthusiast
Posts: 102
Joined: Mon Apr 19, 2004 8:22 pm
Location: NORWAY

Re: Cheap 2D Water ripples

Post 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.
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Cheap 2D Water ripples

Post 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... ??
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
Erlend
Enthusiast
Enthusiast
Posts: 102
Joined: Mon Apr 19, 2004 8:22 pm
Location: NORWAY

Re: Cheap 2D Water ripples

Post 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.
User avatar
HeX0R
Addict
Addict
Posts: 979
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Cheap 2D Water ripples

Post 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
User avatar
Kuron
Addict
Addict
Posts: 1626
Joined: Sat Oct 17, 2009 10:51 pm
Location: Pacific Northwest

Re: Cheap 2D Water ripples

Post by Kuron »

Does nothing for me, other than show a very tiny white square in the upper left corner of the screen.
Best wishes to the PB community. Thank you for the memories. ♥️
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Cheap 2D Water ripples

Post 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 ^^)
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Cheap 2D Water ripples

Post 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)
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply