I have some better code at home... With Jess's help, its much better now, althought its no longer 20 lines
EDIT: Here it is - but its more than 2- lines. I'll see how much I can compress it...
set display mode 800, 600, 32
sync on
sync rate 0
color backdrop 0
backdrop on
randomize timer()
`MEMBLOCK IMAGE
make memblock 1, 12 + (screen width() * 400 * 4)
write memblock dword 1, 0, screen width() * 0.5
write memblock dword 1, 4, 200
write memblock dword 1, 8, 32
for i = 12 to get memblock size(1)-1 step 4
if ((i - 12) / 4) / memblock dword(1, 0) > memblock dword(1, 4) * 0.5
write memblock dword 1, i, 0xFFFFFFFF
else
write memblock dword 1, i, 0x00000000
endif
next i
make image from memblock 1, 1
sprite 1, 0, screen height() - memblock dword(1, 4), 1
`scale sprite 1, 200
size sprite 1, screen width(), memblock dword(1, 4)
`Number of points in the "sea". Currently the width of the screen
`SEA_POINTS = screen width()
`Now the width of the memblock image
SEA_POINTS = memblock dword(1, 0)
SEA_HEIGHT = memblock dword(1, 4)-1
SEA_WIDTH_BYTES = memblock dword(1, 0) * 4
`Define a Wave Point - currently only height
Type WavePoint
h as float
EndType
`Create array of points - 2D size 2 as it is used for buffering the last frame
dim w(SEA_POINTS, 2) as WavePoint
`i = points in the sea
`j = buffers
for i = 1 to SEA_POINTS
for j = 1 to 2
w(i, j).h = 0.0
next j
next i
`Buffer Values - these are more template, for readability. Just need to know that 1 and 2 are the frame ID's to be swapped
new = 1
old = 2
`Space Key toggle. Makes sure that only 1 "plop" is released per press
`skPressed as boolean : skPressed = 0
`Last Plop - this is for a rain effect...
lastPlop = timer()
`My frame time code
frameTime# = 1.0
startTime = timer()
do
frameTime# = (frameTime# * 0.8) + ((timer() - startTime) * 0.2)
startTime = timer()
`Self explanatory :-)
text 0, 0, "FPS: " + str$(screen fps())
if timer() - lastPlop > 1000
p = rnd(SEA_POINTS - 8) + 4
s# = 20
for i = -4 to 4
if i <> 0
w(p + i, old).h = s# / abs(i)
else
w(p, old).h = s#
endif
next i
lastPlop = timer()
endif
`Swap the buffers
temp = new : new = old : old = temp
`Process the sea
for i = 3 to SEA_POINTS-2
`The new height is a combination of the old surrounding points and the current height.
`The surrounding points act as a kind of blur effect.
`The current height gives it a "velocity" towards zero which makes the wave return to ground level.
`w(i, new).h = w(i-1, old).h + w(i+1, old).h - w(i, new).h
w(i, new).h = ((w(i-2, old).h + w(i-1, old).h + w(i+1, old).h + w(i+2, old).h) * 0.5) - w(i, new).h
`This is a dampening effect. The wave loses 1/32nd of its "power" or height each frame.
w(i, new).h = w(i, new).h - (w(i, new).h / 32.0)
`Check its within range...
if w(i, new).h > memblock dword(1, 4) * 0.5 then w(i, new).h = memblock dword(1, 4) * 0.5
`Draw the wave point. From the bottom of the screen to height 200 + the wave height.
`box i, screen height() - int(200.0 - w(i, new).h), i+1, screen height()
`MEMBLOCK METHOD
`WE ONLY NEED TO DO THIS IF THE HEIGHTS ARE DIFFERENT!!!!
if abs(w(i, old).h - w(i, new).h) < 0.001 then w(i, old).h = w(i, new).h
if w(i, old).h <> w(i, new).h
m = 12 + (i * 4)
oldHeight = (SEA_HEIGHT * 0.5) + w(i, old).h
newHeight = (SEA_HEIGHT * 0.5) + w(i, new).h
if newHeight >= oldHeight
inc m, (SEA_HEIGHT - newHeight) * SEA_WIDTH_BYTES
for n = m to get memblock size(1) - 1 step SEA_WIDTH_BYTES
`Check the current pixel - if its white then we can assume the ones below are too!!
if memblock dword(1, n) = 0xFFFFFFFF
exit
else
`Draw one pixel
write memblock dword 1, n, 0xFFFFFFFF
endif
next n
else
`We were previously at a higher point so we need to erase the higher ones then draw the new ones
`nPoint is the point at which we draw new pixels
nPoint = m + ((SEA_HEIGHT - newHeight) * SEA_WIDTH_BYTES)
`m becomes the highest point, ie the oldHeight
inc m, (SEA_HEIGHT - oldHeight) * SEA_WIDTH_BYTES
`Loop for each row - check where we are and write the correct pixel
for n = m to get memblock size(1) - 1 step SEA_WIDTH_BYTES
if n < nPoint
`Start clearing pixels...
write memblock dword 1, n, 0x00000000
else
`Once we have cleared the pixels above - the ones below are all thats needed to be left
exit
endif
next n
endif
endif
next i
`Turn it into an image :-)
make image from memblock 1, 1
sync
loop