Tuesday, May 13, 2008

Blitting and Double Buffering with Flash

As I've been delving deeper into bitmap caching and game optimization I keep running across Blitting (BLIT, bitBlit) and Double Buffering.

I think I finally have a good grasp on both after using more resources from 8BitRocket as well as Jesse Warden's blog post which can be found here.

Here is the brief low-down:

Blitting is taking many bitmaps and combining them into 1 bitmap. Jesse (and wikipedia) use pac man as an example. Instead of having a sprite or bitmaps for each ghost, dot, wall, and pac man himself, the game actually is only displaying 1 bitmap. The rest of the objects are just having their pixels copied onto the main bitmap. This dramatically improves performance since flash no longer has to render all of the multiple movie clips.

Double Buffering is using 2 bitmaps to display the entire game. 1 bitmap is being altered in the background while the other is being shown to the user. Once the first one is finished being altered it will swap with the one being shown. This prevents flickers from occurring on the screen. 8BitRocket makes an interesting suggestions of just using 1 bitmap and locking it while you make all the transformations and then unlocking it before you call the update screen event. They call in the "poor man's double buffer".

Not sure if I will be using either of these techniques yet in the upcoming side scroller, but at least now if I don't use them I will understand them and have a reason for not using them.


darnpunk said...

Hi there,

Interesting to see what you've got here. Anyway, I am curious on the technique you mentioned because we're building an MMORPG which involves many animation and it is isometric tile based.

Our character graphic assets are all vector based. After some prototyping, I realize that placing about 20 avatars (without map and interface) lags the game. FPS is low and response is slow.

I tried with copyPixels and saw the obvious difference and improvements. We could use the techniques you have shared here too but one thing stopping us is the nature of our characters being dynamic. The character can change their appearance as and when (shirt, pants etc). Since each of them are animated, I am not sure if this technique will fit in?

What are your views? Do you have any suggestions?

Ickydime said...

I think you could pull it off. Just when they change their appearance you would need to re-cache the bitmapData sequence.

So at the beginning you run through every frame of the vector sequence and save a bitmapData of each frame. They buy some new pants. Drop the pants into your vector Movieclip (even though it is not on the stage) and then run through every frame again saving the bitmapData again.

That seems like it would work... at least in my head. I am not sure how often they would change appearance in your game or how many frames you would need to then run through. Therefore I am not sure the performance or memory hit you may take.

Sounds like an interesting project. Feel free to hit me up @ mark [at] thebasement [dot] tv If you want to discuss anything in more detail.

Kendrick Blake said...

Actually there are also local seo for lawyers whom I think could use this technique you have here. I do appreciate posts like this one cause you really did explain the process of troubleshooting the problems in a layman's term, in which most of the blogs I've read don't cause they prefer using technical and professional terms. Thank you so much for the information. I hope you also make a post about caching and net resolutions.