commit c7da4e65f30a917a778c3e71e53c8aa2145cc82b
Author: leszek <leszek@koltunski.pl>
Date:   Wed Feb 15 00:30:16 2017 +0000

    Fix OutputSurface.resize(): before it couldn't be called mid-render.

diff --git a/src/main/java/org/distorted/library/DistortedEffects.java b/src/main/java/org/distorted/library/DistortedEffects.java
index 1aeb4af..4a4bda5 100644
--- a/src/main/java/org/distorted/library/DistortedEffects.java
+++ b/src/main/java/org/distorted/library/DistortedEffects.java
@@ -342,7 +342,7 @@ public class DistortedEffects
  */
   public DistortedEffects()
     {
-    mID = mNextID++;
+    mID = ++mNextID;
     initializeEffectLists(this,0);
     }
 
@@ -358,7 +358,7 @@ public class DistortedEffects
  */
   public DistortedEffects(DistortedEffects dc, int flags)
     {
-    mID = mNextID++;
+    mID = ++mNextID;
     initializeEffectLists(dc,flags);
     }
 
diff --git a/src/main/java/org/distorted/library/DistortedFramebuffer.java b/src/main/java/org/distorted/library/DistortedFramebuffer.java
index d893e77..3dad70a 100644
--- a/src/main/java/org/distorted/library/DistortedFramebuffer.java
+++ b/src/main/java/org/distorted/library/DistortedFramebuffer.java
@@ -37,7 +37,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
 
   void create()
     {
-    if( mColorH[0]==NOT_CREATED_YET )
+    if( mColorCreated==NOT_CREATED_YET )
       {
       GLES30.glGenTextures(1, mColorH, 0);
       GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
@@ -51,9 +51,9 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
       GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
       GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
 
-      checkStatus("color");
+      mColorCreated = checkStatus("color");
       }
-    if( mDepthEnabled && mDepthH[0]==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
+    if( mDepthCreated==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
       {
       GLES30.glGenTextures(1, mDepthH, 0);
       GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthH[0]);
@@ -66,18 +66,18 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
       GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
       GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthH[0], 0);
 
-      checkStatus("depth");
+      mDepthCreated = checkStatus("depth");
       }
-    if( !mDepthEnabled && mDepthH[0]!=NOT_CREATED_YET ) // we need to detach and recreate the DEPTH attachment.
+    if( mDepthCreated==DONT_CREATE && mDepthH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
       {
       GLES30.glDeleteTextures(1, mDepthH, 0);
-      mDepthH[0]=NOT_CREATED_YET;
+      mDepthH[0]=0;
       }
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  private boolean checkStatus(String message)
+  private int checkStatus(String message)
     {
     int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
 
@@ -88,14 +88,12 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
       GLES30.glDeleteTextures(1, mColorH, 0);
       GLES30.glDeleteTextures(1, mDepthH, 0);
       GLES30.glDeleteFramebuffers(1, mFBOH, 0);
-      mFBOH[0]   = 0;
-      mColorH[0] = FAILED_TO_CREATE;
-      mDepthH[0] = FAILED_TO_CREATE;
+      mFBOH[0]= 0;
 
-      return false;
+      return FAILED_TO_CREATE;
       }
 
-    return true;
+    return CREATED;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -103,16 +101,18 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
 
   void delete()
     {
-    if( mColorH[0]>=0 )
+    if( mColorH[0]>0 )
       {
-      if( mDepthH[0]>=0 )
+      if( mDepthH[0]>0 )
         {
         GLES30.glDeleteTextures(1, mDepthH, 0);
-        mDepthH[0]=NOT_CREATED_YET;
+        mDepthH[0]=0;
+        mDepthCreated = NOT_CREATED_YET;
         }
 
       GLES30.glDeleteTextures(1, mColorH, 0);
-      mColorH[0] = NOT_CREATED_YET;
+      mColorH[0] = 0;
+      mColorCreated = NOT_CREATED_YET;
 
       GLES30.glDeleteFramebuffers(1, mFBOH, 0);
       mFBOH[0] = 0;
@@ -124,8 +124,8 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
 
   void recreate()
     {
-    if( mColorH[0]!=DONT_CREATE ) mColorH[0] = NOT_CREATED_YET;
-    if( mDepthEnabled           ) mDepthH[0] = NOT_CREATED_YET;
+    if( mColorCreated!=DONT_CREATE ) mColorCreated = NOT_CREATED_YET;
+    if( mDepthCreated!=DONT_CREATE ) mDepthCreated = NOT_CREATED_YET;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -159,7 +159,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
 
   DistortedFramebuffer(boolean depthEnabled, int width, int height)
     {
-    super(width,height,NOT_CREATED_YET,NOT_CREATED_YET,true, depthEnabled);
+    super(width,height,NOT_CREATED_YET, (depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET, true);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -175,7 +175,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
   @SuppressWarnings("unused")
   public DistortedFramebuffer(int width, int height, boolean depthEnabled)
     {
-    super(width,height,NOT_CREATED_YET,NOT_CREATED_YET,false, depthEnabled);
+    super(width,height,NOT_CREATED_YET,(depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET,false);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -189,7 +189,7 @@ public class DistortedFramebuffer extends DistortedOutputSurface implements Dist
   @SuppressWarnings("unused")
   public DistortedFramebuffer(int width, int height)
     {
-    super(width,height,NOT_CREATED_YET,NOT_CREATED_YET,false,false);
+    super(width,height,NOT_CREATED_YET,DONT_CREATE,NOT_CREATED_YET,false);
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/DistortedOutputSurface.java b/src/main/java/org/distorted/library/DistortedOutputSurface.java
index b8e92e9..5e913d4 100644
--- a/src/main/java/org/distorted/library/DistortedOutputSurface.java
+++ b/src/main/java/org/distorted/library/DistortedOutputSurface.java
@@ -37,15 +37,15 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
   float mDistance;
   float[] mProjectionMatrix;
 
-  boolean mDepthEnabled;
+  int mDepthCreated;
   int[] mDepthH = new int[1];
   int[] mFBOH   = new int[1];
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  DistortedOutputSurface(int width, int height, int color, int fbo, boolean system, boolean depth)
+  DistortedOutputSurface(int width, int height, int createColor, int createDepth, int fbo, boolean system)
     {
-    super(width,height,color,system);
+    super(width,height,createColor,system);
 
     mProjectionMatrix = new float[16];
 
@@ -56,9 +56,9 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
     mX   =  0.0f;
     mY   =  0.0f;
 
-    mDepthEnabled= depth;
+    mDepthCreated= createDepth;
     mFBOH[0]     = fbo;
-    mDepthH[0]   = color;
+    mDepthH[0]   = 0;
 
     createProjection();
     }
@@ -158,7 +158,7 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
     {
     GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
 
-    if( mDepthEnabled && mDepthH[0]!=NOT_CREATED_YET )
+    if( mDepthCreated==CREATED )
       {
       GLES30.glEnable(GLES30.GL_DEPTH_TEST);
       GLES30.glDepthMask(true);
@@ -190,6 +190,8 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Resize the underlying Framebuffer.
+ * <p>
+ * This method can be safely called mid-render as it doesn't interfere with rendering.
  *
  * @param width The new width.
  * @param height The new height.
@@ -205,7 +207,7 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
 
       createProjection();
 
-      if( mColorH[0]>0 )
+      if( mColorCreated==CREATED )
         {
         moveToToDo();
         recreate();
@@ -222,9 +224,14 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
  */
   public void enableDepth(boolean enable)
     {
-    if( mDepthEnabled!=enable )
+    if( enable && mDepthCreated==DONT_CREATE )
+      {
+      mDepthCreated = NOT_CREATED_YET;
+      moveToToDo();
+      }
+    if( !enable && mDepthCreated!=DONT_CREATE )
       {
-      mDepthEnabled = enable;
+      mDepthCreated = DONT_CREATE;
       moveToToDo();
       }
     }
@@ -237,7 +244,7 @@ abstract class DistortedOutputSurface extends DistortedSurface implements Distor
  */
   public boolean hasDepth()
     {
-    return mDepthEnabled;
+    return mDepthCreated==CREATED;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/library/DistortedScreen.java b/src/main/java/org/distorted/library/DistortedScreen.java
index d3c2f4e..009c486 100644
--- a/src/main/java/org/distorted/library/DistortedScreen.java
+++ b/src/main/java/org/distorted/library/DistortedScreen.java
@@ -44,6 +44,6 @@ public class DistortedScreen extends DistortedOutputSurface
  */
   public DistortedScreen()
     {
-    super(0,0,DONT_CREATE,0,false,true);
+    super(0,0,CREATED,CREATED,0,false);
     }
   }
diff --git a/src/main/java/org/distorted/library/DistortedSurface.java b/src/main/java/org/distorted/library/DistortedSurface.java
index 9328fce..ca921b2 100644
--- a/src/main/java/org/distorted/library/DistortedSurface.java
+++ b/src/main/java/org/distorted/library/DistortedSurface.java
@@ -31,9 +31,10 @@ import java.util.LinkedList;
 */
 abstract class DistortedSurface
   {
-  static final int FAILED_TO_CREATE = -1;
-  static final int NOT_CREATED_YET  = -2;
-  static final int DONT_CREATE      = -3;
+  static final int FAILED_TO_CREATE = 1;
+  static final int NOT_CREATED_YET  = 2;
+  static final int DONT_CREATE      = 3;
+  static final int CREATED          = 4;
 
   private static boolean mToDo = false;
   private static LinkedList<DistortedSurface> mDoneList = new LinkedList<>();
@@ -44,6 +45,7 @@ abstract class DistortedSurface
   private long mID;
   private boolean mMarked;
   private boolean mSystem;
+  int mColorCreated;
   int[] mColorH = new int[1];
   int mSizeX, mSizeY;  // in screen space
 
@@ -169,16 +171,17 @@ abstract class DistortedSurface
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
-  DistortedSurface(int width, int height, int color, boolean system)
+  DistortedSurface(int width, int height, int create, boolean system)
     {
-    mSizeX    = width ;
-    mSizeY    = height;
-    mColorH[0]= color;
-    mMarked   = false;
-    mID       = system ? --mNextSystemID : ++mNextClientID;
-    mSystem   = system;
-
-    if( color!=DONT_CREATE )
+    mSizeX        = width ;
+    mSizeY        = height;
+    mColorCreated = create;
+    mColorH[0]    = 0;
+    mMarked       = false;
+    mID           = system ? --mNextSystemID : ++mNextClientID;
+    mSystem       = system;
+
+    if( create!=DONT_CREATE )
       {
       mToDoList.add(this);
       mToDo = true;
diff --git a/src/main/java/org/distorted/library/DistortedTexture.java b/src/main/java/org/distorted/library/DistortedTexture.java
index 7a26118..712acb1 100644
--- a/src/main/java/org/distorted/library/DistortedTexture.java
+++ b/src/main/java/org/distorted/library/DistortedTexture.java
@@ -57,10 +57,11 @@ public class DistortedTexture extends DistortedSurface implements DistortedInput
 
   void create()
     {
-    if( mBmp!=null && mColorH !=null )
+    if( mBmp!=null )
       {
-      if( mColorH[0]==NOT_CREATED_YET )
+      if( mColorCreated==NOT_CREATED_YET )
         {
+        mColorCreated = CREATED;
         GLES30.glGenTextures(1, mColorH, 0);
         GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
         GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR );
@@ -84,10 +85,11 @@ public class DistortedTexture extends DistortedSurface implements DistortedInput
 
   void delete()
     {
-    if( mColorH !=null && mColorH[0]>=0 )
+    if( mColorH[0]>=0 )
       {
       GLES30.glDeleteTextures(1, mColorH, 0);
-      mColorH[0] = NOT_CREATED_YET;
+      mColorH[0] = 0;
+      mColorCreated = NOT_CREATED_YET;
       }
     }
 
@@ -96,7 +98,7 @@ public class DistortedTexture extends DistortedSurface implements DistortedInput
 
   void recreate()
     {
-    if( mColorH[0]!=DONT_CREATE ) mColorH[0] = NOT_CREATED_YET;
+    if( mColorCreated!=DONT_CREATE ) mColorCreated = NOT_CREATED_YET;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
