use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.
the class btIndexedMesh method obtain.
/** Create or reuse a btIndexedMesh instance based on the specified tag.
* Use {@link #release()} to release the mesh when it's no longer needed. */
public static btIndexedMesh obtain(final Object tag, final FloatBuffer vertices, int sizeInBytesOfEachVertex, int vertexCount, int positionOffsetInBytes, final ShortBuffer indices, int indexOffset, int indexCount) {
if (tag == null)
throw new GdxRuntimeException("tag cannot be null");
btIndexedMesh result = getInstance(tag);
if (result == null) {
result = new btIndexedMesh(vertices, sizeInBytesOfEachVertex, vertexCount, positionOffsetInBytes, indices, indexOffset, indexCount);
result.tag = tag;
instances.add(result);
}
result.obtain();
return result;
}
use of com.badlogic.gdx.utils.GdxRuntimeException 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();
}
use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.
the class PixmapPacker method pack.
/** Inserts the pixmap. If name was not null, you can later retrieve the image's position in the output image via
* {@link #getRect(String)}.
* @param name If null, the image cannot be looked up by name.
* @return Rectangle describing the area the pixmap was rendered to.
* @throws GdxRuntimeException in case the image did not fit due to the page size being too small or providing a duplicate
* name. */
public synchronized Rectangle pack(String name, Pixmap image) {
if (disposed)
return null;
if (name != null && getRect(name) != null)
throw new GdxRuntimeException("Pixmap has already been packed with name: " + name);
Rectangle rect = new Rectangle(0, 0, image.getWidth(), image.getHeight());
if (rect.getWidth() > pageWidth || rect.getHeight() > pageHeight) {
if (name == null)
throw new GdxRuntimeException("Page size too small for pixmap.");
throw new GdxRuntimeException("Page size too small for pixmap: " + name);
}
Page page = packStrategy.pack(this, name, rect);
if (name != null) {
page.rects.put(name, rect);
page.addedRects.add(name);
}
int rectX = (int) rect.x, rectY = (int) rect.y, rectWidth = (int) rect.width, rectHeight = (int) rect.height;
if (packToTexture && !duplicateBorder && page.texture != null && !page.dirty) {
page.texture.bind();
Gdx.gl.glTexSubImage2D(page.texture.glTarget, 0, rectX, rectY, rectWidth, rectHeight, image.getGLFormat(), image.getGLType(), image.getPixels());
} else
page.dirty = true;
page.image.setBlending(Blending.None);
page.image.drawPixmap(image, rectX, rectY);
if (duplicateBorder) {
int imageWidth = image.getWidth(), imageHeight = image.getHeight();
// Copy corner pixels to fill corners of the padding.
page.image.drawPixmap(image, 0, 0, 1, 1, rectX - 1, rectY - 1, 1, 1);
page.image.drawPixmap(image, imageWidth - 1, 0, 1, 1, rectX + rectWidth, rectY - 1, 1, 1);
page.image.drawPixmap(image, 0, imageHeight - 1, 1, 1, rectX - 1, rectY + rectHeight, 1, 1);
page.image.drawPixmap(image, imageWidth - 1, imageHeight - 1, 1, 1, rectX + rectWidth, rectY + rectHeight, 1, 1);
// Copy edge pixels into padding.
page.image.drawPixmap(image, 0, 0, imageWidth, 1, rectX, rectY - 1, rectWidth, 1);
page.image.drawPixmap(image, 0, imageHeight - 1, imageWidth, 1, rectX, rectY + rectHeight, rectWidth, 1);
page.image.drawPixmap(image, 0, 0, 1, imageHeight, rectX - 1, rectY, 1, rectHeight);
page.image.drawPixmap(image, imageWidth - 1, 0, 1, imageHeight, rectX + rectWidth, rectY, 1, rectHeight);
}
return rect;
}
use of com.badlogic.gdx.utils.GdxRuntimeException 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.utils.GdxRuntimeException in project libgdx by libgdx.
the class PolygonRegionLoader method load.
/** Loads a PolygonRegion from a PSH (Polygon SHape) file. The PSH file format defines the polygon vertices before
* triangulation:
* <p>
* s 200.0, 100.0, ...
* <p>
* Lines not prefixed with "s" are ignored. PSH files can be created with external tools, eg: <br>
* https://code.google.com/p/libgdx-polygoneditor/ <br>
* http://www.codeandweb.com/physicseditor/
* @param file file handle to the shape definition file */
public PolygonRegion load(TextureRegion textureRegion, FileHandle file) {
BufferedReader reader = file.reader(256);
try {
while (true) {
String line = reader.readLine();
if (line == null)
break;
if (line.startsWith("s")) {
// Read shape.
String[] polygonStrings = line.substring(1).trim().split(",");
float[] vertices = new float[polygonStrings.length];
for (int i = 0, n = vertices.length; i < n; i++) vertices[i] = Float.parseFloat(polygonStrings[i]);
// It would probably be better if PSH stored the vertices and triangles, then we don't have to triangulate here.
return new PolygonRegion(textureRegion, vertices, triangulator.computeTriangles(vertices).toArray());
}
}
} catch (IOException ex) {
throw new GdxRuntimeException("Error reading polygon shape file: " + file, ex);
} finally {
StreamUtils.closeQuietly(reader);
}
throw new GdxRuntimeException("Polygon shape not found: " + file);
}
Aggregations