Blitting Optimization

8 posts

Flag Post

Hi everyone!

I’m a begginer dev and I’m working on my first game :) and I was wondering about the efficiency of my blit class.

The way I see it I can do it in 2 ways:
1-Each displayed object contains a bitmapdata instance of the blitted image.
2-Each displayed object contains an array which holds the frames of the blitted image.

I was wondering about the efficiency and pros and cons of each method (in trems of CPU, RAM, render speed, ect…),and I would like to hear from you

BTW I’m working with Flash CS6 using AS3.

Thank you very much!

 
Flag Post

Blitting (if done right) allows for alot better performance than normal use of the display list. It is more difficult to do, and (maybe?) uses more RAM, depending on your method, but drastically cuts down the time spent rendering graphics.
Here is an Adobe tutorial on ways to blit if that’s what you’re looking for.

 
Flag Post

Thanks for the answer, but I already know how to blit (actually, I already used this very tutorial),now I’m trying to optimize my game.

Let’s take this tutorial as example, there they create a bitmapData object for the blockFace, and directly blit from that image to the stage.
This can be done in another way – create an array, store all the frames of the blockFace in the array, and blit from the array to the stage (only use the actual image to insert the frames into the array, say while the game is loading, but not blit from the image).

I guess what I’m trying to ask is – Which is “cheaper” (memory\CUP-wise) bitmapData object or an array..? and does it depend on the size and frames of the blitted object.

Thank you!

 
Flag Post

This is the way I do it.


package  
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class blitting_example
	{
		private var rect:Rectangle = new Rectangle(0, 0, 10, 10);
		public var point:Point = new Point;                                
                public var vect:Vector.<Number> = new Vector.<Number>;  
		public var input:BitmapData;                                              
		public var output:BitmapData;                                            
		public var frame:int;                                                  

		
		public function blit():void
		{
			rect.x = vect[frame * 2];
			rect.y = vect[(frame * 2) + 1];
			output.copyPixels(input, rect, point);
		}
		
	}
	
}

I’ve found this method to be very fast. I had about 40,000 25,000 objects blitting at once and I still maintained an FPS above 24.

 
Flag Post

Are you talking about animating your blitted images? If so, you can do even better than an array by having a spritesheet of your frames as a single huge bitmapdata, and then only drawing the relevant part to the screen.

 
Flag Post
Originally posted by Drakim:

Are you talking about animating your blitted images? If so, you can do even better than an array by having a spritesheet of your frames as a single huge bitmapdata, and then only drawing the relevant part to the screen.

That’s exactly what the above code does, it just uses the vector to store the xy positions of each frame in the bitmapData.

 
Flag Post

Something I just figured out: make your background images into bitmaps. I seems to really help performance (as opposed to things drawn in Flash).
But something along the lines of, “When should/shouldn’t I blit” would be helpful.

 
Flag Post

Thanks for the answers guys.

I actually coded a little stress test and ran it myself, and monitored FPS, RAM and CPU usage, and figured that there isn’t much of a difference when using/not using array (without array resulted in 1-2 FPS better), so I’l answer to myself – it doesnt matter :P

Guess it makes sence, cause the toal size of the bitmap is the same, it doesn’t matter if it’s sliced to single frames and put into an array.

Thanks everyone!