Waiting for loading to finish

Everything else that doesn't fall into one of the other categories.
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

Waiting for loading to finish

Post by MrTAToad »

Is it possible to detect when a graphic (or sound) has been loaded ? I need to get the size of a graphic, and of course, it is currently possible for the image loading command to finish after the getting the size for the graphic, which is, of course, not desired.

I have tried a while loop until either a good or bad load is reported, but that just locks everything up.

I dont synchronous loading as I want to be able to load things at any time, and not just at a fixed point.
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: Waiting for loading to finish

Post by Fred »

You need to use a callback with BindEvent() to know when the image is loadedas shown below:

Code: Select all

  Procedure Loaded(Type, Filename$, ObjectId)

    ; Display the image in a new window
    OpenWindow(#PB_Any, 10, 10, 300, 300, "Image", #PB_Window_SizeGadget)
      ImageGadget(#PB_Any, 0, 0, ImageWidth(ObjectId), ImageHeight(ObjectId), ImageID(ObjectId))
    
  EndProcedure
  
  Procedure LoadingError(Type, Filename$, ObjectId)
    Debug Filename$ + ": loading error"
  EndProcedure
  
  ; Register the loading event before calling any resource load command
  BindEvent(#PB_Event_Loading, @Loaded())
  BindEvent(#PB_Event_LoadingError, @LoadingError())
  
  LoadImage(0, "Data/SpiderBasicLogo.png")
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

Re: Waiting for loading to finish

Post by MrTAToad »

Thanks for that. What I might do is look into setting different events for sprites, sounds etc
Comtois
Posts: 40
Joined: Mon Feb 24, 2014 11:07 pm

Re: Waiting for loading to finish

Post by Comtois »

MrTAToad wrote:Thanks for that. What I might do is look into setting different events for sprites, sounds etc
You can get Type and ObjectID in Procedure Loaded()
Type = 3 for Sprite
Type = 2 for sound
Type = 1 for Image
Fred, it would be fine to write all type possible in doc.

And some constants are missed in the doc for BindEvent() :
#PB_Event_Loading
#PB_Event_LoadingError
#PB_Event_RenderFrame

Code: Select all

Procedure Loaded(Type, Filename$, ObjectId)
  Debug "Type = " + Type + " ObjectID = " + ObjectId
EndProcedure

Procedure LoadingError(Type, Filename$, ObjectId)
  Debug Filename$ + ": loading error"
EndProcedure

; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loaded())
BindEvent(#PB_Event_LoadingError, @LoadingError())

LoadImage(5, "Data/SpiderBasicLogo.png")
LoadSprite(10, "Data/SpiderBasicLogo.png")
run this code and you get :
Type = 1 ObjectID = 5
Type = 3 ObjectID = 10
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

Re: Waiting for loading to finish

Post by MrTAToad »

I have found that trying to get a sprites size inside the loading event sometimes fails (with a value undefined), ie the loading hasn't finished before the sprite size request.
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: Waiting for loading to finish

Post by Fred »

I have updated the doc to reflect this.
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

Re: Waiting for loading to finish

Post by MrTAToad »

It seems that the loading event is called at the start of the loading process, rather than at the end (as the documentation states), as I have noticed that sometimes a graphics width (called in the loading event) can be read and at other times its "undefined" (as though the graphic hasn't loaded by the time the procedure is called).
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: Waiting for loading to finish

Post by Fred »

If you can illustrate this in a small snippet, it will be perfect for us to take a look.
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

Re: Waiting for loading to finish

Post by MrTAToad »

This is the loading event :

Code: Select all

Procedure Loading(type, fileName.s,objectID)			
	Define *Element.__loadingList
	Define spr.__sprite
	
	*Element.__loadingList= LastElement(loadingList())
	If *Element<>0
		Select *Element\type
			Case	#AREA_SPRITES
											sprites(*Element\id)\id=*Element\id
											sprites(*Element\id)\width=SpriteWidth(sprites(*Element\id)\id)
											sprites(*Element\id)\height=SpriteHeight(sprites(*Element\id)\id)
											
											SETSPRITEANIM(*Element\id,sprites(*Element\id)\width,sprites(*Element\id)\height)
											
											Debug "Sprite Width : "
											Debug sprites(*Element\id)\width
											
											Debug "Sprite Height : "
											Debug sprites(*Element\id)\height
											
		EndSelect
									
		; Delete loading item
		LastElement(loadingList())
		DeleteElement(loadingList())
	EndIf
	;__loadGood+1
	; EndIf
EndProcedure
and the routine to start the loading (and setup the array so the event knows what to do with the data) :

Code: Select all

Procedure LOADANIM(fileName.s,index,width,height)
	Define tempFilename.s
	Define g.i
	Define *Element.__loadingList
    
	If index<0 Or index>=#MAX_SPRITES
		SETERROR(#CMP_INVALID_INDEX)
		ProcedureReturn #False
	Else	
			ExpandSprites(index)
			
			If Len(fileName)>0
				*Element = AddElement(loadingList())
  			If *Element<>0
  				*Element\type=#AREA_SPRITES
  				*Element\id=index
  				If width<0 And height<0
  					*Element\info1=#False
  				Else
  					*Element\info2=#True
  				EndIf
  				
  				*Element\info2=width
  				*Element\info3=height
    		EndIf
								
				tempFileName=#SPRITEPATH+fileName
				LoadSprite(index,tempFileName,#PB_Sprite_AlphaBlending | #PB_Sprite_PixelCollision)		
				
				ProcedureReturn #True
			EndIf
		EndIf
EndProcedure
Sometimes the loading returns the sprite size :

https://onedrive.live.com/redir?resid=3 ... hoto%2cpng

and sometimes not :

https://onedrive.live.com/redir?resid=3 ... hoto%2cpng
Post Reply