Haubitze wrote:i add to the update sub an parameter where you can set the number of new drops. so you can make a big storm :D
and i take your updated version, but i think the image version slows down the program.
By all means, do so. The slowdown comes from the fact that you're calculating two buffers into one loop. Use one or the other, I put the image version to test it =D
It could be used to composite the drops with another image. Look:
Code: Select all
#Include "fbgfx.bi"
Using FB
Type wather_t
public:
Declare Constructor( byval As UInteger, byval As uinteger )
Declare Constructor( byval As UInteger, byval as uinteger, byval as fb.image ptr )
Declare Destructor()
Declare Sub update_buffer( byval As Single)
Declare Sub render_buffer()
declare property renderBuffer() as fb.image ptr
private:
m_bitmap as fb.image ptr
buffer1 as single ptr ptr
buffer2 as single ptr ptr
x_size As UInteger
y_size As UInteger
End Type
Constructor wather_t( byval x_size As UInteger, byval y_size As uinteger )
this.x_size=x_size
this.y_size=y_size
buffer1=New single Ptr[x_size]
buffer2=New Single Ptr[x_size]
For i As UInteger=0 To x_size-1
buffer1[i]=New Single[y_size]
buffer2[i]=New Single[y_size]
Next
End Constructor
constructor wather_t( byval x_size as uinteger, byval y_size as uinteger, byval pBitmap as fb.image ptr )
this.x_size=x_size
this.y_size=y_size
buffer1=New single Ptr[x_size]
buffer2=New Single Ptr[x_size]
For i As UInteger=0 To x_size-1
buffer1[i]=New Single[y_size]
buffer2[i]=New Single[y_size]
Next
m_bitmap = pBitmap
end constructor
Destructor wather_t()
For i As UInteger=0 To x_size-1
Delete [] buffer1[i]
Delete [] buffer2[i]
Next
Delete [] buffer1
Delete [] buffer2
buffer1=0
buffer2=0
if( m_bitmap <> 0 ) then
imageDestroy( m_bitmap )
m_bitmap = 0
end if
End Destructor
property wather_t.renderBuffer() as fb.image ptr
return( m_bitmap )
end property
Sub wather_t.update_buffer( byval damping As Single)
buffer2[int(Rnd*x_size)][int(Rnd*y_size)]=1.0
For y As UInteger=1 To y_size-2
For x As UInteger=1 To x_size-2
buffer2[x][y]=Abs(buffer1[x+1][y]+buffer1[x-1][y]+buffer1[x][y+1]+buffer1[x][y-1])/2.0-buffer2[x][y]
buffer2[x][y]=buffer2[x][y]*damping
Next
Next
End Sub
Sub wather_t.render_buffer()
Dim c As UByte
dim as ulong ptr b
dim as integer pitch
if( m_bitmap = 0 ) then
b = screenPtr()
screenInfo( , , , , pitch )
pitch \= sizeOf( ulong )
else
b = cptr( ulong ptr, m_bitmap ) + sizeOf( fb.image ) \ sizeOf( ulong )
pitch = m_bitmap->pitch \ sizeOf( ulong )
end if
'ScreenLock
For y As UInteger=0 To y_size-1
For x As UInteger=0 To x_size-1
c = abs( buffer2[ x ][ y ] * 255 )
b[ ( y * pitch ) + x ] = rgba( c, c, c, c )
'PSet (i,j),RGB(c,c,c)
Next
Next
'ScreenUnLock
'screensync
Swap buffer1,buffer2
End Sub
Randomize timer
dim as integer screenWidth = 1280, screenHeight = 720
screenRes( screenWidth, screenHeight, 32, , fb.gfx_alpha_primitives )
'' Creates an image to be used as background
dim as fb.image ptr background = imageCreate( screenWidth, screenHeight )
'' Load an image into the background buffer. Change to the one you like, the darker the better ;)
bload( "image.bmp", background )
'' Creates a raindrops object
dim as wather_t rainDrops = wather_t( _
screenWidth, screenHeight, imageCreate( screenWidth, screenHeight, rgba( 0, 0, 0, 0 ) ) )
do
screenLock()
'' Draw the background first
put( 0, 0 ), background, pset
'' Update raindrops
rainDrops.update_buffer(0.975)
rainDrops.render_buffer()
'' Composite them with the background
put( 0, 0 ), rainDrops.renderBuffer, add
screenUnlock()
sleep( 1, 1 )
loop until( inkey() <> "" )
imageDestroy( background )
=D