
	load 'macros.dump'
	include 'driver.equ'

	include 'pl.equ'
;-----------------------------------------------
;
;   Imported addresses
;
;-----------------------------------------------

	IMPORT	X_CopyOneHandle
	IMPORT	X_DisposeThread
	IMPORT	P_CopyObjList
	IMPORT	P_MakePict
	IMPORT	T_CpyOrClrObj
	IMPORT	T_ReThread

;-----------------------------------------------
;
;   Forward addresses and entries
;
;-----------------------------------------------

	ENTRY	P_CopyPLScrap
	ENTRY	P_DelObjList
	ENTRY	P_DelPLScrap
	ENTRY	P_PL2Pict
	ENTRY	T_Dispose



P_ScrapJumps	PROC		EXPORT

	DC.L	P_CopyPLScrap
	DC.L	P_DelPLScrap
	DC.L	P_PL2Pict

		ENDP



P_ScrapData	PROC		EXPORT
		EXPORT	P_QDPict
		EXPORT	P_ConvErr

P_QDPict		DS.B 4
P_ConvErr		DS.B 2

		ENDP




;------------------------------------------------------------------------------
;
; P_CopyPLScrap (ScrapHdl:l) : CopyHdl:l
;

P_CopyPLScrap	PROC		EXPORT

		input  ScrapHdl:l
		output CopyHdl:l
		error  Err

		begin

		call P_CopyObjList,in=(ScrapHdl:l,#1:w),out=(CopyHdl:l),err=(Err)

		return
		ENDP




;-----------------------------------------------------------------------------
;
; P_DelPLScrap (ObjHdl:l,KillData:w)
;

P_DelPLScrap	PROC		EXPORT
			;Using P_ObjData
			;Using P_ScrapData

		input ObjHdl:l,KillData:w
		local ObjPtr:l,NextObj:l

		begin +b

		cpzl P_QDPict
		beq DoIt
		tool _KillPicture,in=(P_QDPict:l)
		stzl P_QDPict

DoIt
		call P_DelObjList,in=(ObjHdl:l,KillData:w,#1:w)

		return
		ENDP





;-----------------------------------------------------------------------------
;
; P_PL2Pict (ObjHdl:l) : PictHdl:l
;

P_PL2Pict	PROC		EXPORT
			;Using P_ScrapData

		input  ObjHdl:l
		output PictHdl:l
		error  Err

		begin +b

		cpzl P_QDPict
		beq DoIt

		moveword P_ConvErr,Err
		bne Exit

		call X_CopyOneHandle,in=(P_QDPict:l),out=(PictHdl:l),err=(Err)
		bra Exit

DoIt
		call P_MakePict,in=(ObjHdl:l),out=(PictHdl:l),err=(Err)

Exit
		return
		ENDP





;-----------------------------------------------------------------------------
;
; P_DelObjList (ObjHdl:l,KillData:w,All:w)
;

P_DelObjList	PROC		EXPORT
			;Using P_ObjData
			;Using P_ScrapData

		input ObjHdl:l,KillData:w,All:w
		local ObjPtr:l,NextObj:l

		begin


WhileLoop
		cpzl ObjHdl
		jeq Exit

		movelong [ObjHdl],ObjPtr
		movelong [ObjPtr],NextObj

		lda KillData
		beq TrashIt

		moveword [ObjPtr]:#P_Type,a
		cmp #P_ALPHA
		bne TryPict

		call T_Dispose,in=(ObjHdl:l,All:w)
		bra TrashIt

TryPict	cmp #P_PICTURE
		bne TrashIt

		tool _KillPicture,in=([ObjPtr]:#P_PictHandle:l)


TrashIt
		tool _DisposeHandle,in=(ObjHdl:l)
		movelong NextObj,ObjHdl
		brl WhileLoop


Exit	return
		ENDP






;----------------------------------------------------------------------------
;
; T_Dispose (ObjHdl:l,All:w)
;
; Can call X_DisposeThread if there are no objects attached to this one.

T_Dispose	PROC		EXPORT
			;Using P_ObjData

		input ObjHdl:l,All:w
		local ObjPtr:l,NThread:l,PThread:l,Ptr:l

		begin +b

		movelong [ObjHdl],ObjPtr
		movelong [ObjPtr]:#P_NThread,NThread
		movelong [ObjPtr]:#P_PThread,PThread

		lda All
		beq NotAll

		cpzl PThread
		beq KillThread
		lda PThread
		and PThread+2
		cmp #$FFFF
		beq Killthread
		brl Exit

NotAll
		lda NThread
		ora NThread+2
		ora PThread
		ora PThread+2
		bne Linked


KillThread
		call X_DisposeThread,in=([ObjPtr]:#P_ParagHdl:l,#1:w)
		brl Exit



Linked
		call T_CpyOrClrObj,in=(ObjPtr:l,#0:w),out=(a:l)

		cpzl PThread
		beq NoPrev
		movelong [PThread],Ptr
		movelong NThread,[Ptr]:#P_NThread
NoPrev

		cpzl NThread
		beq NoNext
		movelong [NThread],Ptr
		movelong PThread,[Ptr]:#P_PThread
NoNext


		cpzl PThread
		beq AtStart
		pushlong PThread
		bra Cont
AtStart	pushlong NThread
Cont	jsl T_ReThread


Exit	return
		ENDP
		END

