Search in sources :

Example 11 with FileHandle

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

the class PixmapPackerIO method save.

/** Saves the provided PixmapPacker to the provided file. The resulting file will use the standard TextureAtlas file format and
	 * can be loaded by TextureAtlas as if it had been created using TexturePacker.
	 * 
	 * @param file the file to which the atlas descriptor will be written, images will be written as siblings
	 * @param packer the PixmapPacker to be written
	 * @param parameters the SaveParameters specifying how to save the PixmapPacker
	 * @throws IOException if the atlas file can not be written */
public void save(FileHandle file, PixmapPacker packer, SaveParameters parameters) throws IOException {
    Writer writer = file.writer(false);
    int index = 0;
    for (Page page : packer.pages) {
        if (page.rects.size > 0) {
            FileHandle pageFile = file.sibling(file.nameWithoutExtension() + "_" + (++index) + parameters.format.getExtension());
            switch(parameters.format) {
                case CIM:
                    {
                        PixmapIO.writeCIM(pageFile, page.image);
                        break;
                    }
                case PNG:
                    {
                        PixmapIO.writePNG(pageFile, page.image);
                        break;
                    }
            }
            writer.write("\n");
            writer.write(pageFile.name() + "\n");
            writer.write("size: " + page.image.getWidth() + "," + page.image.getHeight() + "\n");
            writer.write("format: " + packer.pageFormat.name() + "\n");
            writer.write("filter: " + parameters.minFilter.name() + "," + parameters.magFilter.name() + "\n");
            writer.write("repeat: none" + "\n");
            for (String name : page.rects.keys()) {
                writer.write(name + "\n");
                Rectangle rect = page.rects.get(name);
                writer.write("rotate: false" + "\n");
                writer.write("xy: " + (int) rect.x + "," + (int) rect.y + "\n");
                writer.write("size: " + (int) rect.width + "," + (int) rect.height + "\n");
                writer.write("orig: " + (int) rect.width + "," + (int) rect.height + "\n");
                writer.write("offset: 0, 0" + "\n");
                writer.write("index: -1" + "\n");
            }
        }
    }
    writer.close();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle) Rectangle(com.badlogic.gdx.math.Rectangle) Page(com.badlogic.gdx.graphics.g2d.PixmapPacker.Page) Writer(java.io.Writer)

Example 12 with FileHandle

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

the class PolygonRegionLoader method getDependencies.

/** If the PSH file contains a line starting with {@link PolygonRegionParameters#texturePrefix params.texturePrefix}, an
	 * {@link AssetDescriptor} for the file referenced on that line will be added to the returned Array. Otherwise a sibling of the
	 * given file with the same name and the first found extension in {@link PolygonRegionParameters#textureExtensions
	 * params.textureExtensions} will be used. If no suitable file is found, the returned Array will be empty. */
@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, PolygonRegionParameters params) {
    if (params == null)
        params = defaultParameters;
    String image = null;
    try {
        BufferedReader reader = file.reader(params.readerBuffer);
        for (String line = reader.readLine(); line != null; line = reader.readLine()) if (line.startsWith(params.texturePrefix)) {
            image = line.substring(params.texturePrefix.length());
            break;
        }
        reader.close();
    } catch (IOException e) {
        throw new GdxRuntimeException("Error reading " + fileName, e);
    }
    if (image == null && params.textureExtensions != null)
        for (String extension : params.textureExtensions) {
            FileHandle sibling = file.sibling(file.nameWithoutExtension().concat("." + extension));
            if (sibling.exists())
                image = sibling.name();
        }
    if (image != null) {
        Array<AssetDescriptor> deps = new Array<AssetDescriptor>(1);
        deps.add(new AssetDescriptor<Texture>(file.sibling(image), Texture.class));
        return deps;
    }
    return null;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) FileHandle(com.badlogic.gdx.files.FileHandle) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture) AssetDescriptor(com.badlogic.gdx.assets.AssetDescriptor)

Example 13 with FileHandle

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

the class ShaderProgramLoader method loadSync.

@Override
public ShaderProgram loadSync(AssetManager manager, String fileName, FileHandle file, ShaderProgramParameter parameter) {
    String vertFileName = null, fragFileName = null;
    if (parameter != null) {
        if (parameter.vertexFile != null)
            vertFileName = parameter.vertexFile;
        if (parameter.fragmentFile != null)
            fragFileName = parameter.fragmentFile;
    }
    if (vertFileName == null && fileName.endsWith(fragmentFileSuffix)) {
        vertFileName = fileName.substring(0, fileName.length() - fragmentFileSuffix.length()) + vertexFileSuffix;
    }
    if (fragFileName == null && fileName.endsWith(vertexFileSuffix)) {
        fragFileName = fileName.substring(0, fileName.length() - vertexFileSuffix.length()) + fragmentFileSuffix;
    }
    FileHandle vertexFile = vertFileName == null ? file : resolve(vertFileName);
    FileHandle fragmentFile = fragFileName == null ? file : resolve(fragFileName);
    String vertexCode = vertexFile.readString();
    String fragmentCode = vertexFile.equals(fragmentFile) ? vertexCode : fragmentFile.readString();
    if (parameter != null) {
        if (parameter.prependVertexCode != null)
            vertexCode = parameter.prependVertexCode + vertexCode;
        if (parameter.prependFragmentCode != null)
            fragmentCode = parameter.prependFragmentCode + fragmentCode;
    }
    ShaderProgram shaderProgram = new ShaderProgram(vertexCode, fragmentCode);
    if ((parameter == null || parameter.logOnCompileFailure) && !shaderProgram.isCompiled()) {
        manager.getLogger().error("ShaderProgram " + fileName + " failed to compile:\n" + shaderProgram.getLog());
    }
    return shaderProgram;
}
Also used : ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) FileHandle(com.badlogic.gdx.files.FileHandle)

Example 14 with FileHandle

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

the class ResolutionFileResolver method resolve.

protected String resolve(FileHandle originalHandle, String suffix) {
    String parentString = "";
    FileHandle parent = originalHandle.parent();
    if (parent != null && !parent.name().equals("")) {
        parentString = parent + "/";
    }
    return parentString + suffix + "/" + originalHandle.name();
}
Also used : FileHandle(com.badlogic.gdx.files.FileHandle)

Example 15 with FileHandle

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

the class BaseTmxMapLoader method loadImageLayer.

protected void loadImageLayer(TiledMap map, Element element, FileHandle tmxFile, ImageResolver imageResolver) {
    if (element.getName().equals("imagelayer")) {
        int x = Integer.parseInt(element.getAttribute("x", "0"));
        int y = Integer.parseInt(element.getAttribute("y", "0"));
        if (flipY)
            y = mapHeightInPixels - y;
        TextureRegion texture = null;
        Element image = element.getChildByName("image");
        if (image != null) {
            String source = image.getAttribute("source");
            FileHandle handle = getRelativeFileHandle(tmxFile, source);
            texture = imageResolver.getImage(handle.path());
            y -= texture.getRegionHeight();
        }
        TiledMapImageLayer layer = new TiledMapImageLayer(texture, x, y);
        loadBasicLayerInfo(layer, element);
        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(layer.getProperties(), properties);
        }
        map.getLayers().add(layer);
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element)

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