Skip to content

Commit

Permalink
Cache sine and cosine calculations for a tiny performance boost.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kipp Ashford committed Oct 7, 2014
1 parent 268ae82 commit 3084733
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/flambe/display/Sprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,17 @@ 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 (_,_) {
_flags = _flags.add(LOCAL_MATRIX_DIRTY | VIEW_MATRIX_DIRTY);
};
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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<PointerEvent>;
private var _pointerMove :Signal1<PointerEvent>;
private var _pointerUp :Signal1<PointerEvent>;
Expand Down

0 comments on commit 3084733

Please sign in to comment.