Saving Starling Screenshots

Today’s post is (other than a nice example of alliteration) just a quick couple of code snippets to help with saving out screenshots when using Starling, instead of having to print-screen, paste into a paint program, crop to the actual game screen content, etc.

As it turns out, the author of Starling, Daniel, did most of the required work already – he’s included a method drawToBitmapData in the Starling Stage class. So that easily gets us the BitmapData for the current screen, but we also need a way to save this out, preferably as a png or jpeg to save on disk space. As a convenience, I chose to name the image files using the current date and time.

For handling PNG and JPG encoding, I used a couple of classes from the excellent as3corelib. Here’s how simple it is:

		public function savePng(showDebugInfo:Boolean = false):void
		{
			var starlingStatsWereShowing:Boolean;
			var versionTextWasShowing:Boolean;
			if (!showDebugInfo) // don't want debug info in screenshots, so toggle it off
			{
				starlingStatsWereShowing = Starling.current.showStats;
				Starling.current.showStats = false;

				versionTextWasShowing = Globals.Game.showVersionText;
				Globals.Game.showVersionText = false;
			}

			var png:ByteArray = PNGEncoder.encode(Starling.current.stage.drawToBitmapData());

			if (!showDebugInfo) // restore visibility of debug info
			{
				Starling.current.showStats = starlingStatsWereShowing;
				Globals.Game.showVersionText = versionTextWasShowing;
			}

			saveImageFile(png, "png");
		}

		private var mJpgEncoder:JPGEncoder;
		public function saveJpg(showDebugInfo:Boolean = false):void
		{
			if (!mJpgEncoder)
			{
				mJpgEncoder = new JPGEncoder(90);
			}

			var starlingStatsWereShowing:Boolean;
			var versionTextWasShowing:Boolean;
			if (!showDebugInfo) // don't want debug info in screenshots, so toggle it off
			{
				starlingStatsWereShowing = Starling.current.showStats;
				Starling.current.showStats = false;

				versionTextWasShowing = Globals.Game.showVersionText;
				Globals.Game.showVersionText = false;
			}

			var jpg:ByteArray = mJpgEncoder.encode(Starling.current.stage.drawToBitmapData());

			if (!showDebugInfo) // restore visibility of debug info
			{
				Starling.current.showStats = starlingStatsWereShowing;
				Globals.Game.showVersionText = versionTextWasShowing;
			}

			saveImageFile(jpg, "jpg");
		}

		// saves an image file to either the "\png" or "\jpg" subdirectory, using the current date and time as YYYYMMDD_HHMMSS as the filename.
		private function saveImageFile(image:ByteArray, imageExt:String):void
		{
			var fileName:String = Constants.SCREENSHOT_DIRECTORY + imageExt + "\\" + Utils.getIsoFormattedDateImageFilename() + "." + imageExt;

			trace("Saving " + fileName + ".......");

			var file:File = new File();
			file.nativePath = fileName;
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.WRITE);
			fileStream.writeBytes(image);
			fileStream.close();
		}

As you can see, there’s a parameter for savePng and saveJpg called showDebugInfo – if that is false, the Starling stats and my own custom on-screen game version TextFields are disabled for the screenshot, then toggled back to their previous visibility state after the screenshot is taken. I bound savePng and saveJpg with no debug info to F1 and F2, and savePng and saveJpg with debug info to F5 and F6. Now I can take screenshots at any time by simply pressing a key.

A cool thing to consider doing is taking a screenshot automatically whenever the game is closed during development, since this will usually take a shot of whatever feature was being tested – I got the idea from this interview with the author of “The Dungeoning”.

Here’s a couple of sample screens captured using the above code:

20131028_154956

jpg with no debug info

20131028_155018

png with debug info

Update 2014/07/14:

The nice folks over at DiaDraw have created a slick ANE that allows you to record video from your Starling and Away3D apps – be sure to take a look if that’s something that is of use to you!

No Comments

Trackbacks/Pingbacks

  1. Record Starling gameplay video | Easy Native Extensions - […] thanks to the guys at Bacon Bandit Games for their handy post on saving screenshots from Starling and  also thanks to…

Leave a Reply

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