commit 0273ef2ac16243b081241585d2a85a254ac45d2e
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sat Jul 18 23:39:04 2020 +0100

    Fixes for pausing and restarting.

diff --git a/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java b/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
index c287a6e..f76330e 100644
--- a/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
+++ b/src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
@@ -183,6 +183,7 @@ public class PostprocessEffectBlur extends PostprocessEffect
 
     int radius = (int)(uniforms[index+1]*mQualityScale);
     if( radius>=MAX_RADIUS ) radius = MAX_RADIUS-1;
+    if( radius<=0          ) radius = 1;
     computeGaussianKernel(radius);
 
     int offset = radius + radius*radius/4;
diff --git a/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java b/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
index b6990fa..3c55e78 100644
--- a/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
+++ b/src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
@@ -186,6 +186,7 @@ public class PostprocessEffectGlow extends PostprocessEffect
 
     int radius = (int)(uniforms[index+1]*mQualityScale);
     if( radius>=MAX_RADIUS ) radius = MAX_RADIUS-1;
+    if( radius<=0          ) radius = 1;
     computeGaussianKernel(radius);
 
     int offset = radius + radius*radius/4;
diff --git a/src/main/java/org/distorted/library/effect/VertexEffectWave.java b/src/main/java/org/distorted/library/effect/VertexEffectWave.java
index a5a8f74..eb68adb 100644
--- a/src/main/java/org/distorted/library/effect/VertexEffectWave.java
+++ b/src/main/java/org/distorted/library/effect/VertexEffectWave.java
@@ -51,9 +51,9 @@ public class VertexEffectWave extends VertexEffect
     mRegion.get(uniforms,index+REGION_OFFSET,currentDuration,step);
     boolean ret = mWave.get(uniforms,index+VALUES_OFFSET,currentDuration,step);
 
-    uniforms[index+VALUES_OFFSET+2] = (float)(Math.PI*uniforms[index+2]/180);
-    uniforms[index+VALUES_OFFSET+3] = (float)(Math.PI*uniforms[index+3]/180);
-    uniforms[index+VALUES_OFFSET+4] = (float)(Math.PI*uniforms[index+4]/180);
+    uniforms[index+VALUES_OFFSET+2] = (float)(Math.PI*uniforms[index+VALUES_OFFSET+2]/180);
+    uniforms[index+VALUES_OFFSET+3] = (float)(Math.PI*uniforms[index+VALUES_OFFSET+3]/180);
+    uniforms[index+VALUES_OFFSET+4] = (float)(Math.PI*uniforms[index+VALUES_OFFSET+4]/180);
 
     return ret;
     }
diff --git a/src/main/java/org/distorted/library/effectqueue/EffectQueue.java b/src/main/java/org/distorted/library/effectqueue/EffectQueue.java
index 47d2730..15c3869 100644
--- a/src/main/java/org/distorted/library/effectqueue/EffectQueue.java
+++ b/src/main/java/org/distorted/library/effectqueue/EffectQueue.java
@@ -48,7 +48,6 @@ public abstract class EffectQueue implements InternalMaster.Slave
   private int mNumEffectsToBe;  // added them yet (or less if it hasn't removed some yet)
   float[] mUniforms;
   private int mNumUniforms;
-  long[] mCurrentDuration;
   Effect[] mEffects;
   int[] mName;
   int[] mAndAssociation;
@@ -141,7 +140,6 @@ public abstract class EffectQueue implements InternalMaster.Slave
       if( max>0 )
         {
         mUniforms        = new float[max*source.mNumUniforms];
-        mCurrentDuration = new long[max];
         mEffects         = new Effect[max];
         mName            = new int[max];
         mAndAssociation  = new int[max];
@@ -151,7 +149,6 @@ public abstract class EffectQueue implements InternalMaster.Slave
       for(int i=0; i<mNumEffects; i++ )
         {
         mEffects[i]         = source.mEffects[i];
-        mCurrentDuration[i] = source.mCurrentDuration[i];
         mName[i]            = source.mName[i];
         mAndAssociation[i]  = source.mAndAssociation[i];
         mEquAssociation[i]  = source.mEquAssociation[i];
@@ -281,7 +278,6 @@ public abstract class EffectQueue implements InternalMaster.Slave
     for(int j=effect; j<mNumEffects; j++ )
       {
       mEffects[j]         = mEffects[j+1];
-      mCurrentDuration[j] = mCurrentDuration[j+1];
       mName[j]            = mName[j+1];
       mAndAssociation[j]  = mAndAssociation[j+1];
       mEquAssociation[j]  = mEquAssociation[j+1];
@@ -439,7 +435,6 @@ public abstract class EffectQueue implements InternalMaster.Slave
                      if( max>0 )
                        {
                        mUniforms        = new float[max*job.num];
-                       mCurrentDuration = new long[max];
                        mEffects         = new Effect[max];
                        mName            = new int[max];
                        mAndAssociation  = new int[max];
@@ -454,7 +449,6 @@ public abstract class EffectQueue implements InternalMaster.Slave
 
                        if( position==-1 )
                          {
-                         mCurrentDuration[mNumEffects]= 0;
                          mEffects[mNumEffects]        = job.effect;
                          mName[mNumEffects]           = job.effect.getName().ordinal();
                          mAndAssociation[mNumEffects] = job.effect.getAndAssociation();
@@ -467,14 +461,12 @@ public abstract class EffectQueue implements InternalMaster.Slave
                          {
                          for(int j=mNumEffects; j>position; j--)
                            {
-                           mCurrentDuration[j] = mCurrentDuration[j-1];
                            mEffects[j]         = mEffects[j-1];
                            mName[j]            = mName[j-1];
                            mAndAssociation[j]  = mAndAssociation[j-1];
                            mEquAssociation[j]  = mEquAssociation[j-1];
                            }
 
-                         mCurrentDuration[position]= 0;
                          mEffects[position]        = job.effect;
                          mName[position]           = job.effect.getName().ordinal();
                          mAndAssociation[position] = job.effect.getAndAssociation();
diff --git a/src/main/java/org/distorted/library/effectqueue/EffectQueueFragment.java b/src/main/java/org/distorted/library/effectqueue/EffectQueueFragment.java
index f3c2f35..d6d66f3 100644
--- a/src/main/java/org/distorted/library/effectqueue/EffectQueueFragment.java
+++ b/src/main/java/org/distorted/library/effectqueue/EffectQueueFragment.java
@@ -69,9 +69,7 @@ class EffectQueueFragment extends EffectQueue
 
     for(int i=0; i<mNumEffects; i++)
       {
-      mCurrentDuration[i] += step;
-
-      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
+      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, currTime, step) )
         {
         EffectMessageSender.newMessage(mEffects[i]);
         }
diff --git a/src/main/java/org/distorted/library/effectqueue/EffectQueueMatrix.java b/src/main/java/org/distorted/library/effectqueue/EffectQueueMatrix.java
index 5709e21..66db325 100644
--- a/src/main/java/org/distorted/library/effectqueue/EffectQueueMatrix.java
+++ b/src/main/java/org/distorted/library/effectqueue/EffectQueueMatrix.java
@@ -72,9 +72,7 @@ class EffectQueueMatrix extends EffectQueue
 
     for(int i=0; i<mNumEffects; i++)
       {
-      mCurrentDuration[i] += step;
-
-      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
+      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, currTime, step) )
         {
         EffectMessageSender.newMessage(mEffects[i]);
         }
diff --git a/src/main/java/org/distorted/library/effectqueue/EffectQueuePostprocess.java b/src/main/java/org/distorted/library/effectqueue/EffectQueuePostprocess.java
index 7fa7d6d..a66108c 100644
--- a/src/main/java/org/distorted/library/effectqueue/EffectQueuePostprocess.java
+++ b/src/main/java/org/distorted/library/effectqueue/EffectQueuePostprocess.java
@@ -85,14 +85,12 @@ public class EffectQueuePostprocess extends EffectQueue
 
     for(int i=0; i<mNumEffects; i++)
       {
-      mCurrentDuration[i] += step;
-
       // first zero out the 'alpha' because BLUR effect will not overwrite this (it is a 1D effect)
       // and if previously there was a GLOW effect here then mA would be non-zero and we don't want
       // that (see preprocess())
       mUniforms[NUM_UNIFORMS*i+5]=0.0f;
 
-      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
+      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, currTime, step) )
         {
         EffectMessageSender.newMessage(mEffects[i]);
         }
diff --git a/src/main/java/org/distorted/library/effectqueue/EffectQueueVertex.java b/src/main/java/org/distorted/library/effectqueue/EffectQueueVertex.java
index de7244b..07ba3f1 100644
--- a/src/main/java/org/distorted/library/effectqueue/EffectQueueVertex.java
+++ b/src/main/java/org/distorted/library/effectqueue/EffectQueueVertex.java
@@ -83,11 +83,10 @@ public class EffectQueueVertex extends EffectQueue
 
     for(int i=0; i<mNumEffects; i++)
       {
-      mCurrentDuration[i] += step;
       mAndAssociation[i] = mEffects[i].getAndAssociation();
       mEquAssociation[i] = mEffects[i].getEquAssociation();
 
-      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
+      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, currTime, step) )
         {
         EffectMessageSender.newMessage(mEffects[i]);
         }
diff --git a/src/main/java/org/distorted/library/main/DistortedLibrary.java b/src/main/java/org/distorted/library/main/DistortedLibrary.java
index a5f7023..fab5f9d 100644
--- a/src/main/java/org/distorted/library/main/DistortedLibrary.java
+++ b/src/main/java/org/distorted/library/main/DistortedLibrary.java
@@ -41,6 +41,7 @@ import org.distorted.library.mesh.MeshBase;
 import org.distorted.library.message.EffectMessageSender;
 import org.distorted.library.program.DistortedProgram;
 import org.distorted.library.program.VertexCompilationException;
+import org.distorted.library.type.Dynamic;
 
 import java.io.InputStream;
 import java.nio.ByteBuffer;
@@ -1032,7 +1033,7 @@ public class DistortedLibrary
   public static void onPause()
     {
     InternalObject.onPause();
-
+    Dynamic.onPause();
     mLinkedListSSBO[0]= -1;
     mAtomicCounter = null;
     }
diff --git a/src/main/java/org/distorted/library/type/Dynamic.java b/src/main/java/org/distorted/library/type/Dynamic.java
index 11ca13a..ec9f6fd 100644
--- a/src/main/java/org/distorted/library/type/Dynamic.java
+++ b/src/main/java/org/distorted/library/type/Dynamic.java
@@ -157,8 +157,8 @@ public abstract class Dynamic
   private static Random mRnd = new Random();
   private static final int NUM_NOISE = 5; // used iff mNoise>0.0. Number of intermediary points between each pair of adjacent vectors
                                           // where we randomize noise factors to make the way between the two vectors not so smooth.
-  private long mTimeOffset;
-  private boolean mSetOffset;
+  private long mStartTime;
+  private static long mPausedTime;
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // hide this from Javadoc
@@ -184,15 +184,20 @@ public abstract class Dynamic
     mLastPos   = -1;
     mAccessType= ACCESS_TYPE_RANDOM;
     mConvexity = 1.0f;
-
-    mTimeOffset= 0;
-    mSetOffset = true;
+    mStartTime = 0;
 
     baseV      = new float[mDimension][mDimension];
     buf        = new float[mDimension];
     old        = new float[mDimension];
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  public static void onPause()
+    {
+    mPausedTime = System.currentTimeMillis();
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   protected float noise(float time,int vecNum)
@@ -580,7 +585,7 @@ public abstract class Dynamic
  */
   public void resetToBeginning()
     {
-    mSetOffset = true;
+    mStartTime = 0;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -655,43 +660,6 @@ public abstract class Dynamic
     return mDimension;
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-/**
- * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
- *
- * @param buffer Float buffer we will write the results to.
- * @param offset Offset in the buffer where to write the result.
- * @param time   Time of interpolation. Time=0.0 is the beginning of the first revolution, time=1.0 - the end
- *               of the first revolution, time=2.5 - the middle of the third revolution.
- *               What constitutes 'one revolution' depends on the MODE:
- *               {@link Dynamic#MODE_LOOP}, {@link Dynamic#MODE_PATH} or {@link Dynamic#MODE_JUMP}.
- */
-  public void get(float[] buffer, int offset, long time)
-    {
-    if( mDuration<=0.0f )
-      {
-      interpolate(buffer,offset,mCount-(int)mCount);
-      }
-    else
-      {
-      if( mSetOffset )
-        {
-        mSetOffset = false;
-        mTimeOffset= time;
-        mLastPos   = -1;
-        }
-
-      time -= mTimeOffset;
-
-      double pos = (double)time/mDuration;
-
-      if( pos<=mCount || mCount<=0.0f )
-        {
-        interpolate(buffer,offset, (float)(pos-(int)pos) );
-        }
-      }
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 /**
  * Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
@@ -717,14 +685,21 @@ public abstract class Dynamic
       return false;
       }
 
-    if( mSetOffset )
+    if( mStartTime==0 )
       {
-      mSetOffset = false;
-      mTimeOffset= time;
+      mStartTime = time;
       mLastPos   = -1;
       }
 
-    time -= mTimeOffset;
+    long diff = time-mPausedTime;
+
+    if( diff>=0 && diff<=step )
+      {
+      mStartTime += diff;
+      step -= diff;
+      }
+
+    time -= mStartTime;
 
     if( time+step > mDuration*mCount && mCount>0.0f )
       {
