Search in sources :

Example 1 with NullBitmapException

use of org.andengine.util.exception.NullBitmapException 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 2 with NullBitmapException

use of org.andengine.util.exception.NullBitmapException in project AndEngine by nicolasgramlich.

the class BitmapTexture method writeTextureToHardware.

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
protected void writeTextureToHardware(final GLState pGLState) throws IOException {
    final Config bitmapConfig = this.mBitmapTextureFormat.getBitmapConfig();
    final Bitmap bitmap = this.onGetBitmap(bitmapConfig);
    if (bitmap == null) {
        throw new NullBitmapException("Caused by: '" + this.toString() + "'.");
    }
    final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && (this.mPixelFormat == PixelFormat.RGBA_8888);
    if (!useDefaultAlignment) {
        /* Adjust unpack alignment. */
        GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
    }
    final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha;
    if (preMultipyAlpha) {
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    } else {
        pGLState.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0, this.mPixelFormat);
    }
    if (!useDefaultAlignment) {
        /* Restore default unpack alignment. */
        GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
    }
    bitmap.recycle();
}
Also used : Bitmap(android.graphics.Bitmap) Config(android.graphics.Bitmap.Config) NullBitmapException(org.andengine.util.exception.NullBitmapException)

Aggregations

Bitmap (android.graphics.Bitmap)2 Config (android.graphics.Bitmap.Config)2 NullBitmapException (org.andengine.util.exception.NullBitmapException)2 PixelFormat (org.andengine.opengl.texture.PixelFormat)1 IBitmapTextureAtlasSource (org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource)1