
// Project: Seamless loading test 
// Created: 2017-06-03

seterrormode(2)

SetWindowTitle( "Seamless loading test" )
SetWindowSize( 1920, 1080, 1 )
SetWindowAllowResize( 1 )

// set display properties
SetVirtualResolution( 1920, 1080 ) // doesn't have to match the window
SetSyncRate( 60, 0 )
UseNewDefaultFonts( 1 )


loadimage(200,"sprite.png")
createsprite(200,200)
setspriteanimation(200,150,150,24)
setspritescale(200,2.0,2.0)
playsprite(200,60)
setspritedepth(200,0)
pause=0
finished=0
started=0
demomode=0
imnum=2

do

	if GetRawMouseLeftPressed()=1 or getpointerpressed()=1 then demomode=1
	
	t#=timer()
	angle#=angle#+1.0
	if angle#<0.0 then angle#=359.0-abs(angle#)
	distnum#=400.0
	x# = ((getvirtualwidth()/2)*1.0) + (distnum# * cos(angle#))
	y# = ((getvirtualheight()/2)*1.0) + (distnum# * sin(angle#))
	setspriteposition(200,x#-(getspritewidth(200)/2),y#-(getspriteheight(200)/2))
	print(str(screenfps()))
	print("Stream status = "+str(started))
	if demomode=1 then print("Streaming images in the background - do slow down while images load")
	if demomode=2 then print("AGK standard LoadImage code")
	if demomode=3 then print("Loading images as memblocks and converting to images - 4-6 times faster")
	
	if demomode=1
		rem here is the loading progress
		 rem this part converts the data file into a memblock, then back into an image
		pause=0
		if started=0
			if imnum=1
				imnum=2
			else
				imnum=1
			endif
			if imnum=1 then opentoread(1,"earth_1.PACK")
			if imnum=2 then opentoread(1,"image.dat")
			finished=0
			pause=0
			started=1
			ttest#=timer()
		endif
		
		if started>0
			repeat
				if started=3
					if getimageexists(imnum)=1 then deleteimage(imnum)
					CreateImageFromMemblock(imnum,100)
					deletememblock(100)
					finished=1
				endif
				if started=1
					rem this is the initial code, which opens the file and gets the sizes etc. We then set the flag to show that we are in progress
					bloopstart=1
					started=2
					off=0
					iwidth=ReadInteger(1)
					iheight=ReadInteger(1)
					depth=ReadInteger(1)
					creatememblock(100,12+((iwidth*iheight)*4))
					SetMemblockint(100,off,iwidth)
					off=off+4
					SetMemblockint(100,off,iheight)
					off=off+4
					SetMemblockint(100,off,depth)
					off=off+4
				endif

				if started=2
					t3#=t#+0.016
					for b=1+(bloopstart-1) to iheight
						for a=1 to iwidth
							SetMemblockint(100,off,ReadInteger(1))
							off=off+4
						next a
						if b<iheight
							if timer()>t3#
								pause=1
								bloopstart=b+1
								goto skipper:
							endif
						endif
					next b
					//finished=1
					pause=1
					started=3
				endif
				skipper:

				if finished=1
					closefile(1)
					//if getimageexists(imnum)=1 then deleteimage(imnum)
					//CreateImageFromMemblock(imnum,100)
					//deletememblock(100)
					started=0
					pause=0
					if getspriteexists(1)=0 then createsprite(1,imnum)
					setspriteimage(1,imnum)
				endif
			until pause=1 or finished=1
			print(str(bloopstart) + " / " + str(iheight) + " (" + str(round((bloopstart*1.0/iheight*1.0)*100)) + "%)")
		endif
	endif
	
	if demomode=2
		if imnum=1
			imnum=2
		else
			imnum=1
		endif
		if getimageexists(imnum)=1 then deleteimage(imnum)
		if imnum=1 then loadimage(imnum,"earth_1.png")
		if imnum=2 then loadimage(imnum,"image.jpg")
		if getspriteexists(1)=0 then createsprite(1,imnum)
		setspriteimage(1,imnum)
	endif

	if demomode=3
		if imnum=1
			imnum=2
		else
			imnum=1
		endif
		if getimageexists(imnum)=1 then deleteimage(imnum)
		if imnum=1
			//loadimage(imnum,"earth_1.png")
			CreateMemblockFromFile(imnum,"earth_1.PACK")
			CreateImageFromMemblock(imnum,imnum)
			DeleteMemblock(imnum)
		endif
		if imnum=2
			//loadimage(imnum,"image.jpg")
			CreateMemblockFromFile(imnum,"image.dat")
			CreateImageFromMemblock(imnum,imnum)
			DeleteMemblock(imnum)
		endif
		if getspriteexists(1)=0 then createsprite(1,imnum)
		setspriteimage(1,imnum)
	endif

	if GetRawMouseRightPressed()=1 then end

	sync()
loop


rem here is where the testing module appear - everything before this is rubbish

mode=10

if mode=0
	t#=timer()

	for a=1 to 1
		loadimage(a,"earth_1.png")
		sync()
	next a

	t2#=timer()
endif

if mode=1
	t#=timer()

	for a=1 to 50
		loadimage(a,"earth_1.png")
	next a

	t2#=timer()
endif

if mode=2
	t#=timer()

	for a=1 to 50
		loadimage(a,"image.jpg")
	next a

	t2#=timer()
endif

if mode=3
	loadimage(100,"image.png")
	t#=timer()

	for a=1 to 50
		copyimage(a,100,0,0,getimagewidth(100),getimageheight(100))
	next a

	t2#=timer()
endif

if mode=4
	t#=timer()

	for a=1 to 50
		CreateMemblockFromFile(a,"image.jpg")
	next a

	t2#=timer()
	
endif

if mode=5
	loadimage(100,"image.png")
	CreateMemblockFromimage(100,100)
	t#=timer()

	for a=1 to 50
		CreateImageFromMemblock(a,100)
	next a

	t2#=timer()
	
endif

if mode=6
	 rem this part converts the image back into a data file
	loadimage(100,"image.jpg")
	CreateMemblockFromimage(100,100)
	opentowrite(1,"image.dat")

	t#=timer()
	off=0
	iwidth=GetMemblockint(100,off)
	off=off+4
	iheight=getmemblockint(100,off)
	off=off+4
	depth=getmemblockint(100,off)
	off=off+4
	
	WriteInteger(1,iwidth)
	WriteInteger(1,iheight)
	WriteInteger(1,depth)

	for b=1 to iheight
		for a=1 to iwidth
			//dat=GetMemblockint(100,off)
			writeinteger(1,GetMemblockint(100,off))
			off=off+4
		next a
	next b

	closefile(1)
	t2#=timer()
endif

if mode=7
	 rem this part converts the data file into a memblock, then back into an image
	opentoread(1,"packed.DAT")

	t#=timer()
	off=0
	iwidth=ReadInteger(1)
	iheight=ReadInteger(1)
	depth=ReadInteger(1)
	creatememblock(100,12+((iwidth*iheight)*4))
	SetMemblockint(100,off,iwidth)
	off=off+4
	SetMemblockint(100,off,iheight)
	off=off+4
	SetMemblockint(100,off,depth)
	off=off+4

		for b=1 to iheight
			for a=1 to iwidth
				SetMemblockint(100,off,ReadInteger(1))
				//writeinteger(1,GetMemblockint(100,off))
				off=off+4
			next a
		next b

	closefile(1)
	t#=timer()
	CreateImageFromMemblock(1,100)
	t2#=timer()
endif

if mode=8
	 rem this part converts the data file into a memblock, then back into an image
	opentoread(1,"image.dat")
	counter=0
	
	t#=timer()
	t3#=timer()+0.008
	off=0
	iwidth=ReadInteger(1)
	iheight=ReadInteger(1)
	depth=ReadInteger(1)
	creatememblock(100,12+((iwidth*iheight)*4))
	SetMemblockint(100,off,iwidth)
	off=off+4
	SetMemblockint(100,off,iheight)
	off=off+4
	SetMemblockint(100,off,depth)
	off=off+4

		for b=1 to iheight
			for a=1 to iwidth
				SetMemblockint(100,off,ReadInteger(1))
				//writeinteger(1,GetMemblockint(100,off))
				off=off+4
			next a
			if timer()>t3#
				t3#=timer()+0.008
				//message("FREEZE - loaded " + str((off/4)+0.0/(12+((iwidth*iheight)))) + " percent")
				//print("FREEZE - loaded " + str(off/4) + " of " + str(12+((iwidth*iheight))))
				//sync()
				counter=counter+1
			endif
		next b

	closefile(1)
	//t#=timer()
	CreateImageFromMemblock(1,100)
	t2#=timer()
	message("Took " + str(counter) + " frames")
endif

if mode=9
	loadimage(1,"image.jpg")
	CreateMemblockFromimage(1,1)
	CreateFileFromMemblock("packed.DAT",1)
	deletememblock(1)
	deleteimage(1)
endif

if mode=10
	t#=timer()
	for a=1 to 50
		CreateMemblockFromFile(a,"earth_1.PACK")
		CreateImageFromMemblock(a,a)
	next a
	t2#=timer()
endif

if getimageexists(1)=1 then createsprite(1,1)

do
    print("Loading took " + str(t2#-t#))
    print("(" + str((t2#-t#)/500) + " per image")
    Sync()
loop
