Search in sources :

Example 6 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class TiledMapPacker method processInputDir.

/** You can either run the {@link TiledMapPacker#main(String[])} method or reference this class in your own project and call
	 * this method. If working with libGDX sources, you can also run this file to create a run configuration then export it as a
	 * Runnable Jar. To run from a nightly build:
	 * 
	 * <code> <br><br>
	 * Linux / OS X <br>
	   java -cp gdx.jar:gdx-natives.jar:gdx-backend-lwjgl.jar:gdx-backend-lwjgl-natives.jar:gdx-tiled-preprocessor.jar:extensions/gdx-tools/gdx-tools.jar
	    com.badlogic.gdx.tiledmappacker.TiledMapPacker inputDir [outputDir]  [--strip-unused] [--combine-tilesets] [-v]
	 * <br><br>
	 * 
	 * Windows <br>
	   java -cp gdx.jar;gdx-natives.jar;gdx-backend-lwjgl.jar;gdx-backend-lwjgl-natives.jar;gdx-tiled-preprocessor.jar;extensions/gdx-tools/gdx-tools.jar
	    com.badlogic.gdx.tiledmappacker.TiledMapPacker inputDir [outputDir]  [--strip-unused] [--combine-tilesets] [-v]
	 * <br><br> </code>
	 * 
	 * Keep in mind that this preprocessor will need to load the maps by using the {@link TmxMapLoader} loader and this in turn
	 * will need a valid OpenGL context to work.
	 * 
	 * Process a directory containing TMX map files representing Tiled maps and produce multiple, or a single, TextureAtlas as well
	 * as new processed TMX map files, correctly referencing the generated {@link TextureAtlas} by using the "atlas" custom map
	 * property. */
public void processInputDir(Settings texturePackerSettings) throws IOException {
    FileHandle inputDirHandle = new FileHandle(inputDir.getCanonicalPath());
    File[] mapFilesInCurrentDir = inputDir.listFiles(new TmxFilter());
    tilesetsToPack = new ObjectMap<String, TiledMapTileSet>();
    // Processes the maps inside inputDir
    for (File mapFile : mapFilesInCurrentDir) {
        processSingleMap(mapFile, inputDirHandle, texturePackerSettings);
    }
    processSubdirectories(inputDirHandle, texturePackerSettings);
    boolean combineTilesets = this.settings.combineTilesets;
    if (combineTilesets == true) {
        packTilesets(inputDirHandle, texturePackerSettings);
    }
}
Also used : TiledMapTileSet(com.badlogic.gdx.maps.tiled.TiledMapTileSet) FileHandle(com.badlogic.gdx.files.FileHandle) File(java.io.File)

Example 7 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class TiledMapPacker method processSubdirectories.

/** Looks for subdirectories inside parentHandle, processes maps in subdirectory, repeat.
	 * @param currentDir The directory to look for maps and other directories
	 * @throws IOException */
private void processSubdirectories(FileHandle currentDir, Settings texturePackerSettings) throws IOException {
    File parentPath = new File(currentDir.path());
    File[] directories = parentPath.listFiles(new DirFilter());
    for (File directory : directories) {
        currentDir = new FileHandle(directory.getCanonicalPath());
        File[] mapFilesInCurrentDir = directory.listFiles(new TmxFilter());
        for (File mapFile : mapFilesInCurrentDir) {
            processSingleMap(mapFile, currentDir, texturePackerSettings);
        }
        processSubdirectories(currentDir, texturePackerSettings);
    }
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) File(java.io.File)

Example 8 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class FlameMain method saveEffect.

public void saveEffect(File file) {
    Writer fileWriter = null;
    try {
        ParticleEffectLoader loader = (ParticleEffectLoader) assetManager.getLoader(ParticleEffect.class);
        loader.save(effect, new ParticleEffectSaveParameter(new FileHandle(file.getAbsolutePath()), assetManager, particleSystem.getBatches()));
    } catch (Exception ex) {
        System.out.println("Error saving effect: " + file.getAbsolutePath());
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this, "Error saving effect.");
    } finally {
        StreamUtils.closeQuietly(fileWriter);
    }
}
Also used : ParticleEffect(com.badlogic.gdx.graphics.g3d.particles.ParticleEffect) ParticleEffectSaveParameter(com.badlogic.gdx.graphics.g3d.particles.ParticleEffectLoader.ParticleEffectSaveParameter) FileHandle(com.badlogic.gdx.files.FileHandle) ParticleEffectLoader(com.badlogic.gdx.graphics.g3d.particles.ParticleEffectLoader) Writer(java.io.Writer)

Example 9 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class TexturePacker method writePackFile.

private void writePackFile(File outputDir, String scaledPackFileName, Array<Page> pages) throws IOException {
    File packFile = new File(outputDir, scaledPackFileName + settings.atlasExtension);
    File packDir = packFile.getParentFile();
    packDir.mkdirs();
    if (packFile.exists()) {
        // Make sure there aren't duplicate names.
        TextureAtlasData textureAtlasData = new TextureAtlasData(new FileHandle(packFile), new FileHandle(packFile), false);
        for (Page page : pages) {
            for (Rect rect : page.outputRects) {
                String rectName = Rect.getAtlasName(rect.name, settings.flattenPaths);
                for (Region region : textureAtlasData.getRegions()) {
                    if (region.name.equals(rectName)) {
                        throw new GdxRuntimeException("A region with the name \"" + rectName + "\" has already been packed: " + rect.name);
                    }
                }
            }
        }
    }
    Writer writer = new OutputStreamWriter(new FileOutputStream(packFile, true), "UTF-8");
    for (Page page : pages) {
        writer.write("\n" + page.imageName + "\n");
        writer.write("size: " + page.imageWidth + "," + page.imageHeight + "\n");
        writer.write("format: " + settings.format + "\n");
        writer.write("filter: " + settings.filterMin + "," + settings.filterMag + "\n");
        writer.write("repeat: " + getRepeatValue() + "\n");
        page.outputRects.sort();
        for (Rect rect : page.outputRects) {
            writeRect(writer, page, rect, rect.name);
            Array<Alias> aliases = new Array(rect.aliases.toArray());
            aliases.sort();
            for (Alias alias : aliases) {
                Rect aliasRect = new Rect();
                aliasRect.set(rect);
                alias.apply(aliasRect);
                writeRect(writer, page, aliasRect, alias.name);
            }
        }
    }
    writer.close();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) TextureAtlasData(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData) FileOutputStream(java.io.FileOutputStream) Region(com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) ImageWriter(javax.imageio.ImageWriter) Writer(java.io.Writer)

Example 10 with FileHandle

use of com.badlogic.gdx.files.FileHandle in project libgdx by libgdx.

the class TexturePacker method writeImages.

private void writeImages(File outputDir, String scaledPackFileName, Array<Page> pages) {
    File packFileNoExt = new File(outputDir, scaledPackFileName);
    File packDir = packFileNoExt.getParentFile();
    String imageName = packFileNoExt.getName();
    int fileIndex = 0;
    for (Page page : pages) {
        int width = page.width, height = page.height;
        int paddingX = settings.paddingX;
        int paddingY = settings.paddingY;
        if (settings.duplicatePadding) {
            paddingX /= 2;
            paddingY /= 2;
        }
        width -= settings.paddingX;
        height -= settings.paddingY;
        if (settings.edgePadding) {
            page.x = paddingX;
            page.y = paddingY;
            width += paddingX * 2;
            height += paddingY * 2;
        }
        if (settings.pot) {
            width = MathUtils.nextPowerOfTwo(width);
            height = MathUtils.nextPowerOfTwo(height);
        }
        width = Math.max(settings.minWidth, width);
        height = Math.max(settings.minHeight, height);
        page.imageWidth = width;
        page.imageHeight = height;
        File outputFile;
        while (true) {
            outputFile = new File(packDir, imageName + (fileIndex++ == 0 ? "" : fileIndex) + "." + settings.outputFormat);
            if (!outputFile.exists())
                break;
        }
        new FileHandle(outputFile).parent().mkdirs();
        page.imageName = outputFile.getName();
        BufferedImage canvas = new BufferedImage(width, height, getBufferedImageType(settings.format));
        Graphics2D g = (Graphics2D) canvas.getGraphics();
        if (!settings.silent)
            System.out.println("Writing " + canvas.getWidth() + "x" + canvas.getHeight() + ": " + outputFile);
        for (Rect rect : page.outputRects) {
            BufferedImage image = rect.getImage(imageProcessor);
            int iw = image.getWidth();
            int ih = image.getHeight();
            int rectX = page.x + rect.x, rectY = page.y + page.height - rect.y - rect.height;
            if (settings.duplicatePadding) {
                int amountX = settings.paddingX / 2;
                int amountY = settings.paddingY / 2;
                if (rect.rotated) {
                    // Copy corner pixels to fill corners of the padding.
                    for (int i = 1; i <= amountX; i++) {
                        for (int j = 1; j <= amountY; j++) {
                            plot(canvas, rectX - j, rectY + iw - 1 + i, image.getRGB(0, 0));
                            plot(canvas, rectX + ih - 1 + j, rectY + iw - 1 + i, image.getRGB(0, ih - 1));
                            plot(canvas, rectX - j, rectY - i, image.getRGB(iw - 1, 0));
                            plot(canvas, rectX + ih - 1 + j, rectY - i, image.getRGB(iw - 1, ih - 1));
                        }
                    }
                    // Copy edge pixels into padding.
                    for (int i = 1; i <= amountY; i++) {
                        for (int j = 0; j < iw; j++) {
                            plot(canvas, rectX - i, rectY + iw - 1 - j, image.getRGB(j, 0));
                            plot(canvas, rectX + ih - 1 + i, rectY + iw - 1 - j, image.getRGB(j, ih - 1));
                        }
                    }
                    for (int i = 1; i <= amountX; i++) {
                        for (int j = 0; j < ih; j++) {
                            plot(canvas, rectX + j, rectY - i, image.getRGB(iw - 1, j));
                            plot(canvas, rectX + j, rectY + iw - 1 + i, image.getRGB(0, j));
                        }
                    }
                } else {
                    // Copy corner pixels to fill corners of the padding.
                    for (int i = 1; i <= amountX; i++) {
                        for (int j = 1; j <= amountY; j++) {
                            plot(canvas, rectX - i, rectY - j, image.getRGB(0, 0));
                            plot(canvas, rectX - i, rectY + ih - 1 + j, image.getRGB(0, ih - 1));
                            plot(canvas, rectX + iw - 1 + i, rectY - j, image.getRGB(iw - 1, 0));
                            plot(canvas, rectX + iw - 1 + i, rectY + ih - 1 + j, image.getRGB(iw - 1, ih - 1));
                        }
                    }
                    // Copy edge pixels into padding.
                    for (int i = 1; i <= amountY; i++) {
                        copy(image, 0, 0, iw, 1, canvas, rectX, rectY - i, rect.rotated);
                        copy(image, 0, ih - 1, iw, 1, canvas, rectX, rectY + ih - 1 + i, rect.rotated);
                    }
                    for (int i = 1; i <= amountX; i++) {
                        copy(image, 0, 0, 1, ih, canvas, rectX - i, rectY, rect.rotated);
                        copy(image, iw - 1, 0, 1, ih, canvas, rectX + iw - 1 + i, rectY, rect.rotated);
                    }
                }
            }
            copy(image, 0, 0, iw, ih, canvas, rectX, rectY, rect.rotated);
            if (settings.debug) {
                g.setColor(Color.magenta);
                g.drawRect(rectX, rectY, rect.width - settings.paddingX - 1, rect.height - settings.paddingY - 1);
            }
        }
        if (settings.bleed && !settings.premultiplyAlpha && !(settings.outputFormat.equalsIgnoreCase("jpg") || settings.outputFormat.equalsIgnoreCase("jpeg"))) {
            canvas = new ColorBleedEffect().processImage(canvas, settings.bleedIterations);
            g = (Graphics2D) canvas.getGraphics();
        }
        if (settings.debug) {
            g.setColor(Color.magenta);
            g.drawRect(0, 0, width - 1, height - 1);
        }
        ImageOutputStream ios = null;
        try {
            if (settings.outputFormat.equalsIgnoreCase("jpg") || settings.outputFormat.equalsIgnoreCase("jpeg")) {
                BufferedImage newImage = new BufferedImage(canvas.getWidth(), canvas.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
                newImage.getGraphics().drawImage(canvas, 0, 0, null);
                canvas = newImage;
                Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
                ImageWriter writer = writers.next();
                ImageWriteParam param = writer.getDefaultWriteParam();
                param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                param.setCompressionQuality(settings.jpegQuality);
                ios = ImageIO.createImageOutputStream(outputFile);
                writer.setOutput(ios);
                writer.write(null, new IIOImage(canvas, null, null), param);
            } else {
                if (settings.premultiplyAlpha)
                    canvas.getColorModel().coerceData(canvas.getRaster(), true);
                ImageIO.write(canvas, "png", outputFile);
            }
        } catch (IOException ex) {
            throw new RuntimeException("Error writing file: " + outputFile, ex);
        } finally {
            if (ios != null) {
                try {
                    ios.close();
                } catch (Exception ignored) {
                }
            }
        }
    }
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D) IIOImage(javax.imageio.IIOImage) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Aggregations

FileHandle (com.badlogic.gdx.files.FileHandle)79 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)23 IOException (java.io.IOException)22 Array (com.badlogic.gdx.utils.Array)18 File (java.io.File)18 Element (com.badlogic.gdx.utils.XmlReader.Element)9 Texture (com.badlogic.gdx.graphics.Texture)8 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 IntArray (com.badlogic.gdx.utils.IntArray)7 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)6 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)5 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)4 TextureAtlasData (com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData)4 Json (com.badlogic.gdx.utils.Json)4 GwtFileHandle (com.badlogic.gdx.backends.gwt.GwtFileHandle)3 Color (com.badlogic.gdx.graphics.Color)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 Writer (java.io.Writer)3