;-------T-------T------------------------T---------------------------------;
;Open a screen using an external OBJect file.
;--------------------------------------------
;This demo will open a screen, based on the parameters in an external object
;file.  This allows a VERY high level of external editing that will alter
;how this program behaves, without having to reassemble it.
;
;This feature is highly important as it allows up to 100% configurability of
;your game, which previously has only been available to applications using
;libraries such as MUI.  However, it is even more powerful than this as
;graphic artists could not only create new datasets for your game, but also
;alter the dimensions, colours, resolution, attributes (and more) of your
;BOBs and screens.  Support for code segments is also available, so
;external functions could be altered and improved by programmers.  This will
;ensure that your game has a long lasting future as it keeps up with current
;technology and additions to GMS.
;
;I hope you make the most of external objects, its worth it!

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

CALL	MACRO
	jsr	_LVO\1(a6)
	ENDM

	SECTION	"ObjectTags",CODE

;===========================================================================;
;                             INITIALISE DEMO
;===========================================================================;

	STARTGMS

Start:	MOVEM.L	A0-A6/D1-D7,-(SP)
	move.l	GMSBase(pc),a6
	lea	OBJName(pc),a0
	CALL	LoadObjectFile
	move.l	d0,ObjectBase
	beq	.Error_ObjectFile

	move.l	ObjectBase(pc),a0	;a0 = Object Base.
	lea	TXT_ScreenName(pc),a1	;a1 = The name of the object to get.
	CALL	GetObject	;>> = Get our GameScreen object.
	move.l	d0,Screen	;a0 = Screen tags of our object.
	beq.s	.Error_Screen	;>> = Quit if error.

	move.l	ObjectBase(pc),a0	;a0 = Object Base.
	lea	TXT_PictureName(pc),a1	;a1 = The name of the object to get.
	CALL	GetObject	;>> = Get our GameScreen object.
	move.l	d0,Picture	;>> = Picture tags of our object.
	beq.s	.Error_Screen	;>> = Quit if error.

	move.l	Screen(pc),a0	;a0 = GameScreen tags
	CALL	AddScreen	;>> = Initialise the screen.
	tst.l	d0	;ma = Save pointer to the GameScreen.
	beq.s	.Error_Screen	;>> = Quit if error.

	move.l	Screen(pc),a0
	move.l	Picture(pc),a1
	move.l	GS_MemPtr1(a0),PIC_Data(a1)
	CALL	LoadPic
	tst.l	d0
	beq.s	.Error_Picture

	move.l	Screen(pc),a0
	move.l	Picture(pc),a1
	move.l	PIC_Palette(a1),GS_Palette(a0)
	CALL	UpdatePalette

	CALL	ShowScreen

	bsr.s	Main

.ReturnToDOS
	move.l	GMSBase(pc),a6
	move.l	Picture(pc),a1
	CALL	FreePic
.Error_Picture
	move.l	Screen(pc),a0
	CALL	DeleteScreen
.Error_Screen
	move.l	ObjectBase(pc),a0
	CALL	FreeObjectFile
.Error_ObjectFile
	MOVEM.L	(SP)+,A0-A6/D1-D7
	moveq	#ERR_OK,d0
	rts

;===========================================================================;
;                                MAIN LOOP
;===========================================================================;

Main:	CALL	WaitVBL
	moveq	#JPORT1,d0
	CALL	ReadMouse
	btst	#MB_LMB,d0
	beq.s	Main
	rts

;===========================================================================;
;                                  DATA
;===========================================================================;

ObjectBase:	dc.l  0
OBJName:	dc.b  "GMS:demos/data/OBJ.Screen",0
		even
Screen:		dc.l  0
Picture:	dc.l  0

TXT_ScreenName:	dc.b  "Screen",0
		even
TXT_PictureName	dc.b  "Picture",0
		even

