It is currently Thu Jul 02, 2020 8:31 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Cheap 2D Water ripples
PostPosted: Mon May 07, 2018 5:16 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Apr 30, 2009 5:23 pm
Posts: 328
Location: Côtes d'Azur, France
Code:
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)

_________________
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS


Last edited by Fig on Tue May 08, 2018 1:43 pm, edited 9 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water riddles
PostPosted: Mon May 07, 2018 7:28 pm 
Offline
Addict
Addict

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1766
Location: Uttoxeter, UK
@Fig,
Very nice, thank you for sharing. :D
Amazing result with so little code.

_________________
DE AA EB


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water riddles
PostPosted: Mon May 07, 2018 9:43 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Apr 30, 2009 5:23 pm
Posts: 328
Location: Côtes d'Azur, France
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Tue May 08, 2018 8:39 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jun 06, 2005 2:35 pm
Posts: 1239
Location: germany
Hm. On my Kubuntu with PB 5.46 LTS (x64) it immediately crashes on StopDrawing(). Maybe we found another PB bug for Linux?


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Tue May 08, 2018 10:59 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3617
Location: Netherlands
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:
*line+4
into
Code:
*line+3

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Tue May 08, 2018 11:23 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jun 06, 2005 2:35 pm
Posts: 1239
Location: germany
Hi,

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


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Tue May 08, 2018 1:42 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Apr 30, 2009 5:23 pm
Posts: 328
Location: Côtes d'Azur, France
I am not sure but i think linux need to inverse
Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Tue May 08, 2018 3:17 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jun 06, 2005 2:35 pm
Posts: 1239
Location: germany
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Tue May 08, 2018 6:42 pm 
Offline
User
User
User avatar

Joined: Mon Apr 19, 2004 8:22 pm
Posts: 91
Location: NORWAY
I can confirm it crashes at StopDrawing()

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


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Tue May 08, 2018 6:50 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Apr 30, 2009 5:23 pm
Posts: 328
Location: Côtes d'Azur, France
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Tue May 08, 2018 7:11 pm 
Offline
User
User
User avatar

Joined: Mon Apr 19, 2004 8:22 pm
Posts: 91
Location: NORWAY
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Tue May 08, 2018 8:56 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Sep 20, 2004 7:12 am
Posts: 583
Location: Hell
If I change from
Code:
For j=0 To #Y-1

to
Code:
For j=0 To #Y-2

it seems to run fine under linux also

_________________
Link dead?
Change h3x0r.ath.cx into hex0rs.coderbu.de and all will be fine.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Tue May 08, 2018 9:40 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Oct 17, 2009 10:51 pm
Posts: 1387
Location: Nashville
Does nothing for me, other than show a very tiny white square in the upper left corner of the screen.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Wed May 09, 2018 5:31 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Apr 30, 2009 5:23 pm
Posts: 328
Location: Côtes d'Azur, France
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Cheap 2D Water ripples
PostPosted: Wed May 09, 2018 9:54 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3617
Location: Netherlands
When using Plot it seems to work on Linux also (at least when I try)
Code:
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)

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye