Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Increasing Performance Through Custom Bitmap Caching

Increasing Performance Through Custom Bitmap Caching

With Flash Player 8, I thought I would be able to increase massively the performance and persistence of varicose-g by caching the vectors to bitmap. My initial thought was to set the full VeinManager canvas so it uses the built-in bitmap caching:

// Set the canvas to cache to bitmap automatically:

canvas.cacheAsBitmap = true;

While this produced a major performance increase, it still slowed down as more veins were added. This is because the vectors are being retained and recomposited in the redraw regions each frame. As more vectors are lain down, the recompositing time increases, slowing the whole experiment down.

Instead, I wrote a custom bitmap caching routine. With each frame, the VeinManager class takes all of the new vectors that were drawn by the Vein classes and composites them onto a bitmap using the draw method of BitmapData, and then clears all of the vectors—the vectors are never even rendered by Flash to the display, you only ever see the bitmap. This means that the CPU usage for compositing never increases, no matter how long the experiment runs, and how many veins are drawn. Also, because I am reusing the same BitmapData object every frame, the amount of RAM that the system uses does not increase.

The simplified code to do the custom caching looks like this:
// Set up the BitmapData object:
canvasBmp = new BitmapData(WIDTH,HEIGHT,true,0);
// Display the bitmap in the "image" clip:
// Set up the matrix that will be used to draw
//the veins onto the bitmap:
mat = new Matrix ();
// Draw the new vectors to canvas.veins
// See the Vein source for details.
// Composite the vectors to the BitmapData object each frame:
// Clear out the old vectors: