;Load Picture
;------------
;This demo will load in a picture of any size/type and if it is larger than
;the screen width, it scrolls left and right.  This is easily possible due
;to the fact that GMS likes to fill in fields that have been set at zero
;on initialisation, which is great for loading/displaying things like
;pictures.
;
;The benefits of this will become more apparent when you want to do things
;like changing your graphics format from ECS to AGA and vice versa.  The
;benefits for the user are enormous (full graphical editing capabilities,
;if you program correctly).  And you don't even need to change a line of
;code!  Wow!

	opt	o+

	INCLUDE	"exec/exec_lib.i"
	INCLUDE	"games/games_lib.i"
	INCLUDE	"games/games.i"

CALL	MACRO
	jsr	_LVO\1(a6)
	ENDM

SPEED	=	2

	SECTION	"LoadPicture",CODE

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

Start:	MOVEM.L	A0-A6/D1-D7,-(SP)
	move.l	($4).w,a6
	lea	GMS_Name(pc),a1
	moveq	#$00,d0
	CALL	OpenLibrary
	move.l	d0,GMS_Base
	beq.s	.Error_GMS

	move.l	GMS_Base(pc),a6
	sub.l	a0,a0
	CALL	SetUserPrefs

	lea	Picture(pc),a1		;Load picture into background.
	CALL	GetPicInfo
	tst.w	d0
	bne.s	.Error_Picture
	lea	Screen(pc),a0
	move.w	PIC_Planes(a1),GS_Planes(a0)
	move.w	PIC_Width(a1),GS_PicWidth(a0)
	move.w	PIC_Height(a1),GS_PicHeight(a0)
	move.l	PIC_AmtColours(a1),GS_AmtColours(a0)
	move.w	PIC_ScrMode(a1),GS_ScrMode(a0)

	CALL	Add_Screen
	tst.l	d0
	bne.s	.Error_Screen

	move.l	GS_MemPtr1(a0),PIC_Data(a1)
	CALL	LoadPic

	lea	Screen(pc),a0
	CALL	Show_Screen

;---------------------------------------------------------------------------;

	bsr.s	Main	;Go and do the main routine.

;---------------------------------------------------------------------------;

.ReturnToDOS
	move.l	GMS_Base(pc),a6
	lea	Screen(pc),a0
	CALL	Delete_Screen	;Give back screen memory etc.
.Error_Screen
	move.l	GMS_Base(pc),a6
	lea	Picture(pc),a1
	CALL	FreePic	;Give back picture data.
.Error_Picture
	move.l	($4).w,a6
	move.l	GMS_Base(pc),a1
	CALL	CloseLibrary
.Error_GMS
	MOVEM.L	(SP)+,A0-A6/D1-D7
	moveq	#$00,d0
	rts

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

Main:	move.l	GMS_Base(pc),a6
	lea	Screen(pc),a0
	lea	Picture(pc),a2

	moveq	#0,d0	;d0 = Initialise fader.
	moveq	#2,d1	;d1 = Speed of fade.
	move.l	PIC_Palette(a2),a1
	moveq	#$000000,d2
	moveq	#0,d3
	move.l	PIC_AmtColours(a2),d4
.Fade1	CALL	Wait_OSVBL
	CALL	ColourToPalette	;Do the fade routine.
	tst.w	d0	;Has the fade finished yet?
	bne.s	.Fade1	;If not, keep doing it.

	lea	Picture(pc),a1
	moveq	#0,d2
.loop	CALL	Wait_OSVBL
	move.w	GS_ScrWidth(a0),d0	;Only scroll the picture if
	lsr.w	#3,d0	;it is bigger than the actual
	cmp.w	GS_PicWidth(a0),d0	;screen.
	bge.s	.done

	tst.w	d2
	bgt.s	.Right
.Left	tst.w	GS_PicXOffset(a0)
	ble.s	.RRight
.RLeft	moveq	#-SPEED,d2
	bra.s	.scroll
.Right	move.w	GS_PicWidth(a0),d0	;d0 = PicWidth [bytes]
	lsl.w	#3,d0	;d0 = (PicWidth)*8 [pixels]
	sub.w	#320,d0
	cmp.w	GS_PicXOffset(a0),d0	;d0 = Is (Width-320 < PicOffset)?
	ble.s	.RLeft
.RRight	moveq	#SPEED,d2
.scroll	add.w	d2,GS_PicXOffset(a0)
	CALL	Move_Picture

.done	moveq	#JPORT1,d0	;Port 1 (Mouse)
	moveq	#JT_ZBXY,d1
	CALL	Read_JoyPort
	btst	#MB_LMB,d0
	beq.s	.loop
	rts

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

GMS_Name:
	dc.b	"games.library",0
	even
GMS_Base:
	dc.l	0

AMT_PLANES =	4

Screen:	dc.l	GSV1,0
	dc.l	0,0,0	;Screen_Mem1/2/3
	dc.l	0	;Screen link.
	dc.l	0	;Address of palette
	dc.l	0	;Address of rasterlist.
	dc.l	0	;Amt of colours in palette.
	dc.w	320,256	;Screen Width and Height.
	dc.w	0,0	;Picture Width/8 and Height. 
	dc.w	0	;Amt_Planes
	dc.w	0,0	;Screen X/Y offset.
	dc.w	0,0	;Picture X/Y offset.
	dc.l	HSCROLL	;Special attributes.
	dc.w	0	;Screen mode.
	dc.b	INTERLEAVED	;Screen type
	dc.b	0	;Reserved.
	even

Picture	dc.l	PCV1,0	;Version header.
	dc.l	0	;Source data.
	dc.w	0,0	;Width/8, Height.
	dc.w	0	;Amount of Planes.
	dc.l	0	;Amount of colours.
	dc.l	0	;Palette.
	dc.w	0	;Screen mode.
	dc.w	INTERLEAVED	;Screen type.
	dc.l	VIDEOMEM|GETPALETTE|GETVMODE
	dc.l	.File
.File	dc.b	"GAMESLIB:data/IFF.Pic640x256",0
	even
