Skip to content

Instantly share code, notes, and snippets.

@pavels
Created December 5, 2011 18:09
Show Gist options
  • Save pavels/1434610 to your computer and use it in GitHub Desktop.
Save pavels/1434610 to your computer and use it in GitHub Desktop.
Starling flatten fix and repeat fix
diff -ur starej//starling/core/QuadBatch.as novej//starling/core/QuadBatch.as
--- starej//starling/core/QuadBatch.as 2011-12-02 20:56:22.000000000 +0100
+++ novej//starling/core/QuadBatch.as 2011-12-05 18:54:51.000000000 +0100
@@ -9,7 +9,7 @@
import flash.display3D.VertexBuffer3D;
import flash.geom.Matrix3D;
import flash.utils.getQualifiedClassName;
-
+ import de.polygonal.ds.HashMap;
import starling.display.DisplayObject;
import starling.display.DisplayObjectContainer;
import starling.display.Image;
@@ -175,36 +181,35 @@
mCurrentSmoothing != smoothing;
else return true;
}
+
+ public static function getStoredQuadBatch(map:HashMap, texture:Texture, smoothing:String):QuadBatch{
+ var hashkey:String;
+ hashkey = texture.ID.toString() + ";" + smoothing;
+ return QuadBatch(map.get(hashkey));
+ }
+
+ public static function setStoredQuadBatch(map:HashMap, buffer:QuadBatch,texture:Texture, smoothing:String){
+ var hashkey:String;
+ hashkey = texture.ID.toString() + ";" + smoothing;
+ map.set(hashkey,buffer);
+ }
// compilation (for flattened sprites)
public static function compile(container:DisplayObjectContainer,
- quadBatches:Vector.<QuadBatch>):void
+ quadBatches:HashMap):void
{
- compileObject(container, quadBatches, -1, new Matrix3D());
+ compileObject(container, quadBatches, new Matrix3D());
}
private static function compileObject(object:DisplayObject,
- quadBatches:Vector.<QuadBatch>,
- quadBatchID:int,
+ quadBatches:HashMap,
transformationMatrix:Matrix3D,
- alpha:Number=1.0):int
+ alpha:Number=1.0):void
{
var i:int;
var isRootObject:Boolean = false
- if (quadBatchID == -1)
- {
- isRootObject = true;
- quadBatchID = 0;
- if (quadBatches.length == 0) quadBatches.push(new QuadBatch());
- else quadBatches[0].reset();
- }
- else if (object.alpha == 0.0 || !object.visible)
- {
- return quadBatchID; // ignore transparent objects, except root
- }
-
if (object is DisplayObjectContainer)
{
var container:DisplayObjectContainer = object as DisplayObjectContainer;
@@ -216,8 +221,7 @@
var child:DisplayObject = container.getChildAt(i);
childMatrix.copyFrom(transformationMatrix);
RenderSupport.transformMatrixForObject(childMatrix, child);
- quadBatchID = compileObject(child, quadBatches, quadBatchID,
- childMatrix, alpha * child.alpha);
+ compileObject(child, quadBatches, childMatrix, alpha * child.alpha);
}
}
else if (object is Quad)
@@ -226,15 +230,12 @@
var image:Image = quad as Image;
var texture:Texture = image ? image.texture : null;
var smoothing:String = image ? image.smoothing : null;
- var quadBatch:QuadBatch = quadBatches[quadBatchID];
+ var quadBatch:QuadBatch = getStoredQuadBatch(quadBatches,texture,smoothing);
- if (quadBatch.isStateChange(quad, texture, smoothing))
+ if (quadBatch == null)
{
- quadBatch.syncBuffers();
- quadBatchID++;
- if (quadBatches.length <= quadBatchID)
- quadBatches.push(new QuadBatch());
- quadBatch = quadBatches[quadBatchID];
+ quadBatch = new QuadBatch();
+ setStoredQuadBatch(quadBatches,quadBatch,texture,smoothing);
quadBatch.reset();
}
@@ -245,18 +246,6 @@
throw new Error("Unsupported display object: " + getQualifiedClassName(object));
}
- if (isRootObject)
- {
- quadBatches[quadBatchID].syncBuffers();
-
- for (i=quadBatches.length-1; i>quadBatchID; --i)
- {
- quadBatches[i].dispose();
- delete quadBatches[i];
- }
- }
-
- return quadBatchID;
}
// program management
diff -ur starej//starling/display/DisplayObject.as novej//starling/display/DisplayObject.as
--- starej//starling/display/DisplayObject.as 2011-12-02 18:18:40.000000000 +0100
+++ novej//starling/display/DisplayObject.as 2011-12-05 16:56:01.000000000 +0100
@@ -306,7 +306,7 @@
}
/** @private */
- internal function dispatchEventOnChildren(event:Event):void
+ public function dispatchEventOnChildren(event:Event):void
{
dispatchEvent(event);
}
diff -ur starej//starling/display/DisplayObjectContainer.as novej//starling/display/DisplayObjectContainer.as
--- starej//starling/display/DisplayObjectContainer.as 2011-12-02 20:38:46.000000000 +0100
+++ novej//starling/display/DisplayObjectContainer.as 2011-12-05 13:10:16.000000000 +0100
@@ -297,7 +297,7 @@
}
/** @private */
- internal override function dispatchEventOnChildren(event:Event):void
+ public override function dispatchEventOnChildren(event:Event):void
{
// the event listeners might modify the display tree, which could make the loop crash.
// thus, we collect them in a list and iterate over that list instead.
Only in novej//starling/display: FastSprite.as
--- starej//starling/display/Image.as 2011-12-02 21:18:22.000000000 +0100
+++ novej//starling/display/Image.as 2011-12-05 18:22:50.000000000 +0100
@@ -52,6 +54,9 @@
var height:Number = frame ? frame.height : texture.height;
super(width, height);
+
+ var texttureCoordX:Number = texture.repeat ? (Number(width)) : 1.0;
+ var texttureCoordY:Number = texture.repeat ? (Number(height)) : 1.0;
mSmoothing = TextureSmoothing.BILINEAR;
mTexture = texture;
@@ -59,9 +64,9 @@
mVertexData.setPremultipliedAlpha(texture.premultipliedAlpha);
mVertexData.setTexCoords(0, 0.0, 0.0);
- mVertexData.setTexCoords(1, 1.0, 0.0);
- mVertexData.setTexCoords(2, 0.0, 1.0);
- mVertexData.setTexCoords(3, 1.0, 1.0);
+ mVertexData.setTexCoords(1, texttureCoordY, 0.0);
+ mVertexData.setTexCoords(2, 0.0, texttureCoordX);
+ mVertexData.setTexCoords(3, texttureCoordY, texttureCoordX);
updateVertexDataCache();
}
Only in novej//starling/display: QuadGroup.as
diff -ur starej//starling/display/Sprite.as novej//starling/display/Sprite.as
--- starej//starling/display/Sprite.as 2011-12-02 20:44:56.000000000 +0100
+++ novej//starling/display/Sprite.as 2011-12-05 15:25:51.000000000 +0100
@@ -13,6 +13,7 @@
import starling.core.QuadBatch;
import starling.core.RenderSupport;
import starling.events.Event;
+ import de.polygonal.ds.HashMap;
/** Dispatched on all children when the object is flattened. */
[Event(name="flatten", type="starling.events.Event")]
@@ -57,10 +60,15 @@
* either call <code>flatten</code> again, or <code>unflatten</code> the sprite. */
public function flatten():void
{
+ unflatten();
dispatchEventOnChildren(new Event(Event.FLATTEN));
- if (mFlattenedContents == null) mFlattenedContents = new <QuadBatch>[];
- QuadBatch.compile(this, mFlattenedContents);
+ mFlattenedContents = new <QuadBatch>[];
+ var tempMap:HashMap = new HashMap();
+ QuadBatch.compile(this, tempMap);
+ for each(var qBatch:QuadBatch in tempMap.valuesToArray()){
+ mFlattenedContents.push(qBatch);
+ }
}
/** Removes the rendering optimizations that were created when flattening the sprite.
diff -ur starej//starling/textures/Texture.as novej//starling/textures/Texture.as
--- starej//starling/textures/Texture.as 2011-11-28 21:56:08.000000000 +0100
+++ novej//starling/textures/Texture.as 2011-12-05 13:58:49.000000000 +0100
@@ -84,6 +84,10 @@
{
private var mFrame:Rectangle;
private var mRepeat:Boolean;
+
+ private static var mCounter:int = 0;
+
+ private var mID:int;
/** @private */
public function Texture()
@@ -92,6 +96,8 @@
throw new AbstractClassError();
mRepeat = false;
+ mCounter++;
+ mID = mCounter;
}
/** Disposes the underlying texture data. */
@@ -254,5 +260,8 @@
/** Indicates if the alpha values are premultiplied into the RGB values. */
public function get premultipliedAlpha():Boolean { return false; }
+
+ /** Unique identifier of texture. */
+ public function get ID():int {return mID; }
}
}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment