I am trying to test this function, but it does not seem to work at all:
package
{
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
// import flash.filesystem.File;
import flash.geom.Rectangle;
import flash.system.Capabilities;
import starling.core.Starling;
import starling.events.Event;
import starling.textures.Texture;
import starling.utils.AssetManager;
import starling.utils.RectangleUtil;
import starling.utils.ScaleMode;
import flash.display.BitmapData;
import flash.display.Bitmap;
import starling.rendering.Painter;
import starling.display.Stage;
import starling.display.Image;
//import starling.utils.formatString;
[SWF(frameRate = "30", backgroundColor = "#000")]
public class PainterUnitTest extends Sprite
{
[Embed(source = "../assets/background.png")]
public static const bg_Cls: Class;
private var img: Image;
private var mStarling: Starling;
private var rootInstance: Root;
public function PainterUnitTest()
{
var stageWidth: int = 800;
var stageHeight: int = 600;
var viewPort: Rectangle = RectangleUtil.fit(new Rectangle(0, 0, stageWidth, stageHeight), new Rectangle(0, 0, stage.fullScreenWidth, stage.fullScreenHeight), ScaleMode.SHOW_ALL);
var scaleFactor: int = 1; //viewPort.width < 480 ? 1 : 2; // midway between 320 and 640
viewPort = new Rectangle(0, 0, 800, 600);
var assets: AssetManager = new AssetManager(scaleFactor);
assets.verbose = Capabilities.isDebugger;
mStarling = new Starling(Root, stage, viewPort);
mStarling.stage.stageWidth = stageWidth; // <\- same size on all devices!
mStarling.stage.stageHeight = stageHeight; // <- same size on all devices!
mStarling.simulateMultitouch = false;
mStarling.enableErrorChecking = Capabilities.isDebugger;
mStarling.addEventListener(starling.events.Event.ROOT_CREATED, onRootCreated);
mStarling.start();
function onRootCreated(event: Object, app: Root): void
{
rootInstance = app;
mStarling.removeEventListener(starling.events.Event.ROOT_CREATED, onRootCreated);
assets.loadQueue(onProgress)
}
function onProgress(ratio: Number): void
{
if (ratio == 1)
{
img = new Image(Texture.fromEmbeddedAsset(bg_Cls));
rootInstance.addChild(img);
var bd: BitmapData;
bd = rootInstance.drawToBitmapDataCutom(0, bd);
var photo: Image = new Image(Texture.fromBitmapData(bd, false, false, 1));
rootInstance.addChild(photo);
rootInstance.removeChild(img);
//rootInstance.doit();
}
}
}
}
}
Here is the function:
public function drawToBitmapDataCutom(screenOffset: Number, out: BitmapData = null): BitmapData
{
var stage: Stage = Starling.current.stage;
var stageWidth: Number = stage.stageWidth;
var stageHeight: Number = stage.stageHeight;
var scale: Number = Starling.contentScaleFactor;
var painter: Painter = Starling.painter;
var bounds: Rectangle = getBounds(this, new Rectangle(00, 0, 800, 600)); // this is Stage ? stage.getStageBounds(this, sHelperRect) : getBounds(_parent, sHelperRect);
if (out == null)
out = new BitmapData(800, 600); // Math.ceil(bounds.width * scale), Math.ceil(bounds.height * scale - screenOffset));
painter.clear();
painter.pushState();
painter.state.renderTarget = null;
painter.state.setModelviewMatricesToIdentity();
painter.setStateTo(transformationMatrix);
render(painter);
painter.finishMeshBatch();
painter.context.drawToBitmapData(out);
painter.popState();
return out;
}
I don't know why the "insert code" is not displaying correctly here. I am using the surrounding ` . But the last and first lines always go outside.