Synchronous asset loading in HTML backend

10 views
Skip to first unread message

Guillermo Rodriguez Garcia

unread,
Jan 14, 2024, 1:36:36 PM1/14/24
to PlayN
Hi all,

The PlayN overview in the playn.io mentions several times that the HTML5 backend does not support synchronous asset loading. However, the HTML5 backend implements getImageSync, and it seems to work just fine. Are there other issues or limitations that we should be aware of ?

Thanks,

Guillermo

Michael Bayne

unread,
Jan 14, 2024, 1:54:01 PM1/14/24
to pl...@googlegroups.com
Image loading on HTML is somewhat more complicated than on other platforms.

When you load an image, either via getImage or getImageSync, you immediately get back an Image object, but that image object might not be "ready". A ready image has its underlying image data, so it can be rendered, and you can ask it for its width and height and you will get correct values.

Fully async image loading is cumbersome, and oftentimes you just need to know the width and height of an image synchronously (so that you can lay out a UI for example) and if the image data itself comes back a short time later, that's fine. Attempting to render an unready image will just render nothing. So you'd have a blank spot in your UI for a moment while the image loads, but it's not the end of the world.

Because of that, the HTML backend supports a halfway measure that provides getImageSync that works "well enough". It allows you to provide an image manifest, which tells the platform the dimensions of every image that you plan to load, so the platform can return an Image which provides valid width and height information and then simply does not render any image data until the data has completed loading asynchronously. The expectation is that you would add a custom build step to your game that scanned all the images you'll be using and generates some metadata file which is then bundled with your app. Then you just load this metadata file (or have it already if you generate it in code) when your app starts up, and from then on, you can load all of your images using getImageSync and your life is simpler.

So the HTML backend supports Assets.getImageSync _iff_ you provide an image manifest. Otherwise it will throw an exception if you try to call getImageSync.


--

---
You received this message because you are subscribed to the Google Groups "PlayN" group.
To unsubscribe from this group and stop receiving emails from it, send an email to playn+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/playn/7547f89d-f4f2-49f9-b55f-edfb7336e61en%40googlegroups.com.


--

Guillermo Rodriguez Garcia

unread,
Jan 15, 2024, 6:59:31 PM1/15/24
to PlayN
Hi Michael,

I see. I had done a quick test before posting and getImageSync seemed to work, but this must have been a mistake on my side (due to caching perhaps).
Now with your explanation and after looking more closely at the code everything is clear.

Thanks,

Guillermo
Reply all
Reply to author
Forward
0 new messages