From 308473368438ef838b774ec13b5a11e1a0b3d9d0 Mon Sep 17 00:00:00 2001 From: Kipp Ashford Date: Tue, 7 Oct 2014 09:46:41 -0700 Subject: [PATCH 1/6] Cache sine and cosine calculations for a tiny performance boost. --- src/flambe/display/Sprite.hx | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/flambe/display/Sprite.hx b/src/flambe/display/Sprite.hx index 0d4dff26..07061c86 100644 --- a/src/flambe/display/Sprite.hx +++ b/src/flambe/display/Sprite.hx @@ -122,7 +122,7 @@ class Sprite extends Component public function new () { - _flags = _flags.add(VISIBLE | POINTER_ENABLED | VIEW_MATRIX_DIRTY | PIXEL_SNAPPING); + _flags = _flags.add(VISIBLE | POINTER_ENABLED | VIEW_MATRIX_DIRTY | PIXEL_SNAPPING | ROTATION_DIRTY); _localMatrix = new Matrix(); var dirtyMatrix = function (_,_) { @@ -130,7 +130,9 @@ class Sprite extends Component }; x = new AnimatedFloat(0, dirtyMatrix); y = new AnimatedFloat(0, dirtyMatrix); - rotation = new AnimatedFloat(0, dirtyMatrix); + rotation = new AnimatedFloat(0, function(_,_) { + _flags = _flags.add(LOCAL_MATRIX_DIRTY | VIEW_MATRIX_DIRTY | ROTATION_DIRTY); + }); scaleX = new AnimatedFloat(1, dirtyMatrix); scaleY = new AnimatedFloat(1, dirtyMatrix); anchorX = new AnimatedFloat(0, dirtyMatrix); @@ -299,8 +301,19 @@ class Sprite extends Component { if (_flags.contains(LOCAL_MATRIX_DIRTY)) { _flags = _flags.remove(LOCAL_MATRIX_DIRTY); + var rotation :Float = this.rotation._; + var x :Float = this.x._; + var y :Float = this.y._; + var scaleX :Float = this.scaleX._; + var scaleY :Float = this.scaleY._; + + if(_flags.contains(ROTATION_DIRTY)) { + _flags = _flags.remove(ROTATION_DIRTY); + _sinCache = Math.sin(rotation); + _cosCache = Math.cos(rotation); + } - _localMatrix.compose(x._, y._, scaleX._, scaleY._, FMath.toRadians(rotation._)); + _localMatrix.set(_cosCache*scaleX, _sinCache*scaleX, -_sinCache*scaleY, _cosCache*scaleY, x, y); _localMatrix.translate(-anchorX._, -anchorY._); } return _localMatrix; @@ -727,14 +740,17 @@ class Sprite extends Component private static inline var VIEW_MATRIX_DIRTY = Component.NEXT_FLAG << 3; private static inline var PIXEL_SNAPPING = Component.NEXT_FLAG << 4; private static inline var HOVERING = Component.NEXT_FLAG << 5; - private static inline var NEXT_FLAG = Component.NEXT_FLAG << 6; // Must be last! + private static inline var ROTATION_DIRTY = Component.NEXT_FLAG << 6; + private static inline var NEXT_FLAG = Component.NEXT_FLAG << 7; // Must be last! private var _localMatrix :Matrix; private var _viewMatrix :Matrix = null; private var _viewMatrixUpdateCount :Int = 0; private var _parentViewMatrixUpdateCount :Int = 0; - + private var _sinCache :Float = 0; + private var _cosCache :Float = 0; +#if debug private var _savedRotations :Int = 0; #end private var _pointerDown :Signal1; private var _pointerMove :Signal1; private var _pointerUp :Signal1; From be9ac355c0b960ebef0bcdc9a4410995f17b6ba7 Mon Sep 17 00:00:00 2001 From: Kipp Ashford Date: Tue, 14 Oct 2014 09:52:43 -0700 Subject: [PATCH 2/6] Tidy up a little. --- src/flambe/display/Sprite.hx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/flambe/display/Sprite.hx b/src/flambe/display/Sprite.hx index 07061c86..db0f84a8 100644 --- a/src/flambe/display/Sprite.hx +++ b/src/flambe/display/Sprite.hx @@ -301,19 +301,17 @@ class Sprite extends Component { if (_flags.contains(LOCAL_MATRIX_DIRTY)) { _flags = _flags.remove(LOCAL_MATRIX_DIRTY); - var rotation :Float = this.rotation._; - var x :Float = this.x._; - var y :Float = this.y._; - var scaleX :Float = this.scaleX._; - var scaleY :Float = this.scaleY._; if(_flags.contains(ROTATION_DIRTY)) { _flags = _flags.remove(ROTATION_DIRTY); + var rotation :Float = this.rotation._; _sinCache = Math.sin(rotation); _cosCache = Math.cos(rotation); } - _localMatrix.set(_cosCache*scaleX, _sinCache*scaleX, -_sinCache*scaleY, _cosCache*scaleY, x, y); + var scaleX :Float = this.scaleX._; + var scaleY :Float = this.scaleY._; + _localMatrix.set(_cosCache*scaleX, _sinCache*scaleX, -_sinCache*scaleY, _cosCache*scaleY, x._, y._); _localMatrix.translate(-anchorX._, -anchorY._); } return _localMatrix; From 1782821073907ed4b07d77496de7f5a1a2dd6be3 Mon Sep 17 00:00:00 2001 From: Kipp Ashford Date: Tue, 14 Oct 2014 09:59:11 -0700 Subject: [PATCH 3/6] Forgot to translate rotation to radians. --- src/flambe/display/Sprite.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flambe/display/Sprite.hx b/src/flambe/display/Sprite.hx index db0f84a8..b448d761 100644 --- a/src/flambe/display/Sprite.hx +++ b/src/flambe/display/Sprite.hx @@ -304,7 +304,7 @@ class Sprite extends Component if(_flags.contains(ROTATION_DIRTY)) { _flags = _flags.remove(ROTATION_DIRTY); - var rotation :Float = this.rotation._; + var rotation :Float = FMath.toRadians(this.rotation._); _sinCache = Math.sin(rotation); _cosCache = Math.cos(rotation); } From 7d87f9eb608adb5b69fd6c5f59def0d90c720f6a Mon Sep 17 00:00:00 2001 From: Kipp Ashford Date: Tue, 14 Oct 2014 10:12:46 -0700 Subject: [PATCH 4/6] Small cleanup --- src/flambe/display/Sprite.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flambe/display/Sprite.hx b/src/flambe/display/Sprite.hx index b448d761..d4efdccc 100644 --- a/src/flambe/display/Sprite.hx +++ b/src/flambe/display/Sprite.hx @@ -302,7 +302,7 @@ class Sprite extends Component if (_flags.contains(LOCAL_MATRIX_DIRTY)) { _flags = _flags.remove(LOCAL_MATRIX_DIRTY); - if(_flags.contains(ROTATION_DIRTY)) { + if(_flags.contains(ROTATION_DIRTY)) { _flags = _flags.remove(ROTATION_DIRTY); var rotation :Float = FMath.toRadians(this.rotation._); _sinCache = Math.sin(rotation); From e02dfec7d2c49a8f08db351ba2713eec9e9ce1e7 Mon Sep 17 00:00:00 2001 From: Kipp Ashford Date: Tue, 21 Oct 2014 11:29:54 -0700 Subject: [PATCH 5/6] if we aren't rotated, don't bother doing the math. --- src/flambe/swf/MovieSprite.hx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/flambe/swf/MovieSprite.hx b/src/flambe/swf/MovieSprite.hx index 2cd399c7..5dc69d97 100644 --- a/src/flambe/swf/MovieSprite.hx +++ b/src/flambe/swf/MovieSprite.hx @@ -321,8 +321,16 @@ private class LayerAnimator // From an identity matrix, append the translation, skew, and scale var matrix = sprite.getLocalMatrix(); - var sinX = Math.sin(skewX), cosX = Math.cos(skewX); - var sinY = Math.sin(skewY), cosY = Math.cos(skewY); + var sinX = 0.0, cosX = 1.0; + var sinY = 0.0, cosY = 1.0; + if (skewX != 0) { + sinX = Math.sin(skewX); + cosX = Math.cos(skewX); + } + if (skewY != 0) { + sinY = Math.sin(skewY); + cosY = Math.cos(skewY); + } matrix.set(cosY*scaleX, sinY*scaleX, -sinX*scaleY, cosX*scaleY, x, y); // Append the pivot From b4b8167d1857debb20d6097a84a8db0ab3157625 Mon Sep 17 00:00:00 2001 From: Kipp Ashford Date: Fri, 23 Jan 2015 10:34:45 -0800 Subject: [PATCH 6/6] Whoops, cleaned up an unused variable. --- src/flambe/display/Sprite.hx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/flambe/display/Sprite.hx b/src/flambe/display/Sprite.hx index 9df6989b..5d4728d0 100644 --- a/src/flambe/display/Sprite.hx +++ b/src/flambe/display/Sprite.hx @@ -750,7 +750,6 @@ class Sprite extends Component private var _parentViewMatrixUpdateCount :Int = 0; private var _sinCache :Float = 0; private var _cosCache :Float = 0; -#if debug private var _savedRotations :Int = 0; #end private var _pointerDown :Signal1; private var _pointerMove :Signal1; private var _pointerUp :Signal1;