Search in sources :

Example 46 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class TexturePackerTest method render.

public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Settings settings = new Settings();
    settings.fast = false;
    settings.pot = false;
    settings.maxWidth = 1024;
    settings.maxHeight = 1024;
    settings.rotation = false;
    settings.paddingX = 0;
    if (pages == null) {
        Random random = new Random(1243);
        Array<Rect> inputRects = new Array();
        for (int i = 0; i < 240; i++) {
            Rect rect = new Rect();
            rect.name = "rect" + i;
            rect.height = 16 + random.nextInt(120);
            rect.width = 16 + random.nextInt(240);
            inputRects.add(rect);
        }
        for (int i = 0; i < 10; i++) {
            Rect rect = new Rect();
            rect.name = "rect" + (40 + i);
            rect.height = 400 + random.nextInt(340);
            rect.width = 1 + random.nextInt(10);
            inputRects.add(rect);
        }
        long s = System.nanoTime();
        pages = new MaxRectsPacker(settings).pack(inputRects);
        long e = System.nanoTime();
        System.out.println("fast: " + settings.fast);
        System.out.println((e - s) / 1e6f + " ms");
        System.out.println();
    }
    int x = 20, y = 20;
    for (Page page : pages) {
        renderer.setColor(Color.GRAY);
        renderer.begin(ShapeType.Filled);
        for (int i = 0; i < page.outputRects.size; i++) {
            Rect rect = page.outputRects.get(i);
            renderer.rect(x + rect.x + settings.paddingX, y + rect.y + settings.paddingY, rect.width - settings.paddingX, rect.height - settings.paddingY);
        }
        renderer.end();
        renderer.setColor(Color.RED);
        renderer.begin(ShapeType.Line);
        for (int i = 0; i < page.outputRects.size; i++) {
            Rect rect = page.outputRects.get(i);
            renderer.rect(x + rect.x + settings.paddingX, y + rect.y + settings.paddingY, rect.width - settings.paddingX, rect.height - settings.paddingY);
        }
        renderer.setColor(Color.GREEN);
        renderer.rect(x, y, page.width + settings.paddingX * 2, page.height + settings.paddingY * 2);
        renderer.end();
        x += page.width + 20;
    }
}
Also used : Array(com.badlogic.gdx.utils.Array) Rect(com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect) Random(java.util.Random) Page(com.badlogic.gdx.tools.texturepacker.TexturePacker.Page) Settings(com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings)

Example 47 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class AssetManager method injectDependency.

private synchronized void injectDependency(String parentAssetFilename, AssetDescriptor dependendAssetDesc) {
    // add the asset as a dependency of the parent asset
    Array<String> dependencies = assetDependencies.get(parentAssetFilename);
    if (dependencies == null) {
        dependencies = new Array();
        assetDependencies.put(parentAssetFilename, dependencies);
    }
    dependencies.add(dependendAssetDesc.fileName);
    // if the asset is already loaded, increase its reference count.
    if (isLoaded(dependendAssetDesc.fileName)) {
        log.debug("Dependency already loaded: " + dependendAssetDesc);
        Class type = assetTypes.get(dependendAssetDesc.fileName);
        RefCountedContainer assetRef = assets.get(type).get(dependendAssetDesc.fileName);
        assetRef.incRefCount();
        incrementRefCountedDependencies(dependendAssetDesc.fileName);
    } else // else add a new task for the asset.
    {
        log.info("Loading dependency: " + dependendAssetDesc);
        addTask(dependendAssetDesc);
    }
}
Also used : Array(com.badlogic.gdx.utils.Array)

Example 48 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class BitmapFontLoader method getDependencies.

@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, BitmapFontParameter parameter) {
    Array<AssetDescriptor> deps = new Array();
    if (parameter != null && parameter.bitmapFontData != null) {
        data = parameter.bitmapFontData;
        return deps;
    }
    data = new BitmapFontData(file, parameter != null ? parameter.flip : false);
    if (parameter != null && parameter.atlasName != null) {
        deps.add(new AssetDescriptor(parameter.atlasName, TextureAtlas.class));
    } else {
        for (int i = 0; i < data.getImagePaths().length; i++) {
            String path = data.getImagePath(i);
            FileHandle resolved = resolve(path);
            TextureLoader.TextureParameter textureParams = new TextureLoader.TextureParameter();
            if (parameter != null) {
                textureParams.genMipMaps = parameter.genMipMaps;
                textureParams.minFilter = parameter.minFilter;
                textureParams.magFilter = parameter.magFilter;
            }
            AssetDescriptor descriptor = new AssetDescriptor(resolved, Texture.class, textureParams);
            deps.add(descriptor);
        }
    }
    return deps;
}
Also used : Array(com.badlogic.gdx.utils.Array) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) FileHandle(com.badlogic.gdx.files.FileHandle) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) AssetDescriptor(com.badlogic.gdx.assets.AssetDescriptor)

Example 49 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class Cubemap method invalidateAllCubemaps.

/** Invalidate all managed cubemaps. This is an internal method. Do not use it! */
public static void invalidateAllCubemaps(Application app) {
    Array<Cubemap> managedCubemapArray = managedCubemaps.get(app);
    if (managedCubemapArray == null)
        return;
    if (assetManager == null) {
        for (int i = 0; i < managedCubemapArray.size; i++) {
            Cubemap cubemap = managedCubemapArray.get(i);
            cubemap.reload();
        }
    } else {
        // first we have to make sure the AssetManager isn't loading anything anymore,
        // otherwise the ref counting trick below wouldn't work (when a cubemap is
        // currently on the task stack of the manager.)
        assetManager.finishLoading();
        // next we go through each cubemap and reload either directly or via the
        // asset manager.
        Array<Cubemap> cubemaps = new Array<Cubemap>(managedCubemapArray);
        for (Cubemap cubemap : cubemaps) {
            String fileName = assetManager.getAssetFileName(cubemap);
            if (fileName == null) {
                cubemap.reload();
            } else {
                // get the ref count of the cubemap, then set it to 0 so we
                // can actually remove it from the assetmanager. Also set the
                // handle to zero, otherwise we might accidentially dispose
                // already reloaded cubemaps.
                final int refCount = assetManager.getReferenceCount(fileName);
                assetManager.setReferenceCount(fileName, 0);
                cubemap.glHandle = 0;
                // create the parameters, passing the reference to the cubemap as
                // well as a callback that sets the ref count.
                CubemapParameter params = new CubemapParameter();
                params.cubemapData = cubemap.getCubemapData();
                params.minFilter = cubemap.getMinFilter();
                params.magFilter = cubemap.getMagFilter();
                params.wrapU = cubemap.getUWrap();
                params.wrapV = cubemap.getVWrap();
                // special parameter which will ensure that the references stay the same.
                params.cubemap = cubemap;
                params.loadedCallback = new LoadedCallback() {

                    @Override
                    public void finishedLoading(AssetManager assetManager, String fileName, Class type) {
                        assetManager.setReferenceCount(fileName, refCount);
                    }
                };
                // unload the c, create a new gl handle then reload it.
                assetManager.unload(fileName);
                cubemap.glHandle = Gdx.gl.glGenTexture();
                assetManager.load(fileName, Cubemap.class, params);
            }
        }
        managedCubemapArray.clear();
        managedCubemapArray.addAll(cubemaps);
    }
}
Also used : Array(com.badlogic.gdx.utils.Array) LoadedCallback(com.badlogic.gdx.assets.AssetLoaderParameters.LoadedCallback) AssetManager(com.badlogic.gdx.assets.AssetManager) CubemapParameter(com.badlogic.gdx.assets.loaders.CubemapLoader.CubemapParameter)

Example 50 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class GridPacker method pack.

public Array<Page> pack(Array<Rect> inputRects) {
    if (!settings.silent)
        System.out.print("Packing");
    int cellWidth = 0, cellHeight = 0;
    for (int i = 0, nn = inputRects.size; i < nn; i++) {
        Rect rect = inputRects.get(i);
        cellWidth = Math.max(cellWidth, rect.width);
        cellHeight = Math.max(cellHeight, rect.height);
    }
    cellWidth += settings.paddingX;
    cellHeight += settings.paddingY;
    inputRects.reverse();
    Array<Page> pages = new Array();
    while (inputRects.size > 0) {
        Page result = packPage(inputRects, cellWidth, cellHeight);
        pages.add(result);
    }
    return pages;
}
Also used : Array(com.badlogic.gdx.utils.Array) Rect(com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect) Page(com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)

Aggregations

Array (com.badlogic.gdx.utils.Array)67 FileHandle (com.badlogic.gdx.files.FileHandle)18 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)16 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)9 Texture (com.badlogic.gdx.graphics.Texture)8 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 IntArray (com.badlogic.gdx.utils.IntArray)7 Element (com.badlogic.gdx.utils.XmlReader.Element)7 IOException (java.io.IOException)7 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)5 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)5 JsonValue (com.badlogic.gdx.utils.JsonValue)5 ObjectMap (com.badlogic.gdx.utils.ObjectMap)5 Color (com.badlogic.gdx.graphics.Color)4 ModelMaterial (com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial)4 Page (com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)4 Rect (com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect)4 Json (com.badlogic.gdx.utils.Json)4 TextureParameter (com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter)3 GwtFileHandle (com.badlogic.gdx.backends.gwt.GwtFileHandle)3