Wonderful 2D Water effects...

Game development specific discussions.
Post Reply
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Wonderful 2D Water effects...

Post by leopardpm »

I am always attracted to 2-D graphical programming that is able to capture 3-D effects, so I practically fell out of my chair when I came across this guys' little water ripple algorithm. Unfortunately, he starts talking about math things that I don't really understand - like 'normals' and things like 'vector3' in C....

I was wondering if anyone could help me with coding this effect in FreeBasic?

here is the link to his 'tutorial' article on indieDB: http://www.indiedb.com/games/questica/f ... ter-though

I am going to start coding now, and will post as I get each piece up...

looking at his first code example, I even get confused:

Code: Select all

left = waterMap1[i-1][j] or 0
this is in C or javascript, but it shouldn't matter... when you 'OR' something with 0 (zero), you just get the same value, right? so this line is the same as:

Code: Select all

left = waterMap1[i-1][j]
I am assuming the brackets delineate the array indexes, right?

This statement confounds me as well:

Code: Select all

watermap2(i,j) = min(max(watermap2(i,j) +.00005, 0), 0)
to me, it is taking the MAX of two values: (watermap2(i,j) +.00005) and 0(zero).... which is always the value of the expression, right?
then it takes the MIN of two values, one of them being 0(zero)... which will always be 1, right?

Oops, I read it wrong.. what the statement does is clamp the value between 0 and 1... duh
Last edited by leopardpm on Aug 26, 2016 1:51, edited 2 times in total.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

ok, here is my starting point... lots of issues....

Code: Select all


dim as ushort watermap1(400,400), watermap2(400,400)


screenres 600,600,32



 ' set the basic level of the watermaps
for i as integer = 1 to 399
    for j as integer = 1 to 399
        watermap1(i,j) = 0
        watermap2(i,j) = 0
    next j
next i

' put in some disturbances
for r as integer = 1 to 50
    watermap1(rnd*398+1, rnd*398+1) = 4
next r

cls

' main loop
do
    for i as integer = 1 to 399
        for j as integer = 1 to 399
            dim as single l = watermap1(i-1,j)
            dim as single r = watermap1(i+1,j)
            dim as single u = watermap1(i,j-1)
            dim as single d = watermap1(i,j+1)
            watermap2(i,j) = (l + r + u + d) / 2 - watermap2(i,j)
            if watermap2(i,j) > 0 then watermap2(i,j) -= 1
            ' watermap2(i,j) = min(max(watermap2(i,j) +.00005, 0), 0)
        next j
    next i
    
    screenlock
    for i as integer = 1 to 399
        for j as integer = 1 to 399
            pset(100+i, 100+j), rgb(0,0,100+(150*watermap2(i,j)))
        next j
    next i
    screenunlock
    
    ' swap arrays
    for i as integer = 1 to 399
        for j as integer = 1 to 399
            swap watermap1(i,j), watermap2(i,j)
        next j
    next i
loop until inkey = chr(27)

end
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

ok, NOW we are cookin'!

Click on the blue area to make a splash...
ESC to end program

Code: Select all


dim shared as single watermap1(400,400), watermap2(400,400)

Type mouse
    As Integer res
    As Integer x, y, wheel, clip
    Union
        buttons As Integer
        Type
            Left:1 As Integer
            Right:1 As Integer
            middle:1 As Integer
        End Type
    End Union
End type

    dim as mouse m

screenres 600,600,32



 ' set the basic level of the watermaps
for i as integer = 1 to 399
    for j as integer = 1 to 399
        watermap1(i,j) = 0
        watermap2(i,j) = 0
    next j
next i

' put in some disturbances
for r as integer = 1 to 50
    watermap1(rnd*398+1, rnd*398+1) = 1
next r

cls

' main loop
do
    for i as integer = 1 to 399
        for j as integer = 1 to 399

            dim as single l = watermap1(i-1,j)
            dim as single r = watermap1(i+1,j)
            dim as single u = watermap1(i,j-1)
            dim as single d = watermap1(i,j+1)
            
            watermap2(i,j) = (l + r + u + d) / 2 - watermap2(i,j)
    
            ' watermap2(i,j) = min(max(watermap2(i,j) +.00005, 0), 1)
            watermap2(i,j) = watermap2(i,j) - .00005
            if watermap2(i,j) < 0 then watermap2(i,j) = 0
            if watermap2(i,j) > 1 then watermap2(i,j) = 1
        next j
    next i
    
    screenlock
    for i as integer = 1 to 399
        for j as integer = 1 to 399
            pset(100+i, 100+j), rgb(0,0,50+(200*watermap2(i,j)))
        next j
    next i
    screenunlock
    
    ' swap arrays
    for i as integer = 1 to 399
        for j as integer = 1 to 399
            swap watermap1(i,j), watermap2(i,j)
        next j
    next i
    
    m.res = GetMouse( m.x, m.y, m.wheel, m.buttons, m.clip )
    if m.left then watermap1(m.x-100,m.y-100) = 1
loop until inkey = chr(27)

end
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

ok, pretty happy so far... but what the guy says in this part is completely foreign to me (WTF is 'vector3'?):
I also wanted to add in normal maps so the water reacts to light the way it should (I can do a separate post just about normal maps if people really want it). Calculating the normal maps is pretty easy once you have an array of all the heights of the water, all you have to do is:


Vector3 Normal = new Vector3(
((leftUp - rightUp) +
2 * (left - right) +
(leftDown - rightDown)) / 3 + .333f,
((leftUp - leftDown) +
2 * (up - down) +
(rightUp - rightDown)) / 3 + .333f,
1);
where each variable is the just value of the pixel at that relative location. The (/3 + .333f) part is to scale down the normal because otherwise the waves were far too pronounced. Now I just had to throw that new array into a texure and throw it in the shader and we're in business!
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

still haven't figured out the 'normal' stuff, definitely need help there

but, converted everything to integers and working on some edge disturbances... I really like the effect so far!

Code: Select all


dim shared as short watermap1(400,400), watermap2(400,400)

Type mouse
    As Integer res
    As Integer x, y, wheel, clip
    Union
        buttons As Integer
        Type
            Left:1 As Integer
            Right:1 As Integer
            middle:1 As Integer
        End Type
    End Union
End type

    dim as mouse m

screenres 600,600,32



 ' set the basic level of the watermaps
for i as integer = 1 to 399
    for j as integer = 1 to 399
        watermap1(i,j) = 0
        watermap2(i,j) = 0
    next j
next i

' put in some disturbances
for r as integer = 1 to 50
    watermap1(rnd*396+2, rnd*396+2) = 32000
next r

cls

' main loop
do
' put in some 'edge' disturbances
for r as integer = 1 to 1
    watermap1(rnd*396+2, 1) = 20000
    watermap1(rnd*396+2, 399) = 20000
    watermap1(1, rnd*396+2) = 20000
    watermap1(399, rnd*396+2) = 20000
next r
    
    
    for i as integer = 1 to 399
        for j as integer = 1 to 399

            dim as short l = watermap1(i-1,j)
            dim as short r = watermap1(i+1,j)
            dim as short u = watermap1(i,j-1)
            dim as short d = watermap1(i,j+1)
            
            watermap2(i,j) = (l + r + u + d) / 2 - watermap2(i,j)
    
            ' watermap2(i,j) = min(max(watermap2(i,j) +.00005, 0), 1)
            watermap2(i,j) = watermap2(i,j) - 1
            if watermap2(i,j) < 0 then watermap2(i,j) = 0
            if watermap2(i,j) > 32000 then watermap2(i,j) = 32000
        next j
    next i
    
    screenlock
    for i as integer = 1 to 399
        for j as integer = 1 to 399
            dim as single floater = watermap2(i,j) / 32000
            pset(100+i, 100+j), rgb(250*floater,100+(150*floater),210)
        next j
    next i
    screenunlock
    
    ' swap arrays
    for i as integer = 1 to 399
        for j as integer = 1 to 399
            swap watermap1(i,j), watermap2(i,j)
        next j
    next i
    
    m.res = GetMouse( m.x, m.y, m.wheel, m.buttons, m.clip )
    'locate 3,3 : print m.left
    if m.left = 1 then 
        if m.x > 100 and m.x < 500 and m.y > 100 and m.y < 500 then watermap1(m.x-100,m.y-100) = 32000
    end if
    sleep 10
    
loop until inkey = chr(27)

end
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

testing posting an animated GIF to the forum so can show the water effects that I want to achieve...

Image
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

Soooo, here are the parts I need assistance with:

(1) how to do 'normals' etc like he does...

(2) how to extend the water ripples to adjacent tiles (I think I can figure this one out... eventually)

(3) how to bounce the ripples off land? He does it, but doesn't say how... apparently it was so easy for him he didn't think it needed explanation :(

(4) once got it all done, then optimize the sheeeet out of it (I think I can probably manage this one as well)

any ideas out there?
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

my eyes are blurry staring at these waves... I can't even tell if it looks like water anymore...

Here is my latest incarnation:
(1) has 'land'
(2) the land emits 'waves' from the shores (red pixels)
(3) mouse click still will make rain drop ripples

to me it looks like a top-down view of the ocean from very high... not what I am going for... still don't know how to do all those thing in previous post....

Code: Select all


dim shared as ushort watermap1(400,400), watermap2(400,400)
dim shared as ubyte waterMask(400,400)

Type mouse
    As Integer res
    As Integer x, y, wheel, clip
    Union
        buttons As Integer
        Type
            Left:1 As Integer
            Right:1 As Integer
            middle:1 As Integer
        End Type
    End Union
End type

dim as mouse m


screenres 600,600,32

Dim image As Any Ptr = ImageCreate( 400, 400, RGB(0,0,0) )

randomize timer

 ' set the basic level of the watermaps
for i as integer = 2 to 399
    for j as integer = 2 to 399
        watermap1(i,j) = 0
        watermap2(i,j) = 0
        pset image, (i,j), rgb(10,40,130) 'water
    next j
next i

' make some 'land' circles
for r as integer = 1 to 5
    dim as integer size = 30 + rnd*30
    dim as integer x = rnd*396+2
    dim as integer y = rnd*396+2
    circle image, (x, y),size, rgb(127,106,0),,,,F
    circle image, (x, y),size, rgb(255,0,0)
next r

' make some 'land' squares
for r as integer = 1 to 5
    dim as integer size = 30 + rnd*30
    dim as integer x = rnd*396+2
    dim as integer y = rnd*396+2
    line image, (x,y) - step(size,size), rgb(127,106,0), bf
    line image, (x,y) - step(size,size), rgb(255,0,0), b
    
next r
    
' put in border
    line image, (0, 0) - (399,399), rgb(255,0,0), b
    line image, (1, 1) - (398,398), rgb(255,0,0), b
    line image, (3, 3) - (396,396), rgb(255,0,0), b
    
' put in some initial rain drops
for r as integer = 1 to 50
    dim as integer x = rnd*396+2
    dim as integer y = rnd*396+2
    if point(x,y, image) = rgb(10,40,130) then ' if water, then....
        watermap1(rnd*396+2, rnd*396+2) = 32000
    end if
next r

cls
    put (100,100),image,  pset

    
' main loop
do
    ' put in some 'edge' disturbances
'    for r as integer = 1 to 1
'        watermap1(rnd*396+2, 2) = 32000
'        watermap1(rnd*396+2, 398) = 32000
'        watermap1(2, rnd*396+2) = 32000
'        watermap1(398, rnd*396+2) = 32000
'    next r
'    
    screenlock
    put (100,100),image,  pset
    
' emit entire coastline...one big wave
'    if rnd > .9 then
'        for i as integer = 3 to 396
'            for j as integer = 3 to 396
'                dim as integer col = point(i+100,j+100)
'                if col = rgb(255,0,0) then
'                    watermap1(i,j) = 0
'                    watermap1(i-2,j) = 0
'                    watermap1(i+2,j) = 0
'                    watermap1(i,j-2) = 0
'                    watermap1(i,j+2) = 0
'                    'if rnd > .99 then 
'                        if point(i-2+100,j+100) = rgb(10,40,130) then watermap1(i-2,j) = 15000 + rnd* 15000
'                        if point(i+2+100,j+100) = rgb(10,40,130) then watermap1(i+2,j) = 15000 + rnd* 15000
'                        if point(i+100,j-2+100) = rgb(10,40,130) then watermap1(i,j-2) = 15000 + rnd* 15000
'                        if point(i+100,j+2+100) = rgb(10,40,130) then watermap1(i,j+2) = 15000 + rnd* 15000
'                    'end if
'                end if
'            next j
'        next i
'    end if
    
    ' place some random shore emitters....
    for i as integer = 3 to 396
        for j as integer = 3 to 396
            dim as integer col = point(i+100,j+100)
            if col = rgb(255,0,0) then
                watermap1(i,j) = 0
                watermap1(i-2,j) = 0
                watermap1(i+2,j) = 0
                watermap1(i,j-2) = 0
                watermap1(i,j+2) = 0
                if rnd > .98 then 
                    if point(i-2+100,j+100) = rgb(10,40,130) then watermap1(i-2,j) = (18 + rnd* 10) * 1000
                    if point(i+2+100,j+100) = rgb(10,40,130) then watermap1(i+2,j) = (18 + rnd* 10) * 1000
                    if point(i+100,j-2+100) = rgb(10,40,130) then watermap1(i,j-2) = (18 + rnd* 10) * 1000
                    if point(i+100,j+2+100) = rgb(10,40,130) then watermap1(i,j+2) = (18 + rnd* 10) * 1000
                end if
            end if
        next j
    next i
    
    
    ' propogate waves....
    
    dim as single decay = 1
    
    for i as integer = 1 to 398
        for j as integer = 1 to 398
            dim as integer col = point(i+100,j+100)
            
            if col = rgb(10,40,130) then ' if water, then....
                dim as short l = watermap1(i-1,j)
                dim as short r = watermap1(i+1,j)
                dim as short u = watermap1(i,j-1)
                dim as short d = watermap1(i,j+1)
                
                dim as single floater = (l + r + u + d) / 2 - watermap2(i,j)
        
                if floater < decay then 
                    watermap2(i,j) = decay
                else
                    if floater > 32000 then
                        watermap2(i,j) = 32000
                    else
                        watermap2(i,j) = floater - decay
                    end if
                end if
                
            end if
        next j
    next i
    

    for i as integer = 1 to 399
        for j as integer = 1 to 399
            if point(100+i,100+j) = rgb(10,40,130) then ' if water, then....
                dim as single floater = watermap2(i,j) / 32000
                pset(100+i, 100+j), rgb(10+(100*floater),40+(100*floater),130)
            end if
        next j
    next i
    screenunlock
    
    ' swap arrays
    for i as integer = 1 to 399
        for j as integer = 1 to 399
            swap watermap1(i,j), watermap2(i,j)
        next j
    next i
    
    m.res = GetMouse( m.x, m.y, m.wheel, m.buttons, m.clip )
    'locate 3,3 : print m.left
    if m.left = 1 then 
        if m.x > 105 and m.x < 495 and m.y > 105 and m.y < 495 then
            watermap1(m.x-100,m.y-100) = 32000
        end if
    end if
    sleep 1
    
loop until inkey = chr(27)

end
dafhi
Posts: 1645
Joined: Jun 04, 2005 9:51

Re: Wonderful 2D Water effects...

Post by dafhi »

I like the code above the picture. Nice fps .. Neat how it creates near-perfect circles. Maybe after some sleep I can understand things a bit
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Wonderful 2D Water effects...

Post by dodicat »

When you look at the ocean from high above (an aeroplane), it looks as if it doesn't move at all.
Almost a still life snapshot.
It is only when you are on it you know it moves.

But a nice simulation anyway.
BasicCoder2
Posts: 3908
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Wonderful 2D Water effects...

Post by BasicCoder2 »

Because you are copying from one array to another without any intermediate processing a faster way would be to swap pointers. No matter how large the array a pointer swap only swaps two values.
I don't know how to swap array pointers in FreeBasic but you can do it with a UDT

Code: Select all

type WATER
    dim as ushort map(400,400)
end type

dim shared as WATER water1,water2
dim shared as ubyte waterMask(400,400)


Type mouse
    As Integer res
    As Integer x, y, wheel, clip
    Union
        buttons As Integer
        Type
            Left:1 As Integer
            Right:1 As Integer
            middle:1 As Integer
        End Type
    End Union
End type

dim as mouse m


screenres 600,600,32

Dim image As Any Ptr = ImageCreate( 400, 400, RGB(0,0,0) )

randomize timer

 ' set the basic level of the watermaps
for i as integer = 2 to 399
    for j as integer = 2 to 399
        water1.map(i,j) = 0
        water2.map(i,j) = 0
        pset image, (i,j), rgb(10,40,130) 'water
    next j
next i

' make some 'land' circles
for r as integer = 1 to 5
    dim as integer size = 30 + rnd*30
    dim as integer x = rnd*396+2
    dim as integer y = rnd*396+2
    circle image, (x, y),size, rgb(127,106,0),,,,F
    circle image, (x, y),size, rgb(255,0,0)
next r

' make some 'land' squares
for r as integer = 1 to 5
    dim as integer size = 30 + rnd*30
    dim as integer x = rnd*396+2
    dim as integer y = rnd*396+2
    line image, (x,y) - step(size,size), rgb(127,106,0), bf
    line image, (x,y) - step(size,size), rgb(255,0,0), b
    
next r
    
' put in border
    line image, (0, 0) - (399,399), rgb(255,0,0), b
    line image, (1, 1) - (398,398), rgb(255,0,0), b
    line image, (3, 3) - (396,396), rgb(255,0,0), b
    
' put in some initial rain drops
for r as integer = 1 to 50
    dim as integer x = rnd*396+2
    dim as integer y = rnd*396+2
    if point(x,y, image) = rgb(10,40,130) then ' if water, then....
        water1.map(rnd*396+2, rnd*396+2) = 32000
    end if
next r

cls
    put (100,100),image,  pset

    
' main loop
do

    screenlock
    put (100,100),image,  pset

    
    ' place some random shore emitters....
    for i as integer = 3 to 396
        for j as integer = 3 to 396
            dim as integer col = point(i+100,j+100)
            if col = rgb(255,0,0) then
                water1.map(i,j) = 0
                water1.map(i-2,j) = 0
                water1.map(i+2,j) = 0
                water1.map(i,j-2) = 0
                water1.map(i,j+2) = 0
                if rnd > .98 then 
                    if point(i-2+100,j+100) = rgb(10,40,130) then water1.map(i-2,j) = (18 + rnd* 10) * 1000
                    if point(i+2+100,j+100) = rgb(10,40,130) then water1.map(i+2,j) = (18 + rnd* 10) * 1000
                    if point(i+100,j-2+100) = rgb(10,40,130) then water1.map(i,j-2) = (18 + rnd* 10) * 1000
                    if point(i+100,j+2+100) = rgb(10,40,130) then water1.map(i,j+2) = (18 + rnd* 10) * 1000
                end if
            end if
        next j
    next i
    
    
    ' propogate waves....
    
    dim as single decay = 1
    
    for i as integer = 1 to 398
        for j as integer = 1 to 398
            dim as integer col = point(i+100,j+100)
            
            if col = rgb(10,40,130) then ' if water, then....
                dim as short l = water1.map(i-1,j)
                dim as short r = water1.map(i+1,j)
                dim as short u = water1.map(i,j-1)
                dim as short d = water1.map(i,j+1)
                
                dim as single floater = (l + r + u + d) / 2 - water2.map(i,j)
        
                if floater < decay then 
                    water2.map(i,j) = decay
                else
                    if floater > 32000 then
                        water2.map(i,j) = 32000
                    else
                        water2.map(i,j) = floater - decay
                    end if
                end if
                
            end if
        next j
    next i
    

    for i as integer = 1 to 399
        for j as integer = 1 to 399
            if point(100+i,100+j) = rgb(10,40,130) then ' if water, then....
                dim as single floater = water2.map(i,j) / 32000
                pset(100+i, 100+j), rgb(10+(100*floater),40+(100*floater),130)
            end if
        next j
    next i
    screenunlock
    

    
    ' swap arrays
   ' for i as integer = 1 to 399
    '    for j as integer = 1 to 399
            'swap watermap1(i,j), watermap2(i,j)
            swap water1,water2
     '   next j
   ' next i
    
    m.res = GetMouse( m.x, m.y, m.wheel, m.buttons, m.clip )
    'locate 3,3 : print m.left
    if m.left = 1 then 
        if m.x > 105 and m.x < 495 and m.y > 105 and m.y < 495 then
            water1.map(m.x-100,m.y-100) = 32000
        end if
    end if
    sleep 1
    
loop until inkey = chr(27)

end
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

BasicCoder, that makes too much sense - was wondering how to optimize that part - thanks! In fact, there are quite a few optimizations that can be done, but I am waiting until I get the routine working right.... do you know anything about 'normals' and the things the author (see link in first post) is talking about? Totally confounds me.... and I hope that it can be done without accessing the video card because I really like this water animation he has.

Also, any thoughts on how the routine might propagate from one tile to the next, without being too cumbersome?

initially, I am thinking to go through all the map tiles on screen and remember all the ones with water in them. Then, run all 3 parts of the routine on every water tile (first, apply the algo to all the water tiles, then draw all of them, then swap all of the water arrays of each tile)... seems kinda clunky, but I really don't understand fully how the routine works to propagate the waves outward so I am unsure about 'when' I can start processing the next tile...

it is pretty neat how it results in an expanding circle from a central 'disturbance', I just don't get 'how' though
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

I am also interested in modifying the routine to show direction of water travel, ie: like a river flowing all in the same direction (pretty much)
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

i really like the water effect that he has, but after reading his article more, he mentions shaders and textures... makes me think he is utilizing the video card for final processing to get it to look so good... and already the amount of processing load on the CPU with what I have is much too great (on my pokey laptop I am getting between 10 - 24 fps, can probably maybe double that with optimizing things like changing all the point and pset commands to direct screen access routines, but even that is too much)... unless I can figure out a way to make it work much quicker, the pretty results are just not worth the cpu time.... hmmm, maybe spreading it out to a few frames (5?) after optimizing it might actually be ok... sure would be nice to have good looking animated water + dynamic ripples and wakes happening!

I have found that you can tweak the various adjustments indefinitely trying to get the 'perfect' effect.... its a great time suck! I worked on the color for over an hour alone!
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Wonderful 2D Water effects...

Post by leopardpm »

dang pointers get me everytime... always get confused on how to do assignments and arithmetic on them... here is my issue:

I am iterating through an image, via a pointer, and I want to save all the 'pointers' (not the value they point to, but the address of where that value is in memory) in an array of pointers

Code: Select all

dim as any ptr testmap

const RIVER      = rgb(  0,128,160)
const SHALLOWS   = rgb(  0, 96,128)
CONST SEA        = rgb(  0, 96,255)
CONST SHORE      = rgb(255,  0,  0)
CONST MAGIC_PINK = rgb(255,  0,255)

testmap = bmp_load("test map 320 x 320.bmp")
If testmap = NULL Then Print "bmp_load failed" : sleep : end

' analyze test map....
    Dim pitch As Integer
    Dim pixels As Any Ptr
'' Get enough information to iterate through the pixel data.
    If 0 <> ImageInfo( testmap, ,,, pitch, pixels ) Then
        Print "unable to retrieve image information." : Sleep : End
    End If
'' find all the 'shore' pixels, the ones right next to water
dim as integer NumOfShores = 0

dim shared as uinteger ptr ShoreLine(102400) ' enough to store all the pixels in image, way overkill

For y As Integer = 1 To 318
    Dim row As UInteger Ptr = pixels + y * pitch
    dim rowAbove as uinteger ptr = pixels + (y-1) * pitch
    dim rowBelow as uinteger ptr = pixels + (y+1) * pitch
    
    For x As Integer = 1 To 318
        dim as integer isShore = 0
        dim as integer col = row[x]
        if col <> RIVER and col <> SHALLOWS and col <> SEA then
            col = row[x-1]
            if col = RIVER or col = SHALLOWS or col = SEA then isShore = 1
            col = row[x+1]
            if col = RIVER or col = SHALLOWS or col = SEA then isShore = 1
            col = rowAbove[x]
            if col = RIVER or col = SHALLOWS or col = SEA then isShore = 1
            col = rowBelow[x]
            if col = RIVER or col = SHALLOWS or col = SEA then isShore = 1
        end if
        if isShore = 1 then
            row[x] = SHORE ' turn all shores RED
            NumOfShores += 1
            ShoreLine(NumOfShores) = row[x]      <------- this is the problem statement, I am trying to 'save' the pointer itself... not working
        end if
    Next x
Next y

    cls
    locate 2,2 : print "Num of Shores = ";NumOfShores
    put (100,100),testmap,  pset
sleep

for sl as integer = 1 to NumOfShores
    ShoreLine(sl) = MAGIC_PINK                      <------ which makes this not work....
next sl      '          I am trying to use the pointer to now change the value at that memory location..                                                                     
maybe using the asterisk operator... dang it... will have to check... yet again... friggin slow brain.... never get pointers...

YAY! I figured it out - needed BOTH the '@' operator in the first line that'saves' the pointer in the array of pointers, THEN needed to use the '*'(asterisk) when actually trying to change the value of the memory where to pointer points to... this is 'dereferencing' a pointer, right?
Post Reply