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.
Waiting for loading to finish
Re: Waiting for loading to finish
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")
Re: Waiting for loading to finish
Thanks for that. What I might do is look into setting different events for sprites, sounds etc
Re: Waiting for loading to finish
You can get Type and ObjectID in Procedure Loaded()MrTAToad wrote:Thanks for that. What I might do is look into setting different events for sprites, sounds etc
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")
Type = 1 ObjectID = 5
Type = 3 ObjectID = 10
Re: Waiting for loading to finish
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.
Re: Waiting for loading to finish
I have updated the doc to reflect this.
Re: Waiting for loading to finish
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).
Re: Waiting for loading to finish
If you can illustrate this in a small snippet, it will be perfect for us to take a look.
Re: Waiting for loading to finish
This is the loading event :
and the routine to start the loading (and setup the array so the event knows what to do with the data) :
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
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
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
https://onedrive.live.com/redir?resid=3 ... hoto%2cpng
and sometimes not :
https://onedrive.live.com/redir?resid=3 ... hoto%2cpng