From ebdb3106a7047ab60a96cbc052191e0cdb41d885 Mon Sep 17 00:00:00 2001 From: Denis Kniazhev Date: Mon, 16 Oct 2017 18:34:51 +0300 Subject: [PATCH] Checking for texture loading error early. (#10) --- .../android/common/TextureHelper.java | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/android/AndroidOpenGLESLessons/app/src/main/java/com/learnopengles/android/common/TextureHelper.java b/android/AndroidOpenGLESLessons/app/src/main/java/com/learnopengles/android/common/TextureHelper.java index d614fa6..e9b95d8 100644 --- a/android/AndroidOpenGLESLessons/app/src/main/java/com/learnopengles/android/common/TextureHelper.java +++ b/android/AndroidOpenGLESLessons/app/src/main/java/com/learnopengles/android/common/TextureHelper.java @@ -13,34 +13,31 @@ public static int loadTexture(final Context context, final int resourceId) final int[] textureHandle = new int[1]; GLES20.glGenTextures(1, textureHandle, 0); - - if (textureHandle[0] != 0) - { - final BitmapFactory.Options options = new BitmapFactory.Options(); - options.inScaled = false; // No pre-scaling - - // Read in the resource - final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options); - - // Bind to the texture in OpenGL - GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]); - - // Set filtering - GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); - GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); - - // Load the bitmap into the bound texture. - GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); - - // Recycle the bitmap, since its data has been loaded into OpenGL. - bitmap.recycle(); - } - + if (textureHandle[0] == 0) { - throw new RuntimeException("Error loading texture."); + throw new RuntimeException("Error generating texture name."); } - + + final BitmapFactory.Options options = new BitmapFactory.Options(); + options.inScaled = false; // No pre-scaling + + // Read in the resource + final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options); + + // Bind to the texture in OpenGL + GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]); + + // Set filtering + GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); + GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); + + // Load the bitmap into the bound texture. + GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); + + // Recycle the bitmap, since its data has been loaded into OpenGL. + bitmap.recycle(); + return textureHandle[0]; } }