commit b28c6c21743c6aa35a26c068d083d53250227dc4
Author: leszek <leszek@koltunski.pl>
Date:   Fri May 26 23:26:47 2017 +0100

    Move showing FPS in the uppoer-left corner of the Screen to the Library.
    App can enable/disable this at any time with a single API call.

diff --git a/src/main/java/org/distorted/library/Distorted.java b/src/main/java/org/distorted/library/Distorted.java
index 0d8dd09..76f430b 100644
--- a/src/main/java/org/distorted/library/Distorted.java
+++ b/src/main/java/org/distorted/library/Distorted.java
@@ -34,6 +34,14 @@ 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.
@@ -76,6 +84,9 @@ public class Distorted
 
   private static boolean mInitialized=false;
 
+  static int mDebugLevel = 0;
+  static DistortedDebug mDebug;
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // private: hide this from Javadoc
 
@@ -91,6 +102,21 @@ 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.
diff --git a/src/main/java/org/distorted/library/DistortedDebug.java b/src/main/java/org/distorted/library/DistortedDebug.java
new file mode 100644
index 0000000..2aaa5d9
--- /dev/null
+++ b/src/main/java/org/distorted/library/DistortedDebug.java
@@ -0,0 +1,108 @@
+///////////////////////////////////////////////////////////////////////////////////////////////////
+// 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/DistortedOutputSurface.java b/src/main/java/org/distorted/library/DistortedOutputSurface.java
index f5a663e..a5641ec 100644
--- a/src/main/java/org/distorted/library/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/DistortedOutputSurface.java
@@ -363,6 +363,11 @@ 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);
+      }
+
     return numRenders;
     }
 
diff --git a/src/main/java/org/distorted/library/DistortedScreen.java b/src/main/java/org/distorted/library/DistortedScreen.java
index 9c5d0e3..a355474 100644
--- a/src/main/java/org/distorted/library/DistortedScreen.java
+++ b/src/main/java/org/distorted/library/DistortedScreen.java
@@ -33,7 +33,6 @@ import android.opengl.GLSurfaceView;
  */
 public class DistortedScreen extends DistortedOutputSurface
   {
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // here we don't manage underlying OpenGL assets ourselves
 
