Flash 11 – using Stage3D for 2D graphics


The graphics rendering can be a bottle neck with many games. Especially with Flash there can be only limited amount of animating graphics on the screen and there are also limitations on the screen size for running games smoothly. The just released Flash 11 comes with Stage3D API that is a low level GPU accelerated API for rendering graphics. Using GPU acceleration brings the graphic performance on a whole new level but to properly use Stage3D one really needs to know 3D programming. Luckily there are already libraries that implement the functionality needed for drawing 2D graphics and do all the Stage3D stuff behind the scenes. Starling framework is one of these libraries.

Starling framework is an open source library that provides conventional Flash display list architecture while using Stage3D for the actual rendering. Starling is pure AS3 library, it’s free and open source so modifying and tuning it is really simple. The three most fundamental classes in Starling framework are Texture, Image and Sprite.

Texture can be considered as equivalent to BitmapData in the conventional display object world. It wraps the Stage3D texture and can be constructed from BitmapData or Bitmap. To make rendering more efficient once should have the different source images grouped properly on single BitmapData instance, create Texture from it and then create SubTextures with clipping rectangles from the Texture.

Image is used for drawing Texture on the screen so it can be considered to be equivalent to Bitmap. Images can be moved, rotated, scaled and their transparency can be adjusted.

Sprite is equivalent to normal Sprite and it is a display object container that can contain any number of child display objects. Sprite can be also flattened which means the changes to it’s children won’t be visible until the Sprite is flattened again or unflattened. When Sprite is flattened it’s content is optimized for rendering so flattened Sprites should be used when ever possible. One thing to notice is that Starling Sprite has rotation in radians not degrees like the normal Sprite.

Initializing Starling is really simple. Just create a class that extends Starling Sprite and pass it and few other parameters when creating an instance of Starling main class, adjust rendering parameters if you want and call start function for the Starling main class. When everything is ready the Sprite extending class you gave as a parameter to Starling will be instantiated and you can start adding more display objects on it.

That’s all this time. Next post will contain some tips and tricks for tuning the Starling performance.

Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: