Skip to content

Commit

Permalink
first attempt to add pool (only on SpatialInfo for now), add some des…
Browse files Browse the repository at this point in the history
…troy functions
  • Loading branch information
loudoweb committed Aug 12, 2015
1 parent da559c4 commit 5443ab4
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 31 deletions.
6 changes: 5 additions & 1 deletion spriter/definitions/ScmlObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class ScmlObject implements IScml
return newFolders;
}

public function copy():ScmlObject
public function copy(?id:String):ScmlObject
{
var newSCML:ScmlObject = new ScmlObject();
newSCML.folders = copyFolders();
Expand All @@ -238,6 +238,7 @@ class ScmlObject implements IScml
newSCML.currentEntity = Std.string(currentEntity);
newSCML.currentAnimation = Std.string(currentAnimation);
newSCML.currentTime = 0;
newSCML.spriterName = id;
return newSCML;
}

Expand All @@ -248,5 +249,8 @@ class ScmlObject implements IScml
entities = null;
entitiesName = null;
tags = null;
endAnimCallback = null;
tagCallback = null;
varChangeCallback = null;
}
}
70 changes: 65 additions & 5 deletions spriter/definitions/SpatialInfo.hx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package spriter.definitions;
import spriter.interfaces.ISpriterPooled;
import spriter.util.SpriterPool;
import spriter.util.SpriterUtil;

/**
* ...
* @author Loudo
*/
class SpatialInfo
class SpatialInfo implements ISpriterPooled
{
public var x:Float=0;
public var y:Float=0;
Expand All @@ -16,7 +18,25 @@ class SpatialInfo
* Alpha
*/
public var a:Float=1;
public var spin:Int=1;
public var spin:Int = 1;

private static var _pool = new SpriterPool<SpatialInfo>(SpatialInfo);
private var _inPool:Bool = false;

/**
* Recycle or create a new SpatialInfo.
* Be sure to put() them back into the pool after you're done with them!
*
* @param X The X-coordinate of the point in space.
* @param Y The Y-coordinate of the point in space.
* @return This point.
*/
public static inline function get(x:Float = 0, y:Float = 0, angle:Float = 0, scaleX:Float = 1, scaleY:Float = 1, a:Float = 1, spin:Int = 1):SpatialInfo
{
var pooledInfo = _pool.get().init(x, y, angle, scaleX, scaleY, a, spin);
pooledInfo._inPool = false;
return pooledInfo;
}

public function new(x:Float = 0, y:Float = 0, angle:Float = 0, scaleX:Float = 1, scaleY:Float = 1, a:Float = 1, spin:Int = 1)
{
Expand All @@ -29,6 +49,32 @@ class SpatialInfo
this.spin = spin;
}

public function init(x:Float = 0, y:Float = 0, angle:Float = 0, scaleX:Float = 1, scaleY:Float = 1, a:Float = 1, spin:Int = 1):SpatialInfo
{
this.x = x;
this.y = y;
this.angle = angle;
this.scaleX = scaleX;
this.scaleY = scaleY;
this.a = a;
this.spin = spin;
return this;
}

public function setPos(x:Float = 0, y:Float = 0):SpatialInfo
{
this.x = x;
this.y = y;
return this;
}

public function setScale(scale:Float):SpatialInfo
{
this.scaleX = scale;
this.scaleY = scale;
return this;
}

public function unmapFromParent(parentInfo:SpatialInfo):SpatialInfo
{
var unmapped_x : Float;
Expand All @@ -55,13 +101,12 @@ class SpatialInfo
unmapped_y = parentInfo.y;
}

return new SpatialInfo(unmapped_x, unmapped_y, unmapped_angle, unmapped_scaleX, unmapped_scaleY, unmapped_alpha, spin);
return SpatialInfo.get(unmapped_x, unmapped_y, unmapped_angle, unmapped_scaleX, unmapped_scaleY, unmapped_alpha, spin);
}

public function copy():SpatialInfo
{
var c:SpatialInfo = new SpatialInfo(x, y, angle, scaleX, scaleY, a, spin);
return c;
return SpatialInfo.get(x, y, angle, scaleX, scaleY, a, spin);
}

/*public function linear(infoA:SpatialInfo, infoB:SpatialInfo, spin:Int, t:Float):SpatialInfo
Expand All @@ -74,5 +119,20 @@ class SpatialInfo
resultInfo.scaleY = linear(infoA.scaleY,infoB.scaleY,t);
resultInfo.a = linear(infoA.a,infoB.a,t);
}*/
/**
* Add this SpatialInfo to the recycling pool.
*/
public function put():Void
{
if (!_inPool)
{
_inPool = true;
_pool.putUnsafe(this);
}
}
public function destroy():Void
{

}

}
9 changes: 7 additions & 2 deletions spriter/definitions/SpatialTimelineKey.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SpatialTimelineKey extends TimelineKey
var scale_y = fast.has.scale_y ? Std.parseFloat(fast.att.scale_y) : 1;
var alpha = fast.has.a ? Std.parseFloat(fast.att.a) : 1;

info = new SpatialInfo(x, y, angle, scale_x, scale_y, alpha, spin);
info = new SpatialInfo(x, y, angle, scale_x, scale_y, alpha, spin);//we don't get from pool here because a macro use this constructor :/
}
}

Expand All @@ -43,7 +43,7 @@ class SpatialTimelineKey extends TimelineKey

override public function clone (clone:TimelineKey):TimelineKey
{
super.clone(clone);
super.clone(clone);//TODO instead of cloning we should only manipulate SpatialInfo from linearSpatialInfo();

var c:SpatialTimelineKey = cast clone;
c.info = info.copy();
Expand Down Expand Up @@ -72,5 +72,10 @@ class SpatialTimelineKey extends TimelineKey
{
return new PivotInfo(pivotX,pivotY);
}
public function destroy():Void
{
info.put();//add too pool
info = null;
}

}
12 changes: 11 additions & 1 deletion spriter/definitions/SpriterAnimation.hx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class SpriterAnimation
}

//var objectKeys:Array<TimelineKey>;
points = [];
points = [];//TODO back to pool?
boxes = [];
len = mainKey.objectRefs.length;
for(o in 0...len)
Expand Down Expand Up @@ -244,6 +244,16 @@ class SpriterAnimation
}
}
}
//clean nup
spatialInfo.put();//back to pool
spatialInfo = null;
len = transformedBoneKeys.length;
for(p in 0...len)
{
spatialInfo = transformedBoneKeys[p];
spatialInfo.put();//back to pool
spatialInfo = null;
}
}

public function mainlineKeyFromTime(time:Int):MainlineKey
Expand Down
12 changes: 12 additions & 0 deletions spriter/engine/Spriter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,24 @@ class Spriter
{
timeMS = 0;
}
/**
* Set positions of the Spriter
* @param x
* @param y (spriter uses inverted y, so it will automatically inverted in this function)
*/
public function set(x:Float, y:Float):Void
{
//-y because use inverted y coordinates
info.setPos(x, -y);
}

public function destroy():Void
{
scml.destroy();
info.put();
info = null;
//don't destroy library here since library is shared between all Spriter in the engine
library = null;
}

}
15 changes: 3 additions & 12 deletions spriter/engine/SpriterEngine.hx
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,17 @@ class SpriterEngine
* @param x
* @param y
* @param ?index if null same result as addChild, else same result as addChildAt(index). Spriters at and after the replaced index move up. You can use index out of range but negative means 0.
* @param copySCML if false, you can use a same SCML for multiple Spriter entity, allow you to have
* @param autoRemoval if true, the Spriter will be removed after the animation is ended
* @return the Spriter created
*/
public function addEntity(id:String, x:Float = 0, y:Float = 0, ?index:Null<Int>, autoRemoval:Bool = false, copySCML:Bool = true):Spriter
public function addEntity(id:String, x:Float = 0, y:Float = 0, ?index:Null<Int>, autoRemoval:Bool = false):Spriter
{

//create spatial info for the current Spriter
var info:SpatialInfo = new SpatialInfo(x, -y);//-y because use inverted y coordinates
var info:SpatialInfo = SpatialInfo.get(x, -y);//-y because use inverted y coordinates

//select scmlObject
var currentSCML:ScmlObject;
if (copySCML) {
currentSCML = scml.copy();
currentSCML.spriterName = id;
}else {
currentSCML = scml;
}
//create the Spriter
var spriter:Spriter = new Spriter(id, currentSCML, _lib, info);
var spriter:Spriter = new Spriter(id, scml.copy(id), _lib, info);
if (autoRemoval) {
spriter.playAnim(removeSpriterEntity, true);
}
Expand Down
10 changes: 10 additions & 0 deletions spriter/interfaces/ISpriterDestroyable.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package spriter.interfaces;

/**
* ...
* @author Loudo
*/
interface ISpriterDestroyable
{
public function destroy():Void;
}
12 changes: 12 additions & 0 deletions spriter/interfaces/ISpriterPooled.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package spriter.interfaces;

import spriter.interfaces.ISpriterDestroyable;

/**
* @flixel
*/
interface ISpriterPooled extends ISpriterDestroyable
{
public function put():Void;
private var _inPool:Bool;
}
2 changes: 1 addition & 1 deletion spriter/library/AbstractLibrary.hx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AbstractLibrary

var x2 = (preX - pivotX) * c - (preY - pivotY) * s + pivotX;
var y2 = (preX - pivotX) * s + (preY - pivotY) * c + pivotY;
return new SpatialInfo(x2, -y2, degreesUnder360, info.scaleX, info.scaleY, info.a, info.spin);//TODO pool?
return SpatialInfo.get(x2, -y2, degreesUnder360, info.scaleX, info.scaleY, info.a, info.spin);//TODO pool?
}

public function computeRectCoordinates(info:SpatialInfo, pivots:PivotInfo, width:Float, height:Float):Quadrilateral
Expand Down
18 changes: 9 additions & 9 deletions spriter/library/TilelayerLibrary.hx
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ class TilelayerLibrary extends AbstractLibrary
var sprite:TileSprite = getFile(name);
_layer.addChild(sprite);

var spatialResult:SpatialInfo = compute(info, pivots, sprite.width, sprite.height);
_currentSpatialResult = compute(info, pivots, sprite.width, sprite.height);


//sprite.offset = getPivotsRelativeToCenter(info, pivots, sprite.width, sprite.height);//TOFIX tilelayer seems buggy
sprite.x = spatialResult.x;
sprite.y = spatialResult.y;
sprite.rotation = SpriterUtil.toRadians(SpriterUtil.fixRotation(spatialResult.angle));
sprite.scaleX = spatialResult.scaleX;
sprite.scaleY = spatialResult.scaleY;
sprite.alpha = spatialResult.a;
sprite.x = _currentSpatialResult.x;
sprite.y = _currentSpatialResult.y;
sprite.rotation = SpriterUtil.toRadians(SpriterUtil.fixRotation(_currentSpatialResult.angle));
sprite.scaleX = _currentSpatialResult.scaleX;
sprite.scaleY = _currentSpatialResult.scaleY;
sprite.alpha = _currentSpatialResult.a;

sprite.visible = true;
_currentSpatialResult.put();//back to pool
}

private function getPivotsRelativeToCenter(info:SpatialInfo, pivots:PivotInfo, width:Float, height:Float):Point
Expand All @@ -100,7 +100,7 @@ class TilelayerLibrary extends AbstractLibrary
var x2 = (preX - pivotX) * c - (preY - pivotY) * s + pivotX;
var y2 = (preX - pivotX) * s + (preY - pivotY) * c + pivotY;

return new SpatialInfo(x2, -y2, degreesUnder360, info.scaleX, info.scaleY, info.a, info.spin);
return SpatialInfo.get(x2, -y2, degreesUnder360, info.scaleX, info.scaleY, info.a, info.spin);
}

override public function render():Void
Expand Down

0 comments on commit 5443ab4

Please sign in to comment.