commit 86e9990797f627bde24cb070d3b88c3fd8fde54f
Author: leszek <leszek@koltunski.pl>
Date:   Sun May 28 00:41:49 2017 +0100

    Correct the FPS work (now works even after a Pause() ! )

diff --git a/src/main/java/org/distorted/library/Distorted.java b/src/main/java/org/distorted/library/Distorted.java
index 76f430b..1d4f392 100644
--- a/src/main/java/org/distorted/library/Distorted.java
+++ b/src/main/java/org/distorted/library/Distorted.java
@@ -33,15 +33,6 @@ public class Distorted
   {
   static int GLSL;
   static String GLSL_VERSION;
-
-  //////////// DEBUG FLAGS /////////////////////////////////////////////
-  /**
-   * When rendering a Screen, show FPS in the upper-left corner?
-   */
-  public static final int DEBUG_FPS = 1;
-
-  //////////// END DEBUG FLAGS /////////////////////////////////////////
-
   /**
    * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
    * backing up our DistortedTexture.
@@ -84,9 +75,6 @@ public class Distorted
 
   private static boolean mInitialized=false;
 
-  static int mDebugLevel = 0;
-  static DistortedDebug mDebug;
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // private: hide this from Javadoc
 
@@ -102,21 +90,6 @@ public class Distorted
     return mInitialized;
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Make the library show various debugging information.
- * <p>
- * Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined.
- *
- * @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined)
- */
-  public static void setDebug(int bitmask)
-    {
-    mDebugLevel = bitmask;
-
-    if( mDebugLevel!=0 && mDebug==null ) mDebug = new DistortedDebug();
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
@@ -125,11 +98,11 @@ public class Distorted
  * Needs to be called from a thread holding the OpenGL context.
  *   
  * @param context Context of the App using the library - used to open up Resources and read Shader code.
- * @throws FragmentCompilationException
- * @throws VertexCompilationException
- * @throws VertexUniformsException
- * @throws FragmentUniformsException
- * @throws LinkingException
+ * @throws FragmentCompilationException Fragment Shader failed to compile
+ * @throws VertexCompilationException   Vertex Shader failed to compile
+ * @throws VertexUniformsException      Too many uniforms in the Vertex Shader
+ * @throws FragmentUniformsException    Too many uniforms in the Fragment Shader
+ * @throws LinkingException             Shader failed to link
  */
   public static void onCreate(final Context context)
   throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
diff --git a/src/main/java/org/distorted/library/DistortedDebug.java b/src/main/java/org/distorted/library/DistortedDebug.java
deleted file mode 100644
index 2aaa5d9..0000000
--- a/src/main/java/org/distorted/library/DistortedDebug.java
+++ /dev/null
@@ -1,108 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// Copyright 2016 Leszek Koltunski                                                               //
-//                                                                                               //
-// This file is part of Distorted.                                                               //
-//                                                                                               //
-// Distorted is free software: you can redistribute it and/or modify                             //
-// it under the terms of the GNU General Public License as published by                          //
-// the Free Software Foundation, either version 2 of the License, or                             //
-// (at your option) any later version.                                                           //
-//                                                                                               //
-// Distorted is distributed in the hope that it will be useful,                                  //
-// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
-// GNU General Public License for more details.                                                  //
-//                                                                                               //
-// You should have received a copy of the GNU General Public License                             //
-// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-package org.distorted.library;
-
-import android.graphics.Bitmap;
-import android.graphics.Canvas;
-import android.graphics.Paint;
-
-import org.distorted.library.type.Static3D;
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Show various debugs.
- * <p>
- * Currently only showing the FPS in the upper-left corner of every DistortedScreen is supported.
- */
-class DistortedDebug
-{
-   private static final int NUM_FRAMES = 100;
-
-   private MeshObject fpsMesh;
-   private DistortedTexture fpsTexture;
-   private DistortedEffects fpsEffects;
-   private Canvas fpsCanvas;
-   private Bitmap fpsBitmap;
-   private Paint mPaint;
-   private int fpsH, fpsW;
-   private String fpsString = "";
-   private long lastTime=0;
-   private long[] durations;
-   private int currDuration;
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  DistortedDebug()
-    {
-    fpsW = 120;
-    fpsH =  70;
-
-    fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
-    fpsMesh = new MeshFlat(1,1);
-    fpsTexture = new DistortedTexture(fpsW,fpsH);
-    fpsTexture.setTexture(fpsBitmap);
-    fpsCanvas = new Canvas(fpsBitmap);
-    fpsEffects = new DistortedEffects();
-    fpsEffects.move( new Static3D(5,5,0) );
-
-    mPaint = new Paint();
-    mPaint.setAntiAlias(true);
-    mPaint.setTextAlign(Paint.Align.CENTER);
-    mPaint.setTextSize(0.7f*fpsH);
-
-    durations = new long[NUM_FRAMES+1];
-    currDuration = 0;
-    for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private void computeFPS(long currTime)
-    {
-    if( lastTime!=0 )
-      {
-      currDuration++;
-      if( currDuration>=NUM_FRAMES ) currDuration = 0;
-      durations[NUM_FRAMES] += ((currTime-lastTime)-durations[currDuration]);
-      durations[currDuration] = currTime-lastTime;
-
-      fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
-
-      mPaint.setColor(0xffffffff);
-      fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
-      mPaint.setColor(0xff000000);
-      fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
-      fpsTexture.setTexture(fpsBitmap);
-      }
-
-    lastTime = currTime;
-    }
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  void renderFPS(long currTime, DistortedOutputSurface surface)
-    {
-    computeFPS(currTime);
-
-    fpsTexture.setAsInput();
-    surface.setAsOutput();
-    fpsEffects.drawPriv( fpsW/2, fpsH/2, fpsMesh, surface, currTime, 0);
-    }
-}
diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index 9445b7f..6d6dcd1 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -31,6 +31,11 @@ import android.opengl.GLES30;
 public class DistortedFramebuffer extends DistortedOutputSurface implements DistortedInputSurface
   {
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void prepareDebug(long time) {}
+  void renderDebug(long time)  {}
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // Must be called from a thread holding OpenGL Context
 
@@ -170,21 +175,6 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
     super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET, type);
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-// For setting the Depth texture as input to fragment shaders that merge many planes. (currently: blur2)
-
-  boolean setAsDepth()
-    {
-    if( mDepthStencilH[0]>0 )
-      {
-      GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
-      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[0]);
-      return true;
-      }
-
-    return false;
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/DistortedOutputSurface.java b/src/main/java/org/distorted/library/DistortedOutputSurface.java
index a5641ec..8f26514 100644
--- a/src/main/java/org/distorted/library/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/DistortedOutputSurface.java
@@ -21,12 +21,20 @@ package org.distorted.library;
 
 import android.opengl.GLES30;
 import android.opengl.Matrix;
+
 import java.util.ArrayList;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 abstract class DistortedOutputSurface extends DistortedSurface implements DistortedSlave
 {
+//////////// DEBUG FLAGS /////////////////////////////////////////////
+/**
+ * When rendering a Screen, show FPS in the upper-left corner?
+ */
+public static final int DEBUG_FPS = 1;
+//////////// END DEBUG FLAGS /////////////////////////////////////////
+
 /**
  * Do not create DEPTH or STENCIL attachment
  */
@@ -82,6 +90,13 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
   private int mClear;
   float mMipmap;
 
+  private int mDebugLevel;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  abstract void prepareDebug(long time);
+  abstract void renderDebug(long time);
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   DistortedOutputSurface(int width, int height, int createColor, int numcolors, int depthStencil, int fbo, int type)
@@ -100,6 +115,7 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
     mDepthStencilH[0]= 0;
 
     mTime = 0;
+    mDebugLevel = 0;
 
     mClearR = 0.0f;
     mClearG = 0.0f;
@@ -309,6 +325,20 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
+/**
+ * Make the library show various debugging information.
+ * <p>
+ * Currently only DEBUG_FPS - show FPS in the upper-left corner of every Screen - is defined.
+ *
+ * @param bitmask 0, or a bitmask of DEBUG_** flags to enable (currently only DEBUG_FPS defined)
+ */
+  public void setDebug(int bitmask)
+    {
+    mDebugLevel = bitmask;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
 /**
  * Draws all the attached children to this OutputSurface.
  * <p>
@@ -319,6 +349,10 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
  */
   public int render(long time)
     {
+    boolean showDebug = ( mDebugLevel!=0 && this instanceof DistortedScreen );
+
+    if( showDebug ) prepareDebug(time);
+
     // change tree topology (attach and detach children)
 /*
     boolean changed1 =
@@ -363,10 +397,7 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
     setAsOutput(time);
     numRenders += renderChildren(time,mNumChildren,mChildren);
 
-    if( Distorted.mDebugLevel!=0 && this instanceof DistortedScreen )
-      {
-      Distorted.mDebug.renderFPS(time,this);
-      }
+    if( showDebug ) renderDebug(time);
 
     return numRenders;
     }
diff --git a/src/main/java/org/distorted/library/DistortedScreen.java b/src/main/java/org/distorted/library/DistortedScreen.java
index a355474..97eef9c 100644
--- a/src/main/java/org/distorted/library/DistortedScreen.java
+++ b/src/main/java/org/distorted/library/DistortedScreen.java
@@ -24,8 +24,13 @@ package org.distorted.library;
 import android.app.ActivityManager;
 import android.content.Context;
 import android.content.pm.ConfigurationInfo;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Paint;
 import android.opengl.GLSurfaceView;
 
+import org.distorted.library.type.Static3D;
+
 /**
  * Class which represents the Screen.
  * <p>
@@ -33,6 +38,21 @@ import android.opengl.GLSurfaceView;
  */
 public class DistortedScreen extends DistortedOutputSurface
   {
+  ///// DEBUGGING ONLY /////////////////////////
+  private static final int NUM_FRAMES  = 100;
+
+  private MeshObject fpsMesh;
+  private DistortedTexture fpsTexture;
+  private DistortedEffects fpsEffects;
+  private Canvas fpsCanvas;
+  private Bitmap fpsBitmap;
+  private Paint mPaint;
+  private int fpsH, fpsW;
+  private String fpsString = "";
+  private long lastTime=0;
+  private long[] durations;
+  private int currDuration;
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // here we don't manage underlying OpenGL assets ourselves
 
@@ -40,6 +60,39 @@ public class DistortedScreen extends DistortedOutputSurface
   void delete()   {}
   void recreate() {}
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void prepareDebug(long time)
+    {
+    if( lastTime==0 ) lastTime = time;
+
+    currDuration++;
+    if (currDuration >= NUM_FRAMES) currDuration = 0;
+    durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
+    durations[currDuration] = time - lastTime;
+
+    fpsString = "" + ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
+
+    mPaint.setColor(0xffffffff);
+    fpsCanvas.drawRect(0, 0, fpsW, fpsH, mPaint);
+    mPaint.setColor(0xff000000);
+    fpsCanvas.drawText(fpsString, fpsW/2, 0.75f*fpsH, mPaint);
+    fpsTexture.setTexture(fpsBitmap);
+
+    lastTime = time;
+    }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void renderDebug(long time)
+    {
+    if (fpsTexture.setAsInput())
+      {
+      setAsOutput(time);
+      fpsEffects.drawPriv(fpsW/2.0f, fpsH/2.0f, fpsMesh, this, time, 0);
+      }
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // PUBLIC API
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -61,5 +114,27 @@ public class DistortedScreen extends DistortedOutputSurface
       final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
       view.setEGLContextClientVersion((configurationInfo.reqGlEsVersion >> 16) >= 3 ? 3 : 2);
       }
+
+    /////// DEBUGGING ONLY //////////////
+    fpsW = 120;
+    fpsH =  70;
+
+    fpsBitmap = Bitmap.createBitmap(fpsW,fpsH, Bitmap.Config.ARGB_8888);
+    fpsMesh = new MeshFlat(1,1);
+    fpsTexture = new DistortedTexture(fpsW,fpsH);
+    fpsTexture.setTexture(fpsBitmap);
+    fpsCanvas = new Canvas(fpsBitmap);
+    fpsEffects = new DistortedEffects();
+    fpsEffects.move( new Static3D(5,5,0) );
+
+    mPaint = new Paint();
+    mPaint.setAntiAlias(true);
+    mPaint.setTextAlign(Paint.Align.CENTER);
+    mPaint.setTextSize(0.7f*fpsH);
+
+    durations = new long[NUM_FRAMES+1];
+    currDuration = 0;
+
+    for(int i=0; i<NUM_FRAMES+1; i++) durations[i]=0;
     }
   }
