;---------------------------------------------------------
;
; General purpose animation system.
; Copyright (c) 1991 J.P.Garner. This source code may
; only be repeated in a program released into the public
; domain. No program containing this code, or parts of
; this code, may be distributed or sold as a commercial
; program as shareware, or with a magazine or in any
; other form. This code is not to be printed without the
; author's consent.
;	
; In other words.... Use it in your demos folks!
;
;---------------------------------------------------------
;
setanims:
	move.w #0,anim
	move.w #0,alastflag
	move.l #0,alastgraphic
	move.l #0,alastoffset
	move.l #0,alasttable
	rts
;
;---------------------------------------------------------
; Main loop codes ....
;---------------------------------------------------------
doanims:
	move anim,d0	;Each anim is drawn every three
	add #1,d0	;frames so we can interleave 3.
	cmp #3,d0	;Works out which anim to do (0-2)
	bcs daok
	moveq #0,d0
daok:
	move d0,anim	;Current anim number.
	add d0,d0
	add d0,d0
	lea animtables,a1	;Table of table adresses.
	adda.l d0,a1	;Table pointer.
	move.l (a1),a4	;a4 points to anim structure.
;
	move.l 6(a4),d1	;Screen offset.
	move.l drawscreen,a0
	adda.l d1,a0	;a0 now correct.
;
	move.l alastoffset,a2	;Got to plot last on this screen.
	adda.l drawscreen,a2
	move.l alastgraphic,a3	;Address of graphic.
	move.l alasttable,a5	;Table of last graphic.
	move alastflag,d6	;Check it was really plotted.
;
	move.l d1,alastoffset	;Save offset for this frame.
	move.l a4,alasttable	;Save table for this frame.
;
	move 2(a4),d7	;Frames. Zero means none.
	move d7,alastflag
	tst d7
	beq dadone	;Quit if no anim.
;
	move (a4),d0	;Load counter.
	add #1,d0	;Update counter.
	cmp d7,d0
	bcs danowrap
	moveq #0,d0
danowrap:
	move d0,(a4)
	add d0,d0
	add d0,d0
	move.l $14(a4,d0),a1	;Address of data.
	move.l a1,alastgraphic	;Save last graphic.
;
; d7 holds flag. a0 screen. a1 graphic
; d6 holds old flag. a2 screen. a3 graphic
;
;---------------------------------------------------------
;
dadone:
	btst #14,dmaconr+custom	;Check blitter inactive.
jwb1:
	btst #14,dmaconr+custom
	bne jwb1
;
	move #$ffff,bltafwm+custom	;Set up blitter.
	move #$ffff,bltalwm+custom
	move #0000,bltamod+custom
	move #0000,bltcon1+custom
;
	move $4(a4),bltcon0+custom	;Set blitter for this anim
	move $12(a4),bltdmod+custom
;
;---------------------------------------------------------
;
doanimnow:
	move.w $10(a4),d0	;Load planes.
	tst d7
	beq dooldanim	;Jump if no anim.
	subq #1,d0	;Correct for DBF.
;
blitanimloop1:
	btst #14,dmaconr+custom	;Wait for blitter.
jwb2:
	btst #14,dmaconr+custom	;This part is a waste of
	bne jwb2		;time, you'd normally do
;		 another job now.
	move.l a0,bltdpth+custom	;Load blitter pointers.
	move.l a1,bltapth+custom
	adda.l #10240,a0		;Next bitplane.
	adda.l $a(a4),a6		;Next bitplane.
	move.w $e(a4),bltsize+custom	;Start blitter.
;
	dbf d0,blitanimloop1		;Loop for next bitplane.
;
;---------------------------------------------------------
;
dooldanim:
	tst d6	;Is there an old anim to do?
	beq animdone
;
	btst #14,dmaconr+custom	;As above.
jwb3:
	btst #14,dmaconr+custom
	bne jwb3
;
	move $4(a5),bltcon0+custom  ;a5 holds last frame's table.
	move $12(a5),bltdmod+custom ;Set blitter for last frame.
;
	move.w $10(a5),d0
	subq #1,d0
blitanimloop2:
	btst #14,dmaconr+custom	;This is just like above.
jwb4:
	btst #14,dmaconr+custom
	bne jwb4
;
	move.l a2,bltdpth+custom
	move.l a3,bltapth+custom
	adda.l #10240,a2
	adda.l $a(a5),a3
	move.w $e(a5),bltsize+custom
;
	dbf d0,blitanimloop2
;
animdone:
	rts
;
;---------------------------------------------------------
;
anim:
	dc.w 0
;
alastflag:
	dc.w 0
alastgraphic:
	dc.l 0
alastoffset:
	dc.l 0
alasttable:
	dc.l 0
;
animtables:		;This is a list of pointers
	dc.l anim1,anim2,anim3		;to anim structures.
;
;---------------------------------------------------------
;
; This is how you build up the anim instructions ....
;
animx:
	dc.w 0	;00: Counter:	set this zero	
	dc.w 1	;02: Frames:	Number of frames,
		;		0 means no anim.
	dc.w $09f0	;04: Bltcon0:	This should be set
		;		to $x9f0, where x
		;		is the number of
		;		pixels of shift
		;		you want.
	dc.l 0	;06: Offset:	This is the offset
		;		from the start of
		;		the bitplane. For
		;		pixel accurate
		;		positioning use
		;		shift (above).
		;		Offset = (lines
		;		down * 40) + words
		;		across.
		;		Words across is
		;		the lowest whole
		;		number when
		;		words = pixels/16.
		;		The remainder is
		;		the shift value.
	dc.l 0	;0a: Length:	This is the length
		;		of one bitplane.
		;		Length = words
		;		wide * 2 * lines
		;		high.
	dc.w 0	;0e: Bltsize:	This is the length
		;		of one bitplane
		;		encoded for the
		;		blitter.
		;		Bltsize = words
		;		wide + (64 * lines
		;		high).
	dc.w 1	;10: Planes:	This is the number
		;		of bitplanes that
		;		need to be blitted
	dc.w 2	;12: Mod:	This is the number
		;		of bytes of modulo
		;		required.
		;		Mod = screen width
		;		- graphic width in
		;		bytes, and is a
		;		multiple of two.
		;		Screenwidth is
		;		normally 40.
;
; dc.l frame1	;14:		This is just a
; dc.l frame2	;		list of pointers
;     .	;		to each frame.
;     .
;     .
;     .
; dc.l frame n 
;
;---------------------------------------------------------
;
anim1:
	dc.w 0,8,$09f0	;Here are some example structures.
	dc.l 0,32
	dc.w 1025,4,38
	dc.l f1,f2,f3,f4,f5,f6,f7,f8
;
anim2:
	dc.w 0,13,$09f0
	dc.l 1280,32
	dc.w 1025,4,38
	dc.l f1,f2,f3,f4,f5,f6,f7,f6,f5,f4,f3,f2,f1
;
anim3:
	dc.w 0,8,$09f0
	dc.l 2560,32
	dc.w 1025,4,38
	dc.l f1,f2,f3,f4,f5,f6,f7,f8
;
f1: blk.b 2*16*4,%11000000	;These graphics are just
f2: blk.b 2*16*4,%01100000	;vertical bars, so you can
f3: blk.b 2*16*4,%00110000	;see it in action. You'll
f4: blk.b 2*16*4,%00011000	;need to replace this lot 
f5: blk.b 2*16*4,%00001100	;when you incorporate your
f6: blk.b 2*16*4,%00000110	;own stuff.
f7: blk.b 2*16*4,%00000011
f8: blk.b 2*16*4,%10000001

; ---------------- end of listing ------------------------
