use of org.andengine.util.texturepack.exception.TexturePackParseException in project AndEngine by nicolasgramlich.
the class TexturePackLoader method load.
public TexturePack load(final InputStream pInputStream, final String pAssetBasePath) throws TexturePackParseException {
try {
final SAXParserFactory spf = SAXParserFactory.newInstance();
final SAXParser sp = spf.newSAXParser();
final XMLReader xr = sp.getXMLReader();
final TexturePackParser texturePackParser = new TexturePackParser(this.mAssetManager, pAssetBasePath, this.mTextureManager);
xr.setContentHandler(texturePackParser);
xr.parse(new InputSource(new BufferedInputStream(pInputStream)));
return texturePackParser.getTexturePack();
} catch (final SAXException e) {
throw new TexturePackParseException(e);
} catch (final ParserConfigurationException pe) {
/* Doesn't happen. */
return null;
} catch (final IOException e) {
throw new TexturePackParseException(e);
} finally {
StreamUtils.close(pInputStream);
}
}
use of org.andengine.util.texturepack.exception.TexturePackParseException in project AndEngine by nicolasgramlich.
the class TexturePackParser method parseTexture.
private ITexture parseTexture(final Attributes pAttributes) throws TexturePackParseException {
final String file = SAXUtils.getAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_FILE);
if (this.mTextureManager.hasMappedTexture(file)) {
return this.mTextureManager.getMappedTexture(file);
}
final String type = SAXUtils.getAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE);
final PixelFormat pixelFormat = TexturePackParser.parsePixelFormat(pAttributes);
final TextureOptions textureOptions = this.parseTextureOptions(pAttributes);
final ITexture texture;
if (type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_BITMAP)) {
try {
texture = new BitmapTexture(this.mTextureManager, new IInputStreamOpener() {
@Override
public InputStream open() throws IOException {
return TexturePackParser.this.onGetInputStream(file);
}
}, BitmapTextureFormat.fromPixelFormat(pixelFormat), textureOptions);
} catch (final IOException e) {
throw new TexturePackParseException(e);
}
} else if (type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVR)) {
try {
texture = new PVRTexture(this.mTextureManager, PVRTextureFormat.fromPixelFormat(pixelFormat), new SmartPVRTexturePixelBufferStrategy(DataConstants.BYTES_PER_MEGABYTE / 8), textureOptions) {
@Override
protected InputStream onGetInputStream() throws IOException {
return TexturePackParser.this.onGetInputStream(file);
}
};
} catch (final IOException e) {
throw new TexturePackParseException(e);
}
} else if (type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVRGZ)) {
try {
texture = new PVRGZTexture(this.mTextureManager, PVRTextureFormat.fromPixelFormat(pixelFormat), new SmartPVRTexturePixelBufferStrategy(DataConstants.BYTES_PER_MEGABYTE / 8), textureOptions) {
@Override
protected InputStream onGetInputStream() throws IOException {
return TexturePackParser.this.onGetInputStream(file);
}
};
} catch (final IOException e) {
throw new TexturePackParseException(e);
}
} else if (type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVRCCZ)) {
try {
texture = new PVRCCZTexture(this.mTextureManager, PVRTextureFormat.fromPixelFormat(pixelFormat), new SmartPVRTexturePixelBufferStrategy(DataConstants.BYTES_PER_MEGABYTE / 8), textureOptions) {
@Override
protected InputStream onGetInputStream() throws IOException {
return TexturePackParser.this.onGetInputStream(file);
}
};
} catch (final IOException e) {
throw new TexturePackParseException(e);
}
} else {
throw new TexturePackParseException(new IllegalArgumentException("Unsupported pTextureFormat: '" + type + "'."));
}
this.mTextureManager.addMappedTexture(file, texture);
return texture;
}
use of org.andengine.util.texturepack.exception.TexturePackParseException in project AndEngine by nicolasgramlich.
the class TexturePackParser method startElement.
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public void startElement(final String pUri, final String pLocalName, final String pQualifiedName, final Attributes pAttributes) throws SAXException {
if (pLocalName.equals(TexturePackParser.TAG_TEXTURE)) {
this.mVersion = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_VERSION);
this.mTexture = this.parseTexture(pAttributes);
this.mTextureRegionLibrary = new TexturePackTextureRegionLibrary(10);
this.mTexturePack = new TexturePack(this.mTexture, this.mTextureRegionLibrary);
} else if (pLocalName.equals(TexturePackParser.TAG_TEXTUREREGION)) {
final int id = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_ID);
final int x = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_X);
final int y = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_Y);
final int width = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_WIDTH);
final int height = SAXUtils.getIntAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_HEIGHT);
final String source = SAXUtils.getAttributeOrThrow(pAttributes, TAG_TEXTUREREGION_ATTRIBUTE_SOURCE);
// TODO Not sure how trimming could be transparently supported...
final boolean trimmed = SAXUtils.getBooleanAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_TRIMMED);
final boolean rotated = SAXUtils.getBooleanAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTUREREGION_ATTRIBUTE_ROTATED);
final int sourceX = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_X);
final int sourceY = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_Y);
final int sourceWidth = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_WIDTH);
final int sourceHeight = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_TEXTUREREGION_ATTRIBUTE_SOURCE_HEIGHT);
this.mTextureRegionLibrary.put(new TexturePackTextureRegion(this.mTexture, x, y, width, height, id, source, rotated, trimmed, sourceX, sourceY, sourceWidth, sourceHeight));
} else {
throw new TexturePackParseException("Unexpected tag: '" + pLocalName + "'.");
}
}
Aggregations