Search in sources :

Example 1 with TexturePackParseException

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) BufferedInputStream(java.io.BufferedInputStream) SAXParser(javax.xml.parsers.SAXParser) TexturePackParseException(org.andengine.util.texturepack.exception.TexturePackParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 2 with TexturePackParseException

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;
}
Also used : PVRGZTexture(org.andengine.opengl.texture.compressed.pvr.PVRGZTexture) InputStream(java.io.InputStream) PVRTexture(org.andengine.opengl.texture.compressed.pvr.PVRTexture) SmartPVRTexturePixelBufferStrategy(org.andengine.opengl.texture.compressed.pvr.pixelbufferstrategy.SmartPVRTexturePixelBufferStrategy) IOException(java.io.IOException) TexturePackParseException(org.andengine.util.texturepack.exception.TexturePackParseException) PVRCCZTexture(org.andengine.opengl.texture.compressed.pvr.PVRCCZTexture) IInputStreamOpener(org.andengine.util.adt.io.in.IInputStreamOpener) PixelFormat(org.andengine.opengl.texture.PixelFormat) TextureOptions(org.andengine.opengl.texture.TextureOptions) ITexture(org.andengine.opengl.texture.ITexture) BitmapTexture(org.andengine.opengl.texture.bitmap.BitmapTexture)

Example 3 with TexturePackParseException

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 + "'.");
    }
}
Also used : TexturePackParseException(org.andengine.util.texturepack.exception.TexturePackParseException)

Aggregations

TexturePackParseException (org.andengine.util.texturepack.exception.TexturePackParseException)3 IOException (java.io.IOException)2 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 ITexture (org.andengine.opengl.texture.ITexture)1 PixelFormat (org.andengine.opengl.texture.PixelFormat)1 TextureOptions (org.andengine.opengl.texture.TextureOptions)1 BitmapTexture (org.andengine.opengl.texture.bitmap.BitmapTexture)1 PVRCCZTexture (org.andengine.opengl.texture.compressed.pvr.PVRCCZTexture)1 PVRGZTexture (org.andengine.opengl.texture.compressed.pvr.PVRGZTexture)1 PVRTexture (org.andengine.opengl.texture.compressed.pvr.PVRTexture)1 SmartPVRTexturePixelBufferStrategy (org.andengine.opengl.texture.compressed.pvr.pixelbufferstrategy.SmartPVRTexturePixelBufferStrategy)1 IInputStreamOpener (org.andengine.util.adt.io.in.IInputStreamOpener)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 XMLReader (org.xml.sax.XMLReader)1