RenderTexture

The starling.textures.RenderTexture API, allows developers to create nondestructive drawing. As a Flash developer, think about the BitmapData API. This feature can be really useful when creating applications like drawing tools, where you need to draw continuously inside a texture and preserve previous drawings.

In the following code, we replicate the BitmapData.draw feature, on the GPU through Starling:

package
{
    import flash.display.Bitmap;
    import flash.geom.Point;

    import starling.display.Image;
    import starling.display.Sprite;
    import starling.events.Event;
    import starling.events.Touch;
    import starling.events.TouchEvent;
    import starling.events.TouchPhase;
    import starling.textures.RenderTexture;
    import starling.textures.Texture;

    public class Game extends Sprite
    {
        private var mRenderTexture:RenderTexture;
        private var mBrush:Image;

        [Embed(source = "/../media/textures/egg_closed.png")]
        private static const Egg:Class;

        public function Game()
        {
            addEventListener(Event.ADDED_TO_STAGE, onAdded);
        }

        private function onAdded (e:Event):void
        {
            // create a Bitmap object out of the embedded image
            var brush:Bitmap = new Egg();

            // create a Texture object to feed the Image object
            var texture:Texture = Texture.fromBitmap(brush);

            // create the texture to draw into the texture
            mBrush = new Image(texture);
            // set the registration point
            mBrush.pivotX = mBrush.width >> 1;
            mBrush.pivotY = mBrush.height >> 1;

            // scale it
            mBrush.scaleX = mBrush.scaleY = 0.5;

            // creates the canvas to draw into
            

Get Introducing Starling now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.