Skip to content

Commit

Permalink
avoiding some unnecessary object allocations, especially in 'VertexDa…
Browse files Browse the repository at this point in the history
…ta' class
  • Loading branch information
PrimaryFeather committed Dec 2, 2011
1 parent 4af5c0b commit f31575c
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 77 deletions.
31 changes: 16 additions & 15 deletions starling/src/starling/animation/Juggler.as
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ package starling.animation
*/
public class Juggler implements IAnimatable
{
private var mObjects:Vector.<Object>;
private var mObjects:Vector.<IAnimatable>;
private var mElapsedTime:Number;
private var mDisplayObject:DisplayObject;

/** Create an empty juggler. */
public function Juggler()
{
mElapsedTime = 0;
mObjects = new <Object>[];
mObjects = new <IAnimatable>[];
}

/** Adds an object to the juggler. */
Expand All @@ -67,11 +67,11 @@ package starling.animation
/** Removes an object from the juggler. */
public function remove(object:IAnimatable):void
{
mObjects = mObjects.filter(
function(currentObject:Object, index:int, vector:Vector.<Object>):Boolean
{
return object != currentObject;
});
var numObjects:int = mObjects.length;

for (var i:int=numObjects-1; i>=0; --i)
if (mObjects[i] == object)
mObjects.splice(i, 1);
}

/** Removes all tweens with a certain target. */
Expand All @@ -91,7 +91,7 @@ package starling.animation
/** Removes all objects at once. */
public function purge():void
{
mObjects = new <Object>[];
mObjects = new <IAnimatable>[];
}

/** Delays the execution of a function until a certain time has passed. Creates an
Expand All @@ -106,24 +106,25 @@ package starling.animation
return delayedCall;
}

/** Advanced all objects by a certain time (in seconds). Objects with a positive
/** Advances all objects by a certain time (in seconds). Objects with a positive
* 'isComplete'-property will be removed. */
public function advanceTime(time:Number):void
{
mElapsedTime += time;
var objectCopy:Vector.<Object> = mObjects.concat();
if (mObjects.length == 0) return;

var numObjects:int = mObjects.length;
var objectCopy:Vector.<IAnimatable> = mObjects.concat();

// since 'advanceTime' could modify the juggler (through a callback), we split
// the logic in two loops.

for each (var currentObject:IAnimatable in objectCopy)
currentObject.advanceTime(time);

mObjects = mObjects.filter(
function(object:IAnimatable, index:int, vector:Vector.<Object>):Boolean
{
return !object.isComplete;
});
for (var i:int=numObjects-1; i>=0; --i)
if (mObjects[i].isComplete)
mObjects.splice(i, 1);
}

/** Always returns 'false'. */
Expand Down
10 changes: 5 additions & 5 deletions starling/src/starling/core/QuadBatch.as
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ package starling.core
quad.copyVertexDataTo(mVertexData, vertexID);
alpha *= quad.alpha;

for (var i:int = 0; i<4; ++i)
{
mVertexData.scaleAlpha(vertexID+i, alpha);
mVertexData.transformVertex(vertexID+i, modelViewMatrix);
}
if (alpha != 1.0)
for (var i:int = 0; i<4; ++i)
mVertexData.scaleAlpha(vertexID+i, alpha);

mVertexData.transformQuad(vertexID, modelViewMatrix);

++mNumQuads;
}
Expand Down
4 changes: 2 additions & 2 deletions starling/src/starling/core/RenderSupport.as
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ package starling.core

/** Adds a quad to the current batch of unrendered quads. If there is a state change,
* all previous quads are rendered at once, and the batch is reset. */
public function renderQuad(quad:Quad, alpha:Number,
texture:Texture=null, smoothing:String=null):void
public function addToQuadBatch(quad:Quad, alpha:Number,
texture:Texture=null, smoothing:String=null):void
{
if (mQuadBatch.isStateChange(quad, texture, smoothing))
finishQuadBatch();
Expand Down
3 changes: 0 additions & 3 deletions starling/src/starling/display/DisplayObjectContainer.as
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,6 @@ package starling.display
{
if (child.alpha != 0.0 && child.visible)
{
if (!(child is Quad))
support.finishQuadBatch();

support.pushMatrix();
support.transformMatrix(child);
child.render(support, alpha);
Expand Down
9 changes: 5 additions & 4 deletions starling/src/starling/display/Image.as
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ package starling.display
updateVertexDataCache();
}

/** Gets the texture coordinates of a vertex. Coordinates are in the range [0, 1]. */
public function getTexCoords(vertexID:int):Point
/** Gets the texture coordinates of a vertex. Coordinates are in the range [0, 1].
* To avoid allocations, the output is saved into a parameter. */
public function getTexCoords(vertexID:int, coords:Point):void
{
return mVertexData.getTexCoords(vertexID);
mVertexData.getTexCoords(vertexID, coords);
}

/** Copies the raw vertex data to a VertexData instance.
Expand Down Expand Up @@ -148,7 +149,7 @@ package starling.display
/** @inheritDoc */
public override function render(support:RenderSupport, alpha:Number):void
{
support.renderQuad(this, alpha, mTexture, mSmoothing);
support.addToQuadBatch(this, alpha, mTexture, mSmoothing);
}
}
}
22 changes: 12 additions & 10 deletions starling/src/starling/display/Quad.as
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ package starling.display
/** The raw vertex data of the quad. */
protected var mVertexData:VertexData;

/** Helper object. */
private static var sPosition:Vector3D = new Vector3D();

/** Creates a quad with a certain size and color. */
public function Quad(width:Number, height:Number, color:uint=0xffffff)
{
Expand All @@ -55,18 +58,17 @@ package starling.display
{
var minX:Number = Number.MAX_VALUE, maxX:Number = -Number.MAX_VALUE;
var minY:Number = Number.MAX_VALUE, maxY:Number = -Number.MAX_VALUE;
var position:Vector3D;
var i:int;

if (targetSpace == this) // optimization
{
for (i=0; i<4; ++i)
{
position = mVertexData.getPosition(i);
minX = Math.min(minX, position.x);
maxX = Math.max(maxX, position.x);
minY = Math.min(minY, position.y);
maxY = Math.max(maxY, position.y);
mVertexData.getPosition(i, sPosition);
minX = Math.min(minX, sPosition.x);
maxX = Math.max(maxX, sPosition.x);
minY = Math.min(minY, sPosition.y);
maxY = Math.max(maxY, sPosition.y);
}
}
else
Expand All @@ -76,9 +78,9 @@ package starling.display

for (i=0; i<4; ++i)
{
position = mVertexData.getPosition(i);
point.x = position.x;
point.y = position.y;
mVertexData.getPosition(i, sPosition);
point.x = sPosition.x;
point.y = sPosition.y;
var transformedPoint:Point = transformationMatrix.transformPoint(point);
minX = Math.min(minX, transformedPoint.x);
maxX = Math.max(maxX, transformedPoint.x);
Expand Down Expand Up @@ -136,7 +138,7 @@ package starling.display
/** @inheritDoc */
public override function render(support:RenderSupport, alpha:Number):void
{
support.renderQuad(this, alpha);
support.addToQuadBatch(this, alpha);
}
}
}
4 changes: 2 additions & 2 deletions starling/src/starling/display/Sprite.as
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ package starling.display
/** @inheritDoc */
public override function render(support:RenderSupport, alpha:Number):void
{
support.finishQuadBatch();

if (mFlattenedContents)
{
support.finishQuadBatch();

for each (var quadBatch:QuadBatch in mFlattenedContents)
quadBatch.render(support.mvpMatrix, this.alpha * alpha);
}
Expand Down
9 changes: 6 additions & 3 deletions starling/src/starling/textures/SubTexture.as
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ package starling.textures
private var mClipping:Rectangle;
private var mRootClipping:Rectangle;

/** Helper object. */
private static var sTexCoords:Point = new Point();

/** Creates a new subtexture containing the specified region (in pixels) of a parent
* texture. */
public function SubTexture(parentTexture:Texture, region:Rectangle)
Expand Down Expand Up @@ -70,9 +73,9 @@ package starling.textures

for (var i:int=vertexID; i<endIndex; ++i)
{
var texCoords:Point = vertexData.getTexCoords(i);
vertexData.setTexCoords(i, clipX + texCoords.x * clipWidth,
clipY + texCoords.y * clipHeight);
vertexData.getTexCoords(i, sTexCoords);
vertexData.setTexCoords(i, clipX + sTexCoords.x * clipWidth,
clipY + sTexCoords.y * clipHeight);
}
}

Expand Down
Loading

0 comments on commit f31575c

Please sign in to comment.