;=======T=======T========================T==========================================;
;Name:      DPKernel StartUp file.
;Author:    Paul Manias.
;Version:   0.5
;Copyright: DreamWorld Productions (c) 1996-1997.  All rights reserved.

	INCDIR	"INCLUDES:"
	INCLUDE	"games/dpkernel.i"

	xref	_main
	xref	_PREFSNAME

	xdef	_DPKBase
	xdef	__XCEXIT
	xdef	_SNDBase
	xdef	_BLTBase
	xdef	_SCRBase
	xdef	_SNDModule
	xdef	_BLTModule
	xdef	_SCRModule

__XCEXIT =	0

	SECTION	gms,CODE

;===================================================================================;
;                            SPECIAL STARTUP CODE
;===================================================================================;

Start:	MOVEM.L	D0-D7/A0-A6,-(SP)
	cmp.l	#"DPKN",d0
	bne.s	.StartFromDOS
	cmp.w	#ID_STARTUP,(a0)
	bne.s	.StartFromDOS
	move.l	ST_DPKBase(a0),_DPKBase
	bra.s	Launch

.StartFromDOS
	move.l	($4).w,a6	;a6 = ExecBase
	lea	DPKName(pc),a1	;a1 = Library name.
	moveq	#$00,d0	;d0 = Any version.
	jsr	-552(a6)	;>> = OpenLibrary()
	move.l	d0,_DPKBase	;ma = Save base.
	beq.s	ErrDPK	;>> = Error, exit.
	move.w	#1,DOS

	move.l	d0,a6	;a6 = DPKBase.
	lea	Exit(pc),a0	;a0 = Pointer to SelfDestruct() cleanup.
	move.l	a7,a1	;a1 = Stack pointer.
	CALL	InitDestruct	;>> = Initialise the call.

	move.l	_PREFSNAME,a0
	CALL	SetUserPrefs
	tst.l	d0
	bne.s	Exit

Launch:	move.l	_DPKBase(pc),a6
	moveq	#MOD_SCREENS,d0
	sub.l	a0,a0
	CALL	OpenModule
	move.l	d0,_SCRModule
	beq.s	Exit
	move.l	d0,a5
	move.l	MOD_ModBase(a5),_SCRBase

	moveq	#MOD_SOUND,d0
	sub.l	a0,a0
	CALL	OpenModule
	move.l	d0,_SNDModule
	beq.s	Exit
	move.l	d0,a5
	move.l	MOD_ModBase(a5),_SNDBase

	moveq	#MOD_BLITTER,d0
	sub.l	a0,a0
	CALL	OpenModule
	move.l	d0,_BLTModule
	beq.s	Exit
	move.l	d0,a5
	move.l	MOD_ModBase(a5),_BLTBase

	move.l	_DPKBase(pc),a6
	jsr	_main

Exit:	move.l	_DPKBase(pc),a6	;a6 = DPKBase
	move.l	_SCRModule(pc),a0
	CALL	Free
	move.l	_BLTModule(pc),a0
	CALL	Free
	move.l	_SNDModule(pc),a0
	CALL	Free

	tst.w	DOS
	beq.s	ErrDPK
	CALL	CloseDPK
ErrDPK:	MOVEM.L	(SP)+,D0-D7/A0-A6
	moveq	#$00,d0
	rts

DOS:		dc.w  0
_DPKBase:	dc.l  0
_SNDBase:	dc.l  0
_BLTBase:	dc.l  0
_SCRBase:	dc.l  0

_SNDModule:	dc.l  0
_BLTModule:	dc.l  0
_SCRModule:	dc.l  0

DPKName:	dc.b  "GMS:libs/dpkernel.library",0
		even

