diff --git a/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java b/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java index fbd7c22..2fe23f9 100644 --- a/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java +++ b/src/mightypork/rogue/screens/test_bouncyboxes/BouncyBox.java @@ -9,8 +9,8 @@ import mightypork.gamecore.gui.components.SimplePainter; import mightypork.gamecore.render.Render; import mightypork.utils.math.Easing; import mightypork.utils.math.color.RGB; -import mightypork.utils.math.constraints.num.NumAnimated; import mightypork.utils.math.constraints.num.Num; +import mightypork.utils.math.constraints.num.NumAnimated; import mightypork.utils.math.constraints.rect.Rect; diff --git a/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java b/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java index e69dece..239e787 100644 --- a/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java +++ b/src/mightypork/rogue/screens/test_cat_sound/LayerFlyingCat.java @@ -16,8 +16,8 @@ import mightypork.gamecore.render.fonts.FontRenderer.Align; import mightypork.rogue.Res; import mightypork.utils.math.Easing; import mightypork.utils.math.color.RGB; -import mightypork.utils.math.constraints.num.NumAnimated; import mightypork.utils.math.constraints.num.Num; +import mightypork.utils.math.constraints.num.NumAnimated; import mightypork.utils.math.constraints.rect.Rect; import mightypork.utils.math.constraints.vect.Vect; import mightypork.utils.math.constraints.vect.VectAnimated; diff --git a/src/mightypork/test/TestConstCaching.java b/src/mightypork/test/TestConstCaching.java new file mode 100644 index 0000000..67d4dbd --- /dev/null +++ b/src/mightypork/test/TestConstCaching.java @@ -0,0 +1,39 @@ +package mightypork.test; + +import mightypork.utils.math.constraints.vect.Vect; +import mightypork.utils.math.constraints.vect.VectCache; +import mightypork.utils.math.constraints.vect.VectVar; + + +public class TestConstCaching { + public static void main(String[] args) + { + VectVar in = Vect.makeVar(0, 0); + VectCache cache = in.cached(); + cache.enableDigestCaching(true); + + System.out.println("in = "+in); + System.out.println("cache = "+cache); + System.out.println("cache digest = " + cache.digest()); + System.out.println("\n-- in := 100, 50, 25 --\n"); + in.setTo(100,50,25); + System.out.println("in = "+in); + System.out.println("cache = "+cache); + System.out.println("cache digest = " + cache.digest()); + System.out.println("\n-- cache.poll() --\n"); + cache.poll(); + System.out.println("in = "+in); + System.out.println("cache = "+cache); + System.out.println("cache digest = " + cache.digest()); + System.out.println("\n-- in := 1, 2, 3 --\n"); + in.setTo(1,2,3); + System.out.println("in = "+in); + System.out.println("cache = "+cache); + System.out.println("cache digest = " + cache.digest()); + System.out.println("\n-- cache.poll() --\n"); + cache.poll(); + System.out.println("cache = "+cache); + System.out.println("cache digest = " + cache.digest()); + + } +} diff --git a/src/mightypork/utils/math/constraints/num/Num.java b/src/mightypork/utils/math/constraints/num/Num.java index 59c813e..b068227 100644 --- a/src/mightypork/utils/math/constraints/num/Num.java +++ b/src/mightypork/utils/math/constraints/num/Num.java @@ -62,8 +62,9 @@ public abstract class Num implements NumBound, Digestable { private Num p_square; private Num p_neg; private Num p_abs; - private NumDigest lastDigest; - private boolean digestCachingEnabled; + private NumDigest lastDigest = null; + private boolean digestCachingEnabled = false; + private boolean digestDirty = true; public NumConst freeze() @@ -72,6 +73,18 @@ public abstract class Num implements NumBound, Digestable { } + /** + * Wrap this constraint into a caching adapter. Value will stay fixed (ie. + * no re-calculations) until cache receives a poll() call. + * + * @return the caching adapter + */ + public NumCache cached() + { + return new NumCache(this); + } + + /** * Get a snapshot of the current state, to be used for processing. * @@ -81,9 +94,16 @@ public abstract class Num implements NumBound, Digestable { @Override public NumDigest digest() { - if (digestCachingEnabled && lastDigest != null) return lastDigest; + if (digestCachingEnabled) { + if (digestDirty) { + lastDigest = new NumDigest(this); + digestDirty = false; + } + + return lastDigest; + } - return lastDigest = new NumDigest(this); + return new NumDigest(this); } @@ -91,12 +111,10 @@ public abstract class Num implements NumBound, Digestable { public void enableDigestCaching(boolean yes) { digestCachingEnabled = yes; + digestDirty = true; // schedule update nest time digest() is called } - /** - * @return true if digest caching is enabled. - */ @Override public boolean isDigestCachingEnabled() { @@ -107,7 +125,7 @@ public abstract class Num implements NumBound, Digestable { @Override public void poll() { - if (digestCachingEnabled) lastDigest = new NumDigest(this); + if (digestCachingEnabled) digestDirty = true; } diff --git a/src/mightypork/utils/math/constraints/num/NumAnimatedDeg.java b/src/mightypork/utils/math/constraints/num/NumAnimatedDeg.java index 7549eb2..f3278f0 100644 --- a/src/mightypork/utils/math/constraints/num/NumAnimatedDeg.java +++ b/src/mightypork/utils/math/constraints/num/NumAnimatedDeg.java @@ -2,8 +2,8 @@ package mightypork.utils.math.constraints.num; import mightypork.utils.math.Calc; -import mightypork.utils.math.Easing; import mightypork.utils.math.Calc.Deg; +import mightypork.utils.math.Easing; /** diff --git a/src/mightypork/utils/math/constraints/num/NumAnimatedRad.java b/src/mightypork/utils/math/constraints/num/NumAnimatedRad.java index cd7df9c..414ad46 100644 --- a/src/mightypork/utils/math/constraints/num/NumAnimatedRad.java +++ b/src/mightypork/utils/math/constraints/num/NumAnimatedRad.java @@ -2,8 +2,8 @@ package mightypork.utils.math.constraints.num; import mightypork.utils.math.Calc; -import mightypork.utils.math.Easing; import mightypork.utils.math.Calc.Rad; +import mightypork.utils.math.Easing; /** diff --git a/src/mightypork/utils/math/constraints/num/NumBound.java b/src/mightypork/utils/math/constraints/num/NumBound.java index 2f18266..911fd0b 100644 --- a/src/mightypork/utils/math/constraints/num/NumBound.java +++ b/src/mightypork/utils/math/constraints/num/NumBound.java @@ -1,7 +1,6 @@ package mightypork.utils.math.constraints.num; - /** * Numeric constraint * diff --git a/src/mightypork/utils/math/constraints/num/NumBoundAdapter.java b/src/mightypork/utils/math/constraints/num/NumBoundAdapter.java index ada4955..e702c1b 100644 --- a/src/mightypork/utils/math/constraints/num/NumBoundAdapter.java +++ b/src/mightypork/utils/math/constraints/num/NumBoundAdapter.java @@ -1,8 +1,6 @@ package mightypork.utils.math.constraints.num; - - public class NumBoundAdapter extends NumAdapter implements PluggableNumBound { private NumBound backing = null; diff --git a/src/mightypork/utils/math/constraints/num/NumCache.java b/src/mightypork/utils/math/constraints/num/NumCache.java new file mode 100644 index 0000000..435a01f --- /dev/null +++ b/src/mightypork/utils/math/constraints/num/NumCache.java @@ -0,0 +1,37 @@ +package mightypork.utils.math.constraints.num; + + +public class NumCache extends NumAdapter { + + private final NumVar cache = Num.makeVar(); + private final Num source; + private boolean inited = false; + + + public NumCache(Num source) { + this.source = source; + } + + + @Override + protected Num getSource() + { + if (!inited) poll(); + + return cache; + } + + + /** + * Update cached value and cached digest (if digest caching is enabled) + */ + @Override + public void poll() + { + inited = true; + cache.setTo(source); + + super.poll(); + } + +} diff --git a/src/mightypork/utils/math/constraints/num/NumProxy.java b/src/mightypork/utils/math/constraints/num/NumProxy.java new file mode 100644 index 0000000..27d0d12 --- /dev/null +++ b/src/mightypork/utils/math/constraints/num/NumProxy.java @@ -0,0 +1,20 @@ +package mightypork.utils.math.constraints.num; + + +public class NumProxy extends NumAdapter { + + private final Num source; + + + public NumProxy(Num source) { + this.source = source; + } + + + @Override + protected Num getSource() + { + return source; + } + +} diff --git a/src/mightypork/utils/math/constraints/num/PluggableNumBound.java b/src/mightypork/utils/math/constraints/num/PluggableNumBound.java index ba71230..597b861 100644 --- a/src/mightypork/utils/math/constraints/num/PluggableNumBound.java +++ b/src/mightypork/utils/math/constraints/num/PluggableNumBound.java @@ -1,8 +1,6 @@ package mightypork.utils.math.constraints.num; - - /** * Pluggable numeric constraint * diff --git a/src/mightypork/utils/math/constraints/rect/Rect.java b/src/mightypork/utils/math/constraints/rect/Rect.java index db92f16..e0610e8 100644 --- a/src/mightypork/utils/math/constraints/rect/Rect.java +++ b/src/mightypork/utils/math/constraints/rect/Rect.java @@ -161,6 +161,7 @@ public abstract class Rect implements RectBound, Digestable { private RectDigest lastDigest = null; private boolean digestCachingEnabled = false; + private boolean digestDirty = true; // digest is null /** @@ -175,12 +176,31 @@ public abstract class Rect implements RectBound, Digestable { } + /** + * Wrap this constraint into a caching adapter. Value will stay fixed (ie. + * no re-calculations) until cache receives a poll() call. + * + * @return the caching adapter + */ + public RectCache cached() + { + return new RectCache(this); + } + + @Override public RectDigest digest() { - if (digestCachingEnabled && lastDigest != null) return lastDigest; + if (digestCachingEnabled) { + if (digestDirty) { + lastDigest = new RectDigest(this); + digestDirty = false; + } + + return lastDigest; + } - return lastDigest = new RectDigest(this); + return new RectDigest(this); } @@ -188,12 +208,10 @@ public abstract class Rect implements RectBound, Digestable { public void enableDigestCaching(boolean yes) { digestCachingEnabled = yes; + digestDirty = true; // schedule update nest time digest() is called } - /** - * @return true if digest caching is enabled. - */ @Override public boolean isDigestCachingEnabled() { @@ -204,7 +222,7 @@ public abstract class Rect implements RectBound, Digestable { @Override public void poll() { - if (digestCachingEnabled) lastDigest = new RectDigest(this); + if (digestCachingEnabled) digestDirty = true; } diff --git a/src/mightypork/utils/math/constraints/rect/RectBound.java b/src/mightypork/utils/math/constraints/rect/RectBound.java index a112c41..99cd04b 100644 --- a/src/mightypork/utils/math/constraints/rect/RectBound.java +++ b/src/mightypork/utils/math/constraints/rect/RectBound.java @@ -1,8 +1,6 @@ package mightypork.utils.math.constraints.rect; - - /** * Rect constraint (ie. region) * diff --git a/src/mightypork/utils/math/constraints/rect/RectBoundAdapter.java b/src/mightypork/utils/math/constraints/rect/RectBoundAdapter.java index deabf5a..96f3247 100644 --- a/src/mightypork/utils/math/constraints/rect/RectBoundAdapter.java +++ b/src/mightypork/utils/math/constraints/rect/RectBoundAdapter.java @@ -1,8 +1,6 @@ package mightypork.utils.math.constraints.rect; - - /** * Pluggable rect bound adapter * diff --git a/src/mightypork/utils/math/constraints/rect/RectCache.java b/src/mightypork/utils/math/constraints/rect/RectCache.java new file mode 100644 index 0000000..83e0f60 --- /dev/null +++ b/src/mightypork/utils/math/constraints/rect/RectCache.java @@ -0,0 +1,37 @@ +package mightypork.utils.math.constraints.rect; + + +public class RectCache extends RectAdapter { + + private final RectVar cache = Rect.makeVar(); + private final Rect source; + private boolean inited = false; + + + public RectCache(Rect source) { + this.source = source; + } + + + @Override + protected Rect getSource() + { + if (!inited) poll(); + + return cache; + } + + + /** + * Update cached value and cached digest (if digest caching is enabled) + */ + @Override + public void poll() + { + inited = true; + cache.setTo(source); + + super.poll(); + } + +} diff --git a/src/mightypork/utils/math/constraints/rect/RectDigest.java b/src/mightypork/utils/math/constraints/rect/RectDigest.java index 70dcf85..f3d4259 100644 --- a/src/mightypork/utils/math/constraints/rect/RectDigest.java +++ b/src/mightypork/utils/math/constraints/rect/RectDigest.java @@ -1,8 +1,6 @@ package mightypork.utils.math.constraints.rect; - - public class RectDigest { public final double x; diff --git a/src/mightypork/utils/math/constraints/vect/Vect.java b/src/mightypork/utils/math/constraints/vect/Vect.java index a6b0e85..5e5dda8 100644 --- a/src/mightypork/utils/math/constraints/vect/Vect.java +++ b/src/mightypork/utils/math/constraints/vect/Vect.java @@ -104,8 +104,9 @@ public abstract class Vect implements VectBound, Digestable { private Num p_xc; private Num p_yc; private Num p_zc; - private boolean digestCachingEnabled; - private VectDigest lastDigest; + private VectDigest lastDigest = null; + private boolean digestCachingEnabled = false; + private boolean digestDirty = true; // it's null /** @@ -238,12 +239,31 @@ public abstract class Vect implements VectBound, Digestable { } + /** + * Wrap this constraint into a caching adapter. Value will stay fixed (ie. + * no re-calculations) until cache receives a poll() call. + * + * @return the caching adapter + */ + public VectCache cached() + { + return new VectCache(this); + } + + @Override public VectDigest digest() { - if (digestCachingEnabled && lastDigest != null) return lastDigest; + if (digestCachingEnabled) { + if (digestDirty) { + lastDigest = new VectDigest(this); + digestDirty = false; + } + + return lastDigest; + } - return lastDigest = new VectDigest(this); + return new VectDigest(this); } @@ -251,6 +271,7 @@ public abstract class Vect implements VectBound, Digestable { public void enableDigestCaching(boolean yes) { digestCachingEnabled = yes; + digestDirty = true; // schedule update nest time digest() is called } @@ -264,7 +285,7 @@ public abstract class Vect implements VectBound, Digestable { @Override public void poll() { - if (digestCachingEnabled) lastDigest = new VectDigest(this); + if (digestCachingEnabled) digestDirty = true; } @@ -1036,7 +1057,7 @@ public abstract class Vect implements VectBound, Digestable { * @param vec other vector * @return result */ - public final Vect cross(final Vect vec) + public Vect cross(final Vect vec) { return new Vect() { diff --git a/src/mightypork/utils/math/constraints/vect/VectBound.java b/src/mightypork/utils/math/constraints/vect/VectBound.java index 08c4014..678c57f 100644 --- a/src/mightypork/utils/math/constraints/vect/VectBound.java +++ b/src/mightypork/utils/math/constraints/vect/VectBound.java @@ -1,8 +1,6 @@ package mightypork.utils.math.constraints.vect; - - /** * Element holding a vector, used for constraint building. * diff --git a/src/mightypork/utils/math/constraints/vect/VectBoundAdapter.java b/src/mightypork/utils/math/constraints/vect/VectBoundAdapter.java index 43b1055..abbc96e 100644 --- a/src/mightypork/utils/math/constraints/vect/VectBoundAdapter.java +++ b/src/mightypork/utils/math/constraints/vect/VectBoundAdapter.java @@ -1,8 +1,6 @@ package mightypork.utils.math.constraints.vect; - - public class VectBoundAdapter extends VectAdapter implements PluggableVectBound { private VectBound backing = null; diff --git a/src/mightypork/utils/math/constraints/vect/VectCache.java b/src/mightypork/utils/math/constraints/vect/VectCache.java new file mode 100644 index 0000000..daa0cd2 --- /dev/null +++ b/src/mightypork/utils/math/constraints/vect/VectCache.java @@ -0,0 +1,37 @@ +package mightypork.utils.math.constraints.vect; + + +public class VectCache extends VectAdapter { + + private final VectVar cache = Vect.makeVar(); + private final Vect source; + private boolean inited = false; + + + public VectCache(Vect source) { + this.source = source; + } + + + @Override + protected Vect getSource() + { + if (!inited) poll(); + + return cache; + } + + + /** + * Update cached value and cached digest (if digest caching is enabled) + */ + @Override + public void poll() + { + inited = true; + cache.setTo(source); + + super.poll(); + } + +} diff --git a/src/mightypork/utils/math/constraints/vect/VectConst.java b/src/mightypork/utils/math/constraints/vect/VectConst.java index 2e5b758..6d616a6 100644 --- a/src/mightypork/utils/math/constraints/vect/VectConst.java +++ b/src/mightypork/utils/math/constraints/vect/VectConst.java @@ -351,6 +351,12 @@ public final class VectConst extends Vect { } + public VectConst cross(VectConst vec) + { + return super.cross(vec).freeze(); + } + + @Override public RectConst expand(double left, double right, double top, double bottom) { diff --git a/src/mightypork/utils/math/constraints/vect/VectProxy.java b/src/mightypork/utils/math/constraints/vect/VectProxy.java new file mode 100644 index 0000000..bc12bd9 --- /dev/null +++ b/src/mightypork/utils/math/constraints/vect/VectProxy.java @@ -0,0 +1,20 @@ +package mightypork.utils.math.constraints.vect; + + +public class VectProxy extends VectAdapter { + + private final Vect source; + + + public VectProxy(Vect source) { + this.source = source; + } + + + @Override + protected Vect getSource() + { + return source; + } + +}