*
* EW.asm
*
* creates a "exploding window"-effect
* by patching Int's "OpenWindow()" and "CloseWindow()"
*
* based on a 10k C-program by (someone)?
*
* this one for a68k is a *little* bit shorter
* and xxxxxx  ("better" removed due to some problems...)
*
* written & no © 1989 by Olli
*
* a68k ew.asm
* blink ew.o SC SD ND
*
* have fun, use in whatever you want...
* it would be nice if you were so kind to always
* remember who has developed this original code
* during many hard nights...
*
* simply type "1> ew" from CLI or double-click from WB
* no need to RUN, no need to lie...
*
*
* direct comments, contributions, ... to
*
* Oliver Wagner
* Landsberge 5
* 4322 Sprockhövel
* West Germany
*
* no email (I'm not that rich)
*
* 28.10.89: first midnight try
* 29.10.89: sorry, use of "Forbid()" seems to be necessary
* 30.10.89: a second call will remove it
* 30.10.89: nasty-pixel-bug fixed: use Move() instead of
*	    directly writing to cp_x, cp_y (sigh)
*	    Move() seems to recalc some more rp-values...
* 31.10.89: startup-msg
* 31.10.89: finally, wb-bug fixed: you may NOT use DOS until
*	    you received the WBStartup-Msg because DOS will
*	    think the WBMsg is a file-packet...Guru!
*	    solution: not using Exit(), but receive msg by hand...

	section "satisfaction",code

	move.l	4,a6
	lea	intname(pc),a1
	lea	intbase(pc),a5
	jsr	-408(a6)
	move.l	d0,(a5)
	move.l	d0,a0
	move.l	-202(a0),d0
	cmp.l	#$f80000,d0	    * already installed?
	bge.s	noinmem
	move.l	d0,a1
	move.l	-8(a1),-202(a0)     * repatch Int
	move.l	-4(a1),-70(a0)
	move.l	#end-begin,d0
	moveq	#newopen-begin,d1
	sub.l	d1,a1
	jsr	-210(a6)            * free patch
	lea	emsg1(pc),a3
	moveq	#emsg2-emsg1,d6
	bra.s	quit
noinmem:
	lea	gfxname(pc),a1
	jsr	-408(a6)
	move.l	d0,4(a5)
	lea	begin(pc),a4
	lea	end(pc),a1
	sub.l	a4,a1
	move.l	a1,d0
	moveq	#0,d1
	move.l	d0,d7
	jsr	-198(a6)              * allocpatch
	move.l	d0,a5
	move.l	d0,a1
	move.l	a4,a0
	move.l	d7,d0
	jsr	-624(a6)
	jsr	(a5)                  * call patchinstall
	lea	emsg(pc),a3
	moveq	#emsg1-emsg,d6
quit:
	moveq	#0,d4
	move.l	4,a6
	lea	dosname(pc),a1
	jsr	-408(a6)
	move.l	d0,a6
	move.l	d0,a2
	jsr	-60(a6)
	move.l	d0,d7
	moveq	#0,d5
	move.l	d7,d1
	bne.s	nowb
	move.l	4,a6
	move.l	$114(a6),a1             * get that old msg
	lea	$5c(a1),a0
	jsr	-384(a6)
	jsr	-372(a6)
	move.l	d0,d4
	lea	conn(pc),a1
	move.l	a1,d1
	move.l	#1006,d2
	move.l	a2,a6
	jsr	-30(a6)
	move.l	d0,d5
	move.l	d0,d1
	beq.s	ret
	move.l	d0,d7
nowb:
	lea	msg(pc),a0
	move.l	a0,d2
	moveq	#emsg-msg,d3
	jsr	-48(a6)
	move.l	d6,d3
	move.l	d7,d1
	move.l	a3,d2
	jsr	-48(a6)
ret:
	tst.l	d5
	beq.s	goodbye
	moveq	#127,d1
	jsr	-198(a6)
	move.l	d5,d1
	jsr	-36(a6)

goodbye:
	tst.l	d4
	beq.s	nomsg
	move.l	4,a6
	jsr	-132(a6)
	move.l	d4,a1
	jsr	-378(a6)
nomsg:
	moveq	#0,d0
	rts
conn:
	dc.b	"con:100/60/166/61/EW by Ølli",0
intname:
	dc.b	"intuition.library",0
gfxname:
	dc.b	"graphics.library",0
dosname:
	dc.b	"dos.library",0
msg:
	dc.b	10,$9b,"1;3;33m EXPLODING WINDOWS",10
	dc.b	$9b,"0m by Ølli 1989",10," just "
emsg:
	dc.b	"installed...",10,10
emsg1:
	dc.b	"removed...",10,10
emsg2:
	even

* beginning of patch (rest of prog will be freed)

* patch Int's vectors

begin:
	move.l	intbase(pc),a0
	lea	oldopen(pc),a1
	lea	newopen(pc),a2
	move.l	-202(a0),(a1)
	move.l	a2,-202(a0)
	lea	oldclose(pc),a1
	lea	newclose(pc),a2
	move.l	-70(a0),(a1)
	move.l	a2,-70(a0)
	rts
oldopen:
	dc.l	'Olli'   * sorry, couldn't resist
oldclose:
	dc.l	'1989'

* the new "OpenWindow()"

newopen:
	movem.l a0/a2-a6/d2-d7,-(sp)
	move.l	a0,a4
	move.l	30(a4),d0
	bne.s	foundscreen
	jsr	-210(a6)
foundscreen:
	move.l	d0,a5
	lea	$54(a5),a5          * get Rastport
	move.b	$1c(a5),d0          * old drawmode
	move.l	d0,a3
	move.l	4,a6
	jsr	-132(a6)            * Forbid() [sorry]
	move.l	gfxbase(pc),a6
	move.l	a5,a1
	moveq	#2,d0
	jsr	-354(a6) ; SetDrMd

	moveq	#0,d4
	moveq	#0,d5
	move.w	4(a4),d4
	move.w	6(a4),d5
	asr.w	#1,d4
	asr.w	#1,d5
	add.w	(a4),d4
	add.w	2(a4),d5
	move.w	4(a4),d2
	move.w	6(a4),d3
	asr.w	#3,d2
	asr.w	#2,d3
	subq.w	#1,d2
	subq.w	#1,d3

	move.l	d4,d6
	subq.w	#1,d4
	move.l	d5,d7
	subq.w	#1,d5

	movem.w d2-d7,-(sp)
	bsr.s	loop1
	movem.w (sp)+,d2-d7
	bsr.s	loop1

	move.l	a3,d0
	ext.w	d0
	move.l	a5,a1
	jsr	-354(a6)
	move.l	4,a6
	jsr	-138(a6)
	movem.l (sp)+,a0/a2-a6/d2-d7
	move.l	oldopen(pc),a1
	jmp	(a1)

loop1:
	bsr.s	drawframe
	tst.w	d2
	beq.s	loop1a
	subq.w	#4,d4
	addq.w	#4,d6
	subq.w	#1,d2
loop1a:
	tst.w	d3
	beq.s	loop1b
	subq.w	#2,d5
	addq.w	#2,d7
	subq.w	#1,d3
loop1b:
	tst.w	d2
	bne.s	loop1
	tst.w	d3
	bne.s	loop1
	rts

* draws a frame

drawframe:
	move.w	d4,d0
	move.w	d5,d1
	move.l	a5,a1
	jsr	-240(a6)        * Move() fixed due to nasty pixel bug
	move.l	a5,a1
	move.w	d6,d0
	move.w	d5,d1
	jsr	-246(a6)
	move.w	d6,d0
	move.w	d7,d1
	move.l	a5,a1
	jsr	-246(a6)
	move.w	d4,d0
	move.w	d7,d1
	move.l	a5,a1
	jsr	-246(a6)
	move.w	d4,d0
	move.w	d5,d1
	move.l	a5,a1
	jmp	-246(a6)

* the new close routine

newclose:
	movem.l a0/a2-a6/d2-d7,-(sp)
	move.l	46(a0),a5
	lea	$54(a5),a5
	move.b	$1c(a5),d0          * save old rp-style
	move.l	d0,a3
	moveq	#0,d4
	moveq	#0,d5
	move.w	4(a0),d4
	move.w	6(a0),d5
	move.l	d4,d6
	move.l	d5,d7
	add.w	8(a0),d6
	add.w	10(a0),d7
	move.w	8(a0),d2
	move.w	10(a0),d3
	asr.w	#3,d2
	asr.w	#2,d3
	subq.w	#1,d2
	subq.w	#1,d3
	move.l	oldclose(pc),a1
	jsr	(a1)
	move.l	4,a6
	jsr	-132(a6)
	move.l	gfxbase(pc),a6
	moveq	#2,d0
	move.l	a5,a1
	jsr	-354(a6)
	movem.w d2-d7,-(sp)
	bsr.s	loop2
	movem.w (sp)+,d2-d7
	bsr.s	loop2
	move.l	a3,d0
	ext.w	d0
	move.l	a5,a1
	jsr	-354(a6)
	move.l	4,a6
	jsr	-138(a6)
	movem.l (sp)+,a0/a2-a6/d2-d7
	rts

loop2:
	bsr	drawframe
	tst.w	d2
	beq.s	loop2a
	addq.w	#4,d4
	subq.w	#4,d6
	subq.w	#1,d2
loop2a:
	tst.w	d3
	beq.s	loop2b
	addq.w	#2,d5
	subq.w	#2,d7
	subq.w	#1,d3
loop2b:
	tst.w	d2
	bne.s	loop2
	tst.w	d3
	bne.s	loop2
	rts

intbase:
	dc.l	'Olli'
gfxbase:
	dc.l	'1989'
end:
	END
