Starling Framework

Adobe has released a great little framework called the Starling Framework. This framework is based on the iOS game engine Sparrow Framework.
The Sparrow framework replicated the flash environment pretty close in naming conventions and structure. Basically the Sparrow framework is a port of flash to iOS. Now the iOS flash port has been ported to flash, funnily enough…
The Starling framework uses the molehill API to give a 2D engine that is powered by the GPU by placing the assets on planes in 3D and simulating a 2D screen, thus gaining from the GPU rendering of molehill.

Both Sparrow and Starling uses sprite sheets and bitmaps, and these are mapped using a texture XML. This data format is specific to Sparrow, and used again in Starling.
Lee Brimelow has a great video course on how to use Texture Packer to create a png and texure XML here:
http://gotoandlearn.com/play.php?id=147

To decrease the file size of the packages I wipped up a little library that can convert an XML and PNG file to a binary file and then back again. By deflating the XML and the PNG together to binary form you significally decrease the file size as well as you get one less file to load (one file holds both data).

One of the best advantages with this is that the XML in binary form doesn’t take up us much data as it does in it’s verbose string form.
The package consists of 2 files, BytesSprite and SparrowData. SparrowData is only used when inflating a byte array back to XML and PNG.
It should be pretty straightforward to use: The ByteSprite class has 2 functions, inflate and deflate.
This example will convert a PNG and XML to binary form, and then back again using this class:

package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.ByteArray;
	import mx.core.ByteArrayAsset;
	import se.mowday.utils.ByteSprite;
	import se.mowday.utils.SparrowData;

	public class Main extends Sprite
	{
		// The texture XML data
		[Embed(source='../asset/sp.xml', mimeType='application/octet-stream')]
		private var data:Class;

		// The PNG sprite image
		[Embed(source='../asset/sp.png')]
		private var png:Class

		public function Main():void
		{
			var requestHeaderSize:int = 1200;
			var responseHeaderSize:int = 300;
			var perFile:int = (requestHeaderSize + responseHeaderSize) * 0.0078125;
			perFile = 0;

			var data:ByteArray = ByteSprite.deflate(Bitmap(new png()).bitmapData, XML(new data()));
			var oldSum:int = (perFile * 2 +105);
			var newSum:int = (perFile  + convert(data.length));

			trace("Original file: 105kb");
			trace("Original header size: " + (perFile * 2) + "kb");
			trace("Sum original = " + oldSum + "kb");
			trace("------------------------");
			trace("New file: " + convert(data.length) + "kb");
			trace("New header size: " + (perFile) + "kb");
			trace("Sum new = " + newSum + "kb");
			trace("------------------------");
			trace("Gain = " + (oldSum - newSum) + "kb - " + (100 - Math.round(newSum / oldSum * 100)) + " %");

			var newData:SparrowData = ByteSprite.inflate(data);
			addChild(newData.png);
		}

		private function convert(bytes:Number):Number
		{
			return bytes * 0.0009765625;
		}
	}

}

That will output a trace that looks like this:

Original file: 105kb
Original header size: 0kb
Sum original = 105kb
————————
New file: 88.7607421875kb
New header size: 0kb
Sum new = 88kb
————————
Gain = 17kb – 16 %

When inflating a binary byte array, get the XML and PNG from the SparrowData and simply pass this on to the StarlingFramework as would usually do (like in Lee’s example).

To celebrate this I have also created a little helper app. This is a real simple AIR app that uses command line arguments to convert files back and forth.
The command line arguments are:

  • -xml {path} defines the path to XML file
  • -bin {path} defines the path to binary file
  • -png {path} defines the path to PNG file
  • -log {path} defines the path to the log file (OPTIONAL)
  • -inflate inflates the binary file to an XML and a PNG file (overwrites previous files!)
  • -deflate deflates the XML and PNG files into a binary file and saves it to disk
  • -x exits the application on successful completion(OPTIONAL)
  • -e exits the application on failing to convert(OPTIONAL)

Now to use this application is also pretty straight forward, just invoked the .exe file with the arguments and settings you want. In my case I have installed to air app to:

C:\Program Files\StarlingHelper

So to run the application on my machine (using Windows 7) I would run a command line prompt in the folder where the files are and run (for example):

“C:\Program Files\StarlingHelper\StarlingHelper.exe” -x -e -xml myXML.xml -png myPNG.png -bin myBin.ms -log log.txt -deflate

That command will deflate the XML and PNG files into a binary file, exit on completion or error and log everything.

To inflate the files back to XML and PNG again, all you need to do is:

“C:\Program Files\StarlingHelper\StarlingHelper.exe” -x -e -xml backXML.xml -png backPNG.png -bin myBin.ms -log log.txt -inflate

And 2 new files, backXML.xml and backPNG.png will be created for you.

Here is the ByteSprite.as class:
Show ▼

And the SparrowData class:
Show ▼

These are all the files you need, the first is the project that contains the ByteSprite and SparrowData class, and the second is the AIR application. Have fun!

Download: starling_lib (113.12KB)
Added: 26/09/2011
Description:
The Flashdevelop project for the "Starling lib"

Download: StarlingHelper (278.92KB)
Added: 26/09/2011
Description:
The AIR application for the Starling helper project

Leave a Reply

Your email address will not be published. Required fields are marked *

*