use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project libgdx by libgdx.
the class TextureUnpacker method splitAtlas.
/** Splits an atlas into seperate image and ninepatch files. */
public void splitAtlas(TextureAtlasData atlas, String outputDir) {
// create the output directory if it did not exist yet
File outputDirFile = new File(outputDir);
if (!outputDirFile.exists()) {
outputDirFile.mkdirs();
System.out.println(String.format("Creating directory: %s", outputDirFile.getPath()));
}
for (Page page : atlas.getPages()) {
// load the image file belonging to this page as a Buffered Image
BufferedImage img = null;
try {
img = ImageIO.read(page.textureFile.file());
} catch (IOException e) {
printExceptionAndExit(e);
}
for (Region region : atlas.getRegions()) {
System.out.println(String.format("Processing image for %s: x[%s] y[%s] w[%s] h[%s], rotate[%s]", region.name, region.left, region.top, region.width, region.height, region.rotate));
// check if the page this region is in is currently loaded in a Buffered Image
if (region.page == page) {
BufferedImage splitImage = null;
String extension = null;
// check if the region is a ninepatch or a normal image and delegate accordingly
if (region.splits == null) {
splitImage = extractImage(img, region, outputDirFile, 0);
extension = OUTPUT_TYPE;
} else {
splitImage = extractNinePatch(img, region, outputDirFile);
extension = String.format("9.%s", OUTPUT_TYPE);
}
// check if the parent directories of this image file exist and create them if not
File imgOutput = new File(outputDirFile, String.format("%s.%s", region.index == -1 ? region.name : region.name + "_" + region.index, extension));
File imgDir = imgOutput.getParentFile();
if (!imgDir.exists()) {
System.out.println(String.format("Creating directory: %s", imgDir.getPath()));
imgDir.mkdirs();
}
// save the image
try {
ImageIO.write(splitImage, OUTPUT_TYPE, imgOutput);
} catch (IOException e) {
printExceptionAndExit(e);
}
}
}
}
}
use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project libgdx by libgdx.
the class TextureAtlas method load.
private void load(TextureAtlasData data) {
ObjectMap<Page, Texture> pageToTexture = new ObjectMap<Page, Texture>();
for (Page page : data.pages) {
Texture texture = null;
if (page.texture == null) {
texture = new Texture(page.textureFile, page.format, page.useMipMaps);
texture.setFilter(page.minFilter, page.magFilter);
texture.setWrap(page.uWrap, page.vWrap);
} else {
texture = page.texture;
texture.setFilter(page.minFilter, page.magFilter);
texture.setWrap(page.uWrap, page.vWrap);
}
textures.add(texture);
pageToTexture.put(page, texture);
}
for (Region region : data.regions) {
int width = region.width;
int height = region.height;
AtlasRegion atlasRegion = new AtlasRegion(pageToTexture.get(region.page), region.left, region.top, region.rotate ? height : width, region.rotate ? width : height);
atlasRegion.index = region.index;
atlasRegion.name = region.name;
atlasRegion.offsetX = region.offsetX;
atlasRegion.offsetY = region.offsetY;
atlasRegion.originalHeight = region.originalHeight;
atlasRegion.originalWidth = region.originalWidth;
atlasRegion.rotate = region.rotate;
atlasRegion.splits = region.splits;
atlasRegion.pads = region.pads;
if (region.flip)
atlasRegion.flip(false, true);
regions.add(atlasRegion);
}
}
use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project skin-composer by raeleus.
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();
}
use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project bladecoder-adventure-engine by bladecoder.
the class CustomTextureUnpacker method splitAtlas.
/**
* Splits an atlas into seperate image and ninepatch files.
*/
public void splitAtlas(TextureAtlasData atlas, String outputDir) {
// create the output directory if it did not exist yet
File outputDirFile = new File(outputDir);
if (!outputDirFile.exists()) {
outputDirFile.mkdirs();
EditorLogger.debug(String.format("Creating directory: %s", outputDirFile.getPath()));
}
for (Page page : atlas.getPages()) {
// load the image file belonging to this page as a Buffered Image
BufferedImage img = null;
try {
img = ImageIO.read(page.textureFile.file());
} catch (IOException e) {
printExceptionAndExit(e);
}
for (Region region : atlas.getRegions()) {
EditorLogger.debug(String.format("Processing image for %s(%s): x[%s] y[%s] w[%s] h[%s], rotate[%s]", region.name, region.index, region.left, region.top, region.width, region.height, region.rotate));
// Buffered Image
if (region.page == page) {
BufferedImage splitImage = null;
String extension = null;
// delegate accordingly
if (region.splits == null) {
splitImage = extractImage(img, region, outputDirFile, 0);
extension = OUTPUT_TYPE;
} else {
splitImage = extractNinePatch(img, region, outputDirFile);
extension = String.format("9.%s", OUTPUT_TYPE);
}
// check if the parent directories of this image file exist
// and create them if not
File imgOutput = new File(outputDirFile, String.format("%s.%s", region.index == -1 ? region.name : region.name + "_" + region.index, extension));
File imgDir = imgOutput.getParentFile();
if (!imgDir.exists()) {
System.out.println(String.format("Creating directory: %s", imgDir.getPath()));
imgDir.mkdirs();
}
// save the image
try {
ImageIO.write(splitImage, OUTPUT_TYPE, imgOutput);
} catch (IOException e) {
printExceptionAndExit(e);
}
}
}
}
}
use of com.badlogic.gdx.graphics.g2d.TextureAtlas.TextureAtlasData.Region in project ultimate-java by pantinor.
the class GeneratedMapTmxConvert method main.
public static void main(String[] args) throws Exception {
int TILE_SIZE = 16;
FileHandle f = new FileHandle("assets/tilemaps/tiles-vga-atlas.txt");
TextureAtlasData atlas = new TextureAtlasData(f, f.parent(), false);
String[] mapTileIds = new String[atlas.getRegions().size + 1];
for (Region r : atlas.getRegions()) {
int x = r.left / r.width;
int y = r.top / r.height;
int i = y * TILE_SIZE + x + 1;
mapTileIds[i] = r.name;
}
List<StaticGeneratedDungeon> dungeons = new ArrayList<StaticGeneratedDungeon>();
dungeons.add(new StaticGeneratedDungeon("The Dark Pit of Emes the Fallen 01 (tsv).txt"));
dungeons.add(new StaticGeneratedDungeon("The Dark Pit of Emes the Fallen 10 (tsv).txt"));
dungeons.add(new StaticGeneratedDungeon("The Dark Pit of Emes the Fallen 20 (tsv).txt"));
List<String> layers = new ArrayList<String>();
for (StaticGeneratedDungeon sd : dungeons) {
StringBuffer data = new StringBuffer();
for (int y = 0; y < StaticGeneratedDungeon.DIM; y++) {
for (int x = 0; x < StaticGeneratedDungeon.DIM; x++) {
Key val = sd.getCell(x, y);
if (val == null) {
val = StaticGeneratedDungeon.Key.NULL;
}
data.append(findTileId(mapTileIds, val.getName()) + ",");
}
data.append("\n");
}
String dl = data.toString();
dl = dl.substring(0, dl.length() - 2);
layers.add(dl);
}
GeneratedMapTmxConvert c = new GeneratedMapTmxConvert("delve", "tiles-vga.png", StaticGeneratedDungeon.DIM, StaticGeneratedDungeon.DIM, TILE_SIZE, TILE_SIZE, layers.get(0), layers.get(1), layers.get(2), "", "", "");
FileUtils.writeStringToFile(new File("assets/tilemaps/generatedDungeon.tmx"), c.toString());
}
Aggregations