C# (CSharp) Sparrow.Display Namespace

Classes

Name Description
BlendMode
DisplayObject The DisplayObject class is the base class for all objects that are rendered on the screen. In Sparrow, all displayable objects are organized in a display tree. Only objects that are part of the display tree will be displayed (rendered). The display tree consists of leaf nodes (Image, Quad) that will be rendered directly to the screen, and of container nodes (subclasses of DisplayObjectContainer, like Sprite). A container is simply a display object that has child nodes - which can, again, be either leaf nodes or other containers. A display object has properties that define its position in relation to its parent ('X', 'Y'), as well as its rotation, skewing and scaling factors. Use the 'Alpha' and 'Visible' properties to make an object translucent or invisible. Every display object may be the target of touch events. If you don't want an object to be touchable, you can disable the `Touchable` property. When it's disabled, neither the object nor its children will receive any more touch events. **Points vs. Pixels** All sizes and distances are measured in points. What this means in pixels depends on the contentScaleFactor of the device. **Transforming coordinates** Within the display tree, each object has its own local coordinate system. If you rotate a container, you rotate that coordinate system - and thus all the children of the container. Sometimes you need to know where a certain point lies relative to another coordinate system. That's the purpose of the method 'TransformationMatrixToSpace'. It will create a matrix that represents the transformation of a point in one coordinate system to another. **Subclassing DisplayObject** As DisplayObject is an abstract class, you can't instantiate it directly, but have to use one of its subclasses instead. However, you can create custom display objects as well. That's especially useful when you want to create an object with a custom render function. You will need to implement the following methods when you subclass DisplayObject: - void Render ( RenderSupport support); - Rectangle BoundsInSpace ( DisplayObject targetSpace); Have a look at Quad for a sample implementation of those methods.
DisplayObjectContainer A DisplayObjectContainer represents a collection of display objects. It is the base class of all display objects that act as a container for other objects. By maintaining an ordered list of children, it defines the back-to-front positioning of the children within the display tree. A container does not have size in itself. The width and height properties represent the extents of its children. Changing those properties will scale all children accordingly. As this is an abstract class, you can't instantiate it directly, but have to use a subclass instead. The most lightweight container class is Sprite. **Adding and removing children** The class defines methods that allow you to add or remove children. When you add a child, it will be added at the foremost position, possibly occluding a child that was added before. You can access the children via an index. The first child will have index 0, the second child index 1, etc. Adding and removing objects from a container triggers events. - 'Added': the object was added to a DisplayObjectContainer. - 'AddedToStage': the object was added to a DisplayObjectContainer that is connected to the stage. - 'Removed': the object was removed from a DisplayObjectContainer. - 'RemovedFromStage': the object was removed from a DisplayObjectContainer that is connected to the stage. Especially the AddedToStage event is very helpful, as it allows you to automatically execute some logic (e.g. start an animation) when an object is rendered the first time. **Sorting children** The 'sortChildren' method allows you to sort the children of a container by a custom criteria. Below is an example how to depth-sort children by their y-coordinate; this will put objects that are lower on the screen in front of those higher on the screen. public class CompareExample : IComparator { public int Compare(DisplayObject child1, DisplayObject child2) { if (child1.Y < child2.Y) return -1; else if (child1.Y > child2.Y) return 1; else return 0; } }
Image
Quad An Quad represents a rectangle with a uniform color or a color gradient.

You can set one color per vertex. The colors will smoothly fade into each other over the area of the quad. To display a simple linear color gradient, assign one color to vertices 0 and 1 and another color to vertices 2 and 3.

The indices of the vertices are arranged like this:

0 - 1

| / |

2 - 3

**Colors**

Colors in Sparrow are defined as unsigned integers, that's exactly 8 bit per color. The easiest way to define a color is by writing it as a hexadecimal number. A color has the following structure:

0xRRGGBB

That means that you can create the base colors like this:

0xFF0000 -> red

0x00FF00 -> green

0x0000FF -> blue

QuadBatch
Sprite 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.
Stage