Search in sources :

Example 1 with PixelFormat

use of org.andengine.opengl.texture.PixelFormat 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 2 with PixelFormat

use of org.andengine.opengl.texture.PixelFormat in project AndEngine by nicolasgramlich.

the class BitmapTextureAtlas method writeTextureToHardware.

// ===========================================================
// Methods
// ===========================================================
@Override
protected void writeTextureToHardware(final GLState pGLState) {
    final PixelFormat pixelFormat = this.mBitmapTextureFormat.getPixelFormat();
    final int glInternalFormat = pixelFormat.getGLInternalFormat();
    final int glFormat = pixelFormat.getGLFormat();
    final int glType = pixelFormat.getGLType();
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, glInternalFormat, this.mWidth, this.mHeight, 0, glFormat, glType, null);
    final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha;
    /* Non alpha premultiplied bitmaps are loaded with ARGB_8888 and converted down manually. */
    final Config bitmapConfig = (preMultipyAlpha) ? this.mBitmapTextureFormat.getBitmapConfig() : Config.ARGB_8888;
    final ArrayList<IBitmapTextureAtlasSource> textureSources = this.mTextureAtlasSources;
    final int textureSourceCount = textureSources.size();
    final ITextureAtlasStateListener<IBitmapTextureAtlasSource> textureStateListener = this.getTextureAtlasStateListener();
    for (int i = 0; i < textureSourceCount; i++) {
        final IBitmapTextureAtlasSource bitmapTextureAtlasSource = textureSources.get(i);
        try {
            final Bitmap bitmap = bitmapTextureAtlasSource.onLoadBitmap(bitmapConfig);
            if (bitmap == null) {
                throw new NullBitmapException("Caused by: " + bitmapTextureAtlasSource.getClass().toString() + " --> " + bitmapTextureAtlasSource.toString() + " returned a null Bitmap.");
            }
            final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && pixelFormat == PixelFormat.RGBA_8888;
            if (!useDefaultAlignment) {
                /* Adjust unpack alignment. */
                GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
            }
            if (preMultipyAlpha) {
                GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, glFormat, glType);
            } else {
                pGLState.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, this.mPixelFormat);
            }
            if (!useDefaultAlignment) {
                /* Restore default unpack alignment. */
                GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
            }
            bitmap.recycle();
            if (textureStateListener != null) {
                textureStateListener.onTextureAtlasSourceLoaded(this, bitmapTextureAtlasSource);
            }
        } catch (final NullBitmapException e) {
            if (textureStateListener != null) {
                textureStateListener.onTextureAtlasSourceLoadExeption(this, bitmapTextureAtlasSource, e);
            } else {
                throw e;
            }
        }
    }
}
Also used : IBitmapTextureAtlasSource(org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource) Bitmap(android.graphics.Bitmap) Config(android.graphics.Bitmap.Config) NullBitmapException(org.andengine.util.exception.NullBitmapException) PixelFormat(org.andengine.opengl.texture.PixelFormat)

Example 3 with PixelFormat

use of org.andengine.opengl.texture.PixelFormat in project AndEngine by nicolasgramlich.

the class Font method update.

public synchronized void update(final GLState pGLState) {
    if (this.mTexture.isLoadedToHardware()) {
        final ArrayList<Letter> lettersPendingToBeDrawnToTexture = this.mLettersPendingToBeDrawnToTexture;
        if (lettersPendingToBeDrawnToTexture.size() > 0) {
            this.mTexture.bind(pGLState);
            final PixelFormat pixelFormat = this.mTexture.getPixelFormat();
            final boolean preMultipyAlpha = this.mTexture.getTextureOptions().mPreMultiplyAlpha;
            for (int i = lettersPendingToBeDrawnToTexture.size() - 1; i >= 0; i--) {
                final Letter letter = lettersPendingToBeDrawnToTexture.get(i);
                if (!letter.isWhitespace()) {
                    final Bitmap bitmap = this.getLetterBitmap(letter);
                    final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && (pixelFormat == PixelFormat.RGBA_8888);
                    if (!useDefaultAlignment) {
                        /* Adjust unpack alignment. */
                        GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
                    }
                    if (preMultipyAlpha) {
                        GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, letter.mTextureX, letter.mTextureY, bitmap);
                    } else {
                        pGLState.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, letter.mTextureX, letter.mTextureY, bitmap, pixelFormat);
                    }
                    if (!useDefaultAlignment) {
                        /* Restore default unpack alignment. */
                        GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
                    }
                    bitmap.recycle();
                }
            }
            lettersPendingToBeDrawnToTexture.clear();
            System.gc();
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) PixelFormat(org.andengine.opengl.texture.PixelFormat) Paint(android.graphics.Paint)

Aggregations

PixelFormat (org.andengine.opengl.texture.PixelFormat)3 Bitmap (android.graphics.Bitmap)2 Config (android.graphics.Bitmap.Config)1 Paint (android.graphics.Paint)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ITexture (org.andengine.opengl.texture.ITexture)1 TextureOptions (org.andengine.opengl.texture.TextureOptions)1 IBitmapTextureAtlasSource (org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource)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 NullBitmapException (org.andengine.util.exception.NullBitmapException)1 TexturePackParseException (org.andengine.util.texturepack.exception.TexturePackParseException)1