A Sprite is the most lightweight, non-abstract container class. Use it as a simple means of grouping objects together in one coordinate system. Sprite sprite = new Sprite(); // create children Image venus = TextureLoader.LoadLocalImage("../venus.png"); Image mars = TextureLoader.LoadLocalImage("mars.png"); // move children to some relative positions venus.X = 50; mars.X = -20; // add children to the sprite sprite.AddChild(venus); sprite.AddChild(mars); // calculate total width of all children float totalWidth = sprite.Width; // rotate the whole group sprite.Rotation = Math.PI; **Flattened Sprites** The 'Flatten' method allows you to optimize the rendering of static parts of your display list. It analyzes the tree of children attached to the sprite and optimizes the rendering calls in a way that makes rendering extremely fast. The speed-up comes at a price, though: you will no longer see any changes in the properties of the children (position, rotation, alpha, etc). To update the object after changes have happened, simply call 'flatten' again, or 'unflatten' the object.
Inheritance: DisplayObjectContainer
        public Benchmark()
        {
            TextureLoader starRes = new TextureLoader();
            starRes.LoadLocalImage("benchmark_object.png");
            textures = new Texture[] { starRes.Texture };

            // the container will hold all test objects
            _container = new Sprite();
            AddChild(_container);

            EnterFrame += EnterFrameHandler;
            AddedToStage += AddedToStageHandler;
        }
        public Benchmark()
        {
            GLTexture star = SimpleTextureLoader.LoadLocalImage("../../bigstar.png");
            GLTexture bird = SimpleTextureLoader.LoadLocalImage("../../benchmark_object.png");
            textures = new Texture[] { bird, star };

            // the container will hold all test objects
            _container = new Sprite();
            AddChild(_container);

            EnterFrame += EnterFrameHandler;
            AddedToStage += AddedToStageHandler;
        }
        public void TestAddingToStageOrder()
        {
            Sprite sp = new Sprite();
            Sprite sp2 = new Sprite();
            sp.AddChild(sp2);

            sp2.AddedToStage += (target, currentTarget) => {
                sp.AddChild(new Sprite());
            };

            testRoot.AddChild(sp);

            Assert.AreEqual(2, sp.NumChildren);
        }
        public Benchmark()
        {
            GLTexture star = SimpleTextureLoader.LoadAndroidResource(SparrowSharp.Samples.Android.Resource.Drawable.star);
            GLTexture bird = SimpleTextureLoader.LoadAndroidResource(SparrowSharp.Samples.Android.Resource.Drawable.benchmark_object);
            GLTexture bigstar = SimpleTextureLoader.LoadAndroidResource(SparrowSharp.Samples.Android.Resource.Drawable.bigstar);
            textures = new Texture[] { bird, bigstar, star };


            // the container will hold all test objects
            _container = new Sprite();
            AddChild(_container);

            EnterFrame += EnterFrameHandler;
            AddedToStage += AddedToStageHandler;
        }
        public void TestTransformationMatrix()
        {
            Sprite sprite = new Sprite();
            sprite.X = 50;
            sprite.Y = 100;
            sprite.Rotation = (float)(Math.PI / 4);
            sprite.ScaleX = 0.5f;
            sprite.ScaleY = 1.5f;

            Matrix matrix = Matrix.Create();
            matrix.Scale(sprite.ScaleX, sprite.ScaleY);
            matrix.Rotate(sprite.Rotation);
            matrix.Translate(sprite.X, sprite.Y);

            Assert.IsTrue(sprite.TransformationMatrix.IsEqual(matrix));
        }
        public void TestRoot()
        {
            SparrowSharpApp.Start(12, 12, typeof(Sprite));

            Sprite root = new Sprite();
            Sprite child = new Sprite();
            Sprite grandChild = new Sprite();
            root.AddChild(child);
            child.AddChild(grandChild);

            Assert.AreEqual(root, grandChild.Root, "Wrong root " + grandChild.Root);

            SparrowSharpApp.Stage.AddChild(root);

            Assert.AreEqual(SparrowSharpApp.Stage, grandChild.Root, "Wrong root " + grandChild.Root);

            SparrowSharpApp.Stage.RemoveAllChildren();
            SparrowSharpApp.Destroy();
        }
        public void TestAddingToStageCallOrder()
        {
            string order = "";
            Sprite sp = new Sprite();
            Sprite sp2 = new Sprite();
            sp2.AddedToStage += (target, currentTarget) => {
                order = order + "2";
                Sprite sp3 = new Sprite();
                sp3.AddedToStage += (target2, currentTarget2) => {
                    order = order + "3";
                };
                sp.AddChild(sp3);
            };
            sp.AddChild(sp2);

            order = order + "1";
            testRoot.AddChild(sp);
            order = order + "4";

            Assert.AreEqual(2, sp.NumChildren);
            Assert.AreEqual("1234", order);
        }
        private void AddTestObjects(int numObjects)
        {
            int border = 15;

            Random r = new Random();
            for (int i = 0; i < numObjects; ++i)
            {   
                Image egg = new Image(textures[0]);
                if (i < 5)
                {
                    ColorMatrix cm = new ColorMatrix();
                    cm.AdjustSaturation(-0.8f);
                    ColorMatrixFilter fi = new ColorMatrixFilter (cm);
                    //EmptyFilter fi = new EmptyFilter();
                    //BlurFilter fi = new BlurFilter(4, 1.1f);
                    egg.Filter = fi;
                    //egg.Filter.Cache();
                }
                //MovieClip egg = new MovieClip (textures, 3);
                //SP.DefaultJuggler.Add (egg);
                egg.X = r.Next(border, (int)Stage.Width - border);
                egg.Y = r.Next(border, (int)Stage.Height - border);
                egg.Rotation = (float)(r.Next(0, 100) / 100.0f * Math.PI);
                _container.AddChild(egg);
            }

            Sprite sp = new Sprite();
            sp.X = sp.Y = 250;
            _container.AddChild(sp);

            Image test = new Image(textures[1]);
            test.PivotX = test.PivotY = test.Width / 2;
            sp.AddChild(test);

            Image test1 = new Image(textures[1]);
            sp.AddChild(test1);
            test1.X = test1.Y = 60;
        }
        public void TestTransformationMatrixToSpace()
        {
            Sprite sprite = new Sprite();
            Sprite child = new Sprite();

            child.X = 30;
            child.Y = 20;
            child.ScaleX = 1.2f;
            child.ScaleY = 1.5f;
            child.Rotation = (float)(Math.PI / 4.0f);    
            sprite.AddChild(child);

            Matrix matrix = sprite.TransformationMatrixToSpace(child);
            Matrix expectedMatrix = child.TransformationMatrix;
            expectedMatrix.Invert();

            Assert.IsTrue(matrix.IsEqual(expectedMatrix));

            matrix = child.TransformationMatrixToSpace(sprite);

            Assert.IsTrue(child.TransformationMatrix.IsEqual(matrix));

            // more is tested indirectly via 'testBoundsInSpace' in DisplayObjectContainerTest
        }
 protected void SetUp()
 {
     SparrowSharpApp.Start(12, 12, typeof(Sprite));
     testRoot = new Sprite();
     SparrowSharpApp.Stage.AddChild(testRoot);
 }
        public void TestSetTransformationMatrix()
        {
            const float x = 50;
            const float y = 100;
            const float scaleX = 0.5f;
            const float scaleY = 1.5f;
            const float rotation = (float)(Math.PI / 4.0f);

            Matrix matrix = Matrix.Create();
            matrix.Scale(scaleX, scaleY);
            matrix.Rotate(rotation);
            matrix.Translate(x, y);

            Sprite sprite = new Sprite();
            sprite.TransformationMatrix = matrix;

            AssertAreEqualWithSmallError(x, sprite.X);
            AssertAreEqualWithSmallError(y, sprite.Y);
            AssertAreEqualWithSmallError(scaleX, sprite.ScaleX);
            AssertAreEqualWithSmallError(scaleY, sprite.ScaleY);
            AssertAreEqualWithSmallError(rotation, sprite.Rotation);
        }
        public void TestLocalToGlobalWithPivot()
        {
            Sprite sprite = new Sprite();
            Quad quad = new Quad(40, 30);
            quad.X = 10;
            quad.Y = 20;
            quad.PivotX = quad.Width;
            quad.PivotY = quad.Height;
            sprite.AddChild(quad);

            Point point = Point.Create(0, 0);
            Point globalPoint = quad.LocalToGlobal(point);
            AssertAreEqualWithSmallError(-30.0f, globalPoint.X, "wrong global point with pivot");
            AssertAreEqualWithSmallError(-10.0f, globalPoint.Y, "wrong global point with pivot");
        }
        public void TestLocalToGlobal()
        {
            Sprite root = new Sprite();
            Sprite sprite = new Sprite();
            sprite.X = 10;
            sprite.Y = 20;
            root.AddChild(sprite);
            Sprite sprite2 = new Sprite();
            sprite2.X = 150;
            sprite2.Y = 200;    
            sprite.AddChild(sprite2);

            Point localPoint = Point.Create(0, 0);
            Point globalPoint = sprite2.LocalToGlobal(localPoint);
            Point expectedPoint = Point.Create(160, 220);
            Assert.IsTrue(globalPoint.Equals(expectedPoint));
            // the position of the root object should be irrelevant -- we want the coordinates
            // *within* the root coordinate system!
            root.X = 50;
            globalPoint = sprite2.LocalToGlobal(localPoint);
            Assert.IsTrue(globalPoint.Equals(expectedPoint));
        }
        public void TestZeroSize()
        {
            Sprite sprite = new Sprite();

            AssertAreEqualWithSmallError(1.0f, sprite.ScaleX);
            AssertAreEqualWithSmallError(1.0f, sprite.ScaleY);

            // sprite is empty, scaling should thus have no effect!
            sprite.Width = 100;
            sprite.Height = 200;
            AssertAreEqualWithSmallError(1.0f, sprite.ScaleX, "wrong scaleX value");
            AssertAreEqualWithSmallError(1.0f, sprite.ScaleY, "wrong scaleY value");
            AssertAreEqualWithSmallError(0.0f, sprite.Width, "wrong width");
            AssertAreEqualWithSmallError(0.0f, sprite.Height, "wrong height");

            // setting a value to zero should be no problem -- and the original size should be remembered.
            Quad quad = new Quad(100, 200);
            quad.ScaleX = 0.0f;
            quad.ScaleY = 0.0f;
            AssertAreEqualWithSmallError(0.0f, quad.Width, "wrong width");
            AssertAreEqualWithSmallError(0.0f, quad.Height, "wrong height");

            quad.ScaleX = 1.0f;
            quad.ScaleY = 1.0f;
            AssertAreEqualWithSmallError(100.0f, quad.Width, "wrong width");
            AssertAreEqualWithSmallError(200.0f, quad.Height, "wrong height");
            AssertAreEqualWithSmallError(1.0f, quad.ScaleX, "wrong scaleX value");
            AssertAreEqualWithSmallError(1.0f, quad.ScaleY, "wrong scaleY value");
        }
        public void TestSetTransformationMatrixWithZeroValues()
        {
            Matrix matrix = Matrix.Create(0,0,0,0,0,0);
            Sprite sprite = new Sprite();
            sprite.TransformationMatrix = matrix;

            Assert.AreEqual(0.0f, sprite.X, "wrong x");
            Assert.AreEqual(0.0f, sprite.Y, "wrong y");
            Assert.AreEqual(0.0f, sprite.ScaleX, "wrong scaleX");
            Assert.AreEqual(0.0f, sprite.ScaleY, "wrong scaleY");
            Assert.AreEqual(0.0f, sprite.Rotation, "wrong rotation");
            Assert.AreEqual(0.0f, sprite.SkewX, "wrong skewX");
            Assert.AreEqual(0.0f, sprite.SkewY, "wrong skewY");
        }
        public void TestSetTransformationMatrixWithRightAngle()
        {
            Sprite sprite = new Sprite();
            float[] angles = { (float)(Math.PI / 2.0f), (float)(-Math.PI / 2.0f) };
            Matrix[] matrices = {
                Matrix.Create(0, 1,-1,0,0,0), 
                Matrix.Create(0,-1, 1,0,0,0)
            };

            for (int i=0; i<2; ++i)
            {
                float angle = angles[i];
                Matrix matrix = matrices[i];
                sprite.TransformationMatrix = matrix;

                AssertAreEqualWithSmallError(0.0f, sprite.X, "wrong x coord");
                AssertAreEqualWithSmallError(0.0f, sprite.Y, "wrong y coord");
                AssertAreEqualWithSmallError(1.0f, sprite.ScaleX, "wrong scaleX");
                AssertAreEqualWithSmallError(1.0f, sprite.ScaleY, "wrong scaleY");
                AssertAreEqualWithSmallError(angle, sprite.Rotation, "wrong rotation");
            }
        }