How can I load atlas one time and use it in my andorid app?

Anything libgdx related goes here!

How can I load atlas one time and use it in my andorid app?

Postby zhaoweihao » Wed Sep 04, 2019 3:11 pm

I create an Assets class
Code: Select all
public class Assets {

    public static final String TAG = "Assets";

    public static AssetManager manager;
    public static TextureAtlas textureAtlas;

    public static void create() {
        AbsoluteFileHandleResolver fileHandleResolver = new AbsoluteFileHandleResolver();
        manager = new AssetManager(fileHandleResolver);
        manager.setLoader(TextureAtlas.class, new TextureAtlasLoader(fileHandleResolver));

        load();
    }

    private static void load() {

        //This could be anywhere else
        String fileName = Constant.ALL_COMPONENTS_PATH + "/skeleton.atlas";

        if (!new FileHandle(fileName).exists()) {
            Gdx.app.error(TAG, "invalid file '" + fileName + "'");
            Gdx.app.exit();
        }

        if (!manager.isLoaded(fileName, TextureAtlas.class))
            manager.load(fileName, TextureAtlas.class);

        manager.finishLoading();

        if (manager.isLoaded(fileName, TextureAtlas.class))
            Gdx.app.log(TAG, "Texture loaded!");
        else
            Gdx.app.log(TAG, "Texture not loaded!");

    }

    public static TextureAtlas getTextureAtlas() {
        return textureAtlas;
    }
   

    public static void dispose() {
        LogUtils.d("assets dispose");
        manager.dispose();
    }
}


I extends AndroidFragmentApplication for my Fragment
And new a class extends to ApplicationAdapter

on the create method
Code: Select all
Assets.create();
       atlas = Assets.manager.get(Constant.ALL_COMPONENTS_PATH + "/skeleton.atlas", TextureAtlas.class);


First Time it works

but When I use it in other fragment like this

Code: Select all
atlas = Assets.manager.get(Constant.ALL_COMPONENTS_PATH + "/skeleton.atlas", TextureAtlas.class);


It turns out to be black rectangle ... with the components

*I don't know why I can't upload image , So I upload it on google drive
https://drive.google.com/file/d/1DFTsho ... sp=sharing
zhaoweihao
 
Posts: 2
Joined: Wed Sep 04, 2019 3:01 pm

Re: How can I load atlas one time and use it in my andorid a

Postby evilentity » Wed Sep 04, 2019 3:59 pm

Odds are different fragmens have different opengl contexts, stuff like textures cant be shared. You could perhaps reuse single fragment with libgdx in different places, dunno how would that work.
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4854
Joined: Wed Aug 24, 2011 11:37 am

Re: How can I load atlas one time and use it in my andorid a

Postby zhaoweihao » Thu Sep 05, 2019 3:01 pm

So Can I load all the resources one time and use It everyWhere in my app?Because everytime I call new TextAtlas() method in different fragment will load all the resources again and again...
zhaoweihao
 
Posts: 2
Joined: Wed Sep 04, 2019 3:01 pm

Re: How can I load atlas one time and use it in my andorid a

Postby evilentity » Thu Sep 05, 2019 7:56 pm

You could probably keep the TextureData in memory and recreate the Textures. Perhaps Texture.reload() would work?

You are doing unusual stuff, that will cause unusual problems.
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4854
Joined: Wed Aug 24, 2011 11:37 am


Return to Libgdx

Who is online

Users browsing this forum: Jamez0r, MSN [Bot] and 1 guest