All Downloads are FREE. Search and download functionalities are using the official Maven repository.

games.rednblack.talos.runtime.assets.AtlasAssetProvider Maven / Gradle / Ivy

package games.rednblack.talos.runtime.assets;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.ObjectMap;
import games.rednblack.talos.runtime.utils.ShaderDescriptor;

public class AtlasAssetProvider extends BaseAssetProvider {
    private final TextureAtlas atlas;

    public AtlasAssetProvider (final TextureAtlas atlas) {
        this.atlas = atlas;

        setAssetHandler(TextureRegion.class, new AssetHandler() {
            @Override
            public TextureRegion findAsset (String assetName) {
                return atlas.findRegion(assetName);
            }
        });

        setAssetHandler(Sprite.class, new AssetHandler() {
            @Override
            public Sprite findAsset (String assetName) {
                return atlas.createSprite(assetName);
            }
        });
        
        setAssetHandler(ShaderDescriptor.class, new AssetHandler() {

            private final ObjectMap shaderDescriptorMap = new ObjectMap<>();
            /**
            * assetName: filename of the Shader (*.shdr file)
            **/
            @Override
            public ShaderDescriptor findAsset(String assetName) {
                ShaderDescriptor asset = shaderDescriptorMap.get(assetName);
                if (asset == null) {
                    final FileHandle file = Gdx.files.internal(assetName);

                    asset = new ShaderDescriptor();
                    if (file.exists()) {
                        asset.setData(file.readString());
                        shaderDescriptorMap.put(assetName, asset);
                    }
                }
                return asset;
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy