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();
}
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;
}
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;
}
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();
}
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);
}
}
Aggregations