Water is a really nice effect, one of the better tricks around. Its alsopretty simple to code, should take no more than an afternoons work to get agood water routine. First thing you'll need is 2 buffers, for the water. This needs to be anarray of ints, same size as destination buffer. Arrange these in a 2-elementarray, to ease the flipping. Clear these to zero, and you're ready to start.Calculate The New Water Calculating the new water is pretty simple. You'll need a loop thatexecute from 1 to height-1, then 1 to width - 1. At each element for thewater, you'll need to sum the North, South, East, West points from thecurrent water, and divide by 2 Not 4, 2. Then subtract the new waterfor [y] from this. This is your basic smooth function. Now you need toadd the 'harmonic motion' to it. Take the new-water[y], shift it rightx places (x could be 4) and subtract it from the new-water[y].Pseudo-code for this might look like:for y := 1 to height - 1 for x := 1 to width - 1 new-water[y] = ((old-water[y-1] + old-water[y+1] + old-water[y][x-1] + old-water[y][x+1]) / 2) - new-water[y]) new-water[y] -= new-water[y] shr x endendNot too hard to make into working code. Because there are 2 pages, and we areflipping between them, the references to what seem like uncalculated water( - new-water[y]) are ok, because they come around and feed back. This isall it takes to calculate the water! You may also want a part where you setthe 1 pixel wide frame around the water to 0, just to be safe.Paint The WaterWater is also pretty straightforward to paint. In fact some of thetechniques here, you will see later on in the bump-mapping. Back to the taskin hand. We will again need a loop for every pixel on the screen. What weneed to do is calculate a kind-of normal at each pixel. This is simply:offsetx = water[y] - water[y+1]offsety = water[y] - water[y][x+1]Which measures changes in X and Y. 'Colour' is then calculated by128 - offsetx. Clip colour to the 0..255 range. Divide both offsetx andoffsety by 8, and add them to x and y. Now for the tricky bit. You'll need abackground image (or perhaps you can do without ...). You need to lightthe background image by the water. This is done by:offsetx /= 3;offsety /= 8;indexu = offsetx + x;indexv = offsety + y;MulTable[backdrop[indexv*256+indexu]*256+colour];MulTable is another handy lookup table. It simply takes the value of(row*col) >> 8. Pseudo code for this would be :For y := 1 to height - 1 For x := 1 to width - 1 offsetx = water[y] - water[y+1] offsety = water[y] - water[y][x+1] colour = 128 - offsetx trim colour to 0..255 divide offsetx and offsety by 8 add offsetx to x giving indexu add offsety to y giving indexv Plot (backdrop*colour) >> 8, lookup in table EndEndWhat we are effectively doing here is applying fake lighting to the water,then mixing the colours. There are plenty of variations on calculating thenormals. Plenty of room for exploration there. The Water Loop Note it makes a difference what order you do calculations in. Its prettysimple though. You need to:Draw to the waterPaint itCalculate new waterPage flip the waterIf you stick to that, you can't go wrong. If you really want to be smart,you'll use the texturemap lighting info on this page to do make logos and soon ripple. I've even seen it combined with bump-mapping. Water is a veryrewarding effect, well worth coding.