Advertisement
Guest User

Android GL Context Loss

a guest
Jun 17th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. package com.diamondtools.opengl;
  2.  
  3. import javax.microedition.khronos.egl.EGLConfig;
  4. import javax.microedition.khronos.opengles.GL10;
  5.  
  6. import android.app.Activity;
  7. import android.content.pm.ApplicationInfo;
  8. import android.content.pm.PackageManager.NameNotFoundException;
  9. import android.opengl.GLSurfaceView;
  10.  
  11. import android.os.Bundle;
  12. import android.view.Window;
  13. import android.view.WindowManager;
  14.  
  15. public class MainActivity extends Activity {
  16.  
  17.     static {
  18.         System.loadLibrary("DiamondTools");
  19.     }
  20.  
  21.     private native int Activity_Pause();
  22.  
  23.     private native int Activity_DrawFrame();
  24.  
  25.     private native int Activity_SurfaceChange(int width, int height);
  26.  
  27.     private native int Activity_SurfaceCreate(String filePath);
  28.  
  29.     class EngineRenderer implements GLSurfaceView.Renderer {
  30.  
  31.         public void Destroy() {
  32.             Activity_Pause(); // Destroys and cleans up textures + native objects
  33.         }
  34.  
  35.         @Override
  36.         public void onDrawFrame(GL10 gl) {
  37.             Activity_DrawFrame(); // Calls GLES draw routines
  38.         }
  39.  
  40.         @Override
  41.         public void onSurfaceChanged(GL10 gl, int width, int height) {
  42.             Activity_SurfaceChange(width, height); // Set viewport, rebuilds camera matrix
  43.         }
  44.        
  45.         @Override
  46.         public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  47.             ApplicationInfo ai = null;
  48.             try {
  49.                 ai = getPackageManager()
  50.                         .getApplicationInfo(getPackageName(), 0);
  51.             } catch (NameNotFoundException e) {
  52.                 e.printStackTrace();
  53.             }
  54.  
  55.             if (ai != null) {
  56.                 Activity_SurfaceCreate(ai.sourceDir); // Create native objects with path to APK for resources
  57.             }
  58.         }
  59.     }
  60.  
  61.     private GLSurfaceView glSurfaceView = null;
  62.     private EngineRenderer renderer = null;
  63.  
  64.     @Override
  65.     protected void onCreate(Bundle savedInstanceState) {
  66.         super.onCreate(savedInstanceState);
  67.  
  68.         glSurfaceView = new GLSurfaceView(this);
  69.         renderer = new EngineRenderer();
  70.  
  71.         glSurfaceView.setEGLContextClientVersion(2);
  72.         glSurfaceView.setRenderer(renderer);
  73.  
  74.         glSurfaceView.setPreserveEGLContextOnPause(true); // Does nothing at all...
  75.  
  76.         setContentView(glSurfaceView);
  77.     }
  78.  
  79.     @Override
  80.     public void onPause() {
  81.         super.onPause();
  82.  
  83.         if (renderer != null) {
  84.             glSurfaceView.onPause();
  85.             renderer.Destroy(); // Deconstruct the native objects + gl textures
  86.         }
  87.     }
  88.  
  89.     @Override
  90.     public void onResume() {
  91.         super.onResume();
  92.  
  93.         if (renderer != null) {
  94.             glSurfaceView.onResume();
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement