3
\$\begingroup\$

It's easy to load a simple text file from the internal:

Gdx.files.internal("data/colors.txt");

But I currently use an AssetManager to load assets that are defined in an array of AssetDescriptor<?>s while a loading screen (with a progress indicator) is shown. So I'd like to use something like this in my array of descriptors:

...
new AssetDescriptor<_type_>(Gdx.files.internal("data/colors.txt"), _type_.class),
...

I'd like to load the file through an asset manager, so the overall progress can be displayed as the data/colors.txt file can get very large and therefore take longer to load (I use an tool to automatically generate a very long list of color definitions: RRR,GGG,BBB,AAA, each in the range of 0-255).

Is there a good way of doing this?

\$\endgroup\$
2
\$\begingroup\$

I searched the wiki for an answer and found that there is no Loader for text files (see list here).

So I followed the instructions on the same page to write my own loader (the instructions can be found here). This is what I came up with:


The Text class is basically a wrapper around a simple String:

package mygame.assets;

import com.badlogic.gdx.files.FileHandle;

public class Text {

    private String string;

    public Text() {

        this.string = new String("".getBytes());

    }

    public Text(byte[] data) {

        this.string = new String(data);

    }

    public Text(String string) {

        this.string = new String(string.getBytes());

    }

    public Text(FileHandle file) {

        this.string = new String(file.readBytes());

    }

    public Text(Text text) {

        this.string = new String(text.getString().getBytes());

    }

    public void setString(String string) {

        this.string = string;

    }

    public String getString() {

        return this.string;

    }

    public void clear() {

        this.string = new String("".getBytes());

    }

}

The TextLoader class is the corresponding loader class for an asset of type Text:

package mygame.assets.loaders;

import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetLoaderParameters;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.AsynchronousAssetLoader;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;

import mygame.assets.Text;

public class TextLoader extends AsynchronousAssetLoader<Text, TextLoader.TextParameter> {

    public TextLoader(FileHandleResolver resolver) {

        super(resolver);

    }

    Text text;

    @Override
    public void loadAsync(AssetManager manager, String fileName, FileHandle file, TextParameter parameter) {

        this.text = null;
        this.text = new Text(file);

    }

    @Override
    public Text loadSync(AssetManager manager, String fileName, FileHandle file, TextParameter parameter) {

        Text text = this.text;
        this.text = null;

        return text;

    }

    @SuppressWarnings("rawtypes")
    @Override
    public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, TextParameter parameter) {

        return null;

    }

    public static class TextParameter extends AssetLoaderParameters<Text> {

    }

}

Then, to register the Text asset and its TextLoader, in the create() method of the main class:

@Override
public void create() {

    // ...

    // Assuming that the main class contains the global AssetManager

    assetManager.setLoader(
            Text.class,
            new TextLoader(
                    new InternalFileHandleResolver()
            )
    );

    // ...

}

To use it:

assetLoader.load( new AssetDescriptor< Text >( "data/colors.txt", Text.class, new TextLoader.TextParameter() );
String string = assetLoader.get( "data/colors.txt", Text.class ).getString();
// Process the string

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

As you automatically generate these color definitions, maybe you could generate it instead of loading from a file. If the problem loading is the size of the file, you could consider using compression. But as you wanna try with assets loader, I'll add some links that could be useful.

To manage these color definition assets you could write your own loader.

Also you could consider a custom json serialization.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.