;=========[ Code for amycoders Watercompo ]============
;
;	by PG / Limited Edition 12/12-97
;	
;	Works with 4 pixels / loop
;
;	a small note:
;	  by making a palette that ranges from
;	  blue (0) to white (127), and from
;	  black (128) to blue (255) you will be
;	  able to leave out the 3 last instr before
;	  the dbra in water_do. You could instead c2p
;	  convert from water_back (or front).
;	  This would give 3.75 instr/pixel. 
;	  the add.l d0,d0 could also be left out but
;	  the water would not be as visable as now.
;	  It would work the same however.
;
;	another note:
;	  as everyone knows you could always put a rept
;	  around the inner loop to gain speed but for
;	  since this isn't really the issue here I left
;	  that one alone.
;
;======================================================

;============[ Water generating code ]=================

BUFFER_W	EQU	256
BUFFER_H	EQU	256
BUFFER_S	EQU	BUFFER_W*BUFFER_H

	section prog,code_f

;--------------[ Water init Routine ]------------------

; destroyed d0/a0-a1

water_init:	Move.l	#water1,water_front
		Move.l	#water2,water_back

		Lea	water_s,a0	;clear buffers
		Lea	water_e+4,a1
		Moveq	#0,d0
.lop		Move.l	d0,(a0)+
		Cmp.l	a0,a1
		Bne	.lop
		
		Rts

;--------------[ Water drop Routine ]------------------

; d0.l x coord
; d1.l y coord

; destroyed a0

water_drop:	Lsl.w	#8,d1
		Move.b	d0,d1

		Move.l	water_back,a0

		Move.w	#$2020,(a0,d1.l)
		Add.l	#BUFFER_W,a0
		Move.w	#$2020,(a0,d1.l)

		Rts

;---------------[ Water do Routine ]-------------------
; a0 ptr to chunky

; destroyed d0-d4/d7/a0-a5

water_do:	Move.l	water_back,a1
		Move.l	water_front,a5
		Move.l	a5,water_back
		Move.l	a1,water_front

		Lea	-1(a1),a2
		Lea	BUFFER_W(a1),a3
		Lea	-BUFFER_W(a1),a4
		Addq.l	#1,a1

		Move.w	#BUFFER_S/4-1,d7	;4 at a time

		Move.l	#$40404040,d2	;sign mask
		Move.l	#$7f7f7f7f,d3	;value mask

		Move.l	#$80808080,d4	;chunky add

.Lop		Move.l	(a4)+,d0	;add boundry pixels
		Add.l	(a3)+,d0
		Add.l	(a2)+,d0
		Add.l	(a1)+,d0

		Lsr.l	#1,d0 
		Sub.l	(a5),d0

		Move.l	d0,d1		;bit 6 = sign (7 = trash)
		And.l	d2,d1		;mask sign

		Add.l	d1,d1		; << 1
		And.l	d3,d0
		Or.l	d1,d0		;Restore Sign

		Lsr.l	#7,d1
		Add.l	d1,d0		;d0 - d0 >> 7 (asr)

		Move.l	d0,(a5)+

		Add.l	d0,d0		;prepare for chunky
		Add.l	d4,d0
		Move.l	d0,(a0)+	;write to chunky

		DBra	d7,.Lop
		Rts

;-------------------[ Data ]---------------------

water_front	dc.l	0
water_back	dc.l	0

	section	data,bss_f

water_s	ds.b	BUFFER_W
water1	ds.b	BUFFER_S
	ds.b	BUFFER_W
water2	ds.b	BUFFER_S
	ds.b	BUFFER_W*2
water_e

;-----------------[ End of code ]-----------------

