Starling is an ActionScript 3 2D framework developed on top of the Stage3D APIs (available for the desktop in Flash Player 11 and Adobe AIR 3). Starling was designed for game development,but you can use it for many other applications. Starling makes it possible to write fast GPU accelerated applications without having to touch the low-level Stage3D APIs.
Most Flash developers want the ability to leverage GPU acceleration (through Stage3D) without having to write such higher-level frameworks and digging into the low-level Stage3D APIs. Starling is completely based on the Flash Player APIs and abstracts the complexity of Stage3D (Molehill). The result is intuitive programming for everyone.
Starling is designed for ActionScript 3 developers,especially those involved in 2D game development. To use it,you must have a basic understanding of ActionScript 3. Since Starling is lightweight,flexible and easy to use,you can also use it for other project needs,such as UI programming. The framework is designed to be as intuitive as possible,so any Java™ or .Net™ developer can begin using it right away.
Overview of working with Starling
Starling is intuitive and easy to learn. Flash and Flex developers will understand it immediately because it follows most of the ActionScript dogmas and abstracts the complexity of the low-level Stage3D APIs. Rather than coding against concepts like vertices buffer,perspective matrices,shader programs,and assembly bytecode,Starling uses familiar concepts such as a DOM display list,an event model,and familiar APIs like MovieClip,Sprite,TextField,and others.
Starling is a lightweight bird in many ways. The number of classes is limited (around 80k of code). There are no external dependencies besides Flash Player 11 or AIR 3 (and mobile support is slated for a future release). These factors keep your applications small and your workflow simple.
Starling is free and alive. It is licensed under the Simplified BSD license,so you can use it freely even in commercial applications. We are working on it every day and we count on an active community to improve it even more.
Behind the scenes,Starling uses the Stage3D APIs—which are low-level GPU APIs running on top of OpenGL and DirectX on desktop and OpenGL ES2 on mobile devices. It's important to note that Starling is the ActionScript 3 port of Sparrow,the equivalent library for iOS relying on OpenGL ES2 APIs (see Figure 1):
Starling recreates many APIs that are familiar to Flash developers. The graph below illustrates the graphic element APIs exposed by Starling (see Figure 2):
It's odd to consider that 2D content can be created on top of 3D GPU APIs. When it comes to Stage3D APIs,many people think that those APIs are strictly limited to 3D content. The name causes confusion: If it is called Stage3D,how can you use it to create 2D elements?
The graph below illustrates the question about the ability to draw a MovieClip with the drawTriangles API (see Figure 3).
GPU are efficient and can draw triangles quickly. Using the drawTriangles API,you can draw two triangles,and then sample a texture and apply it to the triangles using UV mapping. This creates a textured quad,which represents a sprite. By updating the textures on the triangles on each frame,the end result is a MovieClip.
The good news is that it is not necessary to work through those details to use Starling. You simply provide the frames,supply them to a Starling MovieClip and that's all it takes (see Figure 4).
To better understand how Starling reduces the complexity,examine the code you would have to write to display a simple textured quad using the low-level Stage3D APIs:
// create the verticesvar vertices:Vector.<Number> = Vector.<Number>([-0.5,-0.5,// x,y,z,u,v-0.5,0.5,1,0]);// create the buffer to upload the verticesvar vertexbuffer:VertexBuffer3D = context3D.createVertexBuffer(4,5);// upload the verticesvertexbuffer.uploadFromVector(vertices,4);// create the buffer to upload the indicesvar indexbuffer:IndexBuffer3D = context3D.createIndexBuffer(6);// upload the indicesindexbuffer.uploadFromVector (Vector.<uint>([0,2,3,0]),6);// create the bitmap texturevar bitmap:Bitmap = new TextureBitmap();// create the texture bitmap to upload the bitmapvar texture:Texture = context3D.createTexture(bitmap.bitmapData.width,bitmap.bitmapData.height,Context3DTextureFormat.BGRA,false);// upload the bitmaptexture.uploadFromBitmapData(bitmap.bitmapData);// create the mini assemblervar vertexShaderAssembler : AGALMiniAssembler = new AGALMiniAssembler();// assemble the vertex shadervertexShaderAssembler.assemble( Context3DProgramType.VERTEX,"m44 op,va0,vc0\n" + // pos to clipspace"mov v0,va1" // copy uv);// assemble the fragment shaderfragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT,"tex ft1,v0,fs0 <2d,linear,nomip>;\n" +"mov oc,ft1");// create the shader programvar program:Program3D = context3D.createProgram();// upload the vertex and fragment shadersprogram.upload( vertexShaderAssembler.agalcode,fragmentShaderAssembler.agalcode);// clear the buffercontext3D.clear ( 1,1 );// set the vertex buffercontext3D.setVertexBufferAt(0,vertexbuffer,Context3DVertexBufferFormat.FLOAT_3);context3D.setVertexBufferAt(1,Context3DVertexBufferFormat.FLOAT_2);// set the texturecontext3D.setTextureAt( 0,texture );// set the shaders programcontext3D.setProgram( program );// create a 3D matrix var m:Matrix3D = new Matrix3D();// apply rotation to the matrix to rotate vertices along the Z axism.appendRotation(getTimer()/50,Vector3D.Z_AXIS);// set the program constants (matrix here)context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX,m,true);// draw the trianglescontext3D.drawTriangles( indexBuffer);// present the pixels to the screencontext3D.present();
The code sample above creates a square 2D instance (see Figure 5):
The code sample shown above is admittedly pretty complex. That is the cost of having access to low-level APIs. On the plus side,you can control many aspects,but it takes a considerable amount of code to set everything up.
With Starling,you can write the following code instead: