Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the immediate use of async images, once loaded #3022

Merged
merged 1 commit into from
Feb 15, 2024

Conversation

T1mL3arn
Copy link
Contributor

@T1mL3arn T1mL3arn commented Feb 3, 2024

When I want to use async assets

<assets path="assets" embed="false" preload="false"/>
openfl.Assets.loadBitmapData(asset_id, true).onComplete(bd ->
{
	trace('image is loaded');
	FlxAssets.getBitmapData(asset_id);
});

I get this

image is loaded
ERROR: IMAGE asset "asset.png" exists, but only asynchronously'

because Flixel doesn't use cache when it gets bmd from openfl assets for some reason, I believe it is a bug.

--- update

example is valid for html5

@Geokureli
Copy link
Member

Geokureli commented Feb 9, 2024

This seems like a good change, I'd like to poke around the history and verify that there's no valid reason for this arg to be false in the first place (it's an optional arg which makes it was intentional for some reason) , but it seems like it's a good change.

The only issue is that this is gonna conflict with this PR: #2982, and this change may affect how I approach that other PR, so it just needs some consideration

@Geokureli Geokureli added this to the 5.7.0 milestone Feb 9, 2024
@T1mL3arn
Copy link
Contributor Author

there's no valid reason for this arg to be false

At least on openfl's side it is true by default.

@Geokureli
Copy link
Member

I have one theory as to why it's false, I fear that if it's true it will return the same bitmap instance on each call rather than fetching a new copy. This means that editing one bitmap will effect all other sprites using the same path.

this is just a guess, but it's something I would like checked before merging this

@T1mL3arn
Copy link
Contributor Author

This means that editing one bitmap will effect all other sprites using the same path.

Yes, it works that way.

@Geokureli
Copy link
Member

Geokureli commented Feb 15, 2024

I'm unable to reproduce this issue due to the following reasons:
the preload attribute does not actually affect asset tags in Project.xml

<assets path="assets" embed="false" preload="false"/>

it can only be used in library tags, which forces you to use the lib name in asset_id like so: "libName:assets/psth/to/image.png", and then if I try openfl.Assets.loadBitmapData(asset_id, true).onComplete(bd->{}); it returns an error 'could not find a library named "libName"'.
but if I use libraries in my project.cml and do: openfl.Assets.loadLibrary(asset_id).onComplete(bd->{}); then I'm able to load images in that library via FlxAssets.getBitmapData(asset_id) after it completes, despite useCache being false

@T1mL3arn
Copy link
Contributor Author

T1mL3arn commented Feb 15, 2024

I updated the original post - example is valid for html5. On sys targets assets are treat as local files and get re-read from filesystem when they are not cached (at least how I understand it from Lime sources).

@Geokureli
Copy link
Member

it turns out the part i was missing was embed="false", preload does in fact do nothing in asset tags and I was able to reproduce the issue with both: <assets path="assets" embed="false" preload="false"/> and <assets path="assets" embed="false" preload="true"/>

@Geokureli
Copy link
Member

Geokureli commented Feb 15, 2024

This means that editing one bitmap will effect all other sprites using the same path.

Yes, it works that way.

So this doesn't seem to be the case, in the following example:

import flixel.FlxSprite;

class AsyncImageTestState extends flixel.FlxState
{
    inline static var PATH = "async-assets/images/haxe.png";
    
    override function create()
    {
        super.create();
        
        openfl.Assets.loadBitmapData(PATH, true).onComplete(
            function drawSprites(_)
            {
                for (i in 0...3)
                {
                    final sprite = new FlxSprite(10 + 110 * i, 10);
                    sprite.loadGraphic(PATH, false, 0, 0, true);
                    add(sprite);
                }
                // edit the first sprite, see if all change
                final bitmap = (cast members[0]:FlxSprite).graphic.bitmap;
                bitmap.fillRect(bitmap.rect, 0xFFff0000);
            }
        );
    }   
}

only the first sprite is modified, meaning unique is honored despite the use of caching. so this should be good

@Geokureli Geokureli merged commit 27324ef into HaxeFlixel:dev Feb 15, 2024
16 checks passed
@T1mL3arn T1mL3arn deleted the patch-2 branch February 15, 2024 14:57
@Geokureli Geokureli changed the title Allow to use async images that was already loaded Allow the immediate use of async images, once loaded Feb 24, 2024
@Geokureli Geokureli added the Assets Pertains to using and including assets label Feb 24, 2024
Geokureli added a commit that referenced this pull request Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Assets Pertains to using and including assets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants