Saturday, June 04, 2011

How to get high-res images in a low-res Air app

I thought about another way I might get partial HD graphics working.  The problem is that the resolution of the screen is 1024x600, and setting the application to that size (even without changing the assets) slows things down too far.  So I set it to 512x300 and then let Air scale it to full res to get reasonable performance.  This means everything is pixelated doubly.

After noticing that text and standard buttons render at full HD, I had the thought of loading a bitmap that's double the resolution that it needs to be, then scaling it down before letting Air render it.  This works for the one item I tested it on.  Some of my other objects use a different method for rendering, so those will take some work to HD-ify.  Here's what the code looks like:


// This image is double the size it needs to be
[Embed(source='rainStormTitleFinalHD.png')] public static var RainStormTitleImage:Class;
titleImage = new Image();
titleImage.setImage(new RainStormTitleImage);
titleImage.x = -30;
titleImage.y = 10;

// This part is where we scale it down.
titleImage.scaleX = 0.5;
titleImage.scaleY = 0.5;

addChild(titleImage);
The result of this is that when an Air app designed for 512x300 is upscaled to 1024x600, the above image is the perfect resolution (not pixelated).

Here is a comparison of a section of the screen.






First the original, pixelated image.










And here's the HD image.

So it's clearly working, and so far it doesn't affect the framerate noticeably.  But it's only the title screen that's setup this way.

Unfortunately, most of my game objects use BitmapData and copyPixels to an off-screen buffer which is eventually rendered in the normal Air way.  Before GPU acceleration in Air, this was a speedy way of drawing graphics that don't need to be scaled or rotated.  With GPU acceleration, this way is slower.  So I need to make everything a Sprite or some such and just do things with the "normal" Air method to get HD bitmaps in a low-res app.  And that's a fair bit of work.  But right now it's not my highest priority.

No comments: