Games [XUI2D] BitmapCreator vs. CompressedBC

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are two types in BitmapCreator library that are used by XUI2D: BitmapCreator and CompressedBC (Compressed Bitmap Creator).
In many cases you do not need to deal with any of them as they are created and used internally by the XUI2D framework.
If you want to do custom drawings then you do need to understand how to use them.
Lets start with BitmapCreator.

BitmapCreator is similar to a mutable bitmap. Its core is made of an array of bytes that stores the image data. When you call BitmapCreator.Bitmap a B4XBitmap object is created from this buffer.
You can convert a B4XBitmap to BitmapCreator with X2.BitmapToBC. This sub calls bc.CopyPixelsFromBitmap to extract the image data from the bitmap.

Copying a BitmapCreator to another BitmapCreator is a very fast operation as it just needs to copy the bytes from one array to another.
There is one caveat. Transparent parts are copied as well. They are not blended with the existing pixels.
BitmapCreator does support copying another BitmapCreator without skipping blending:
B4X:
Public Sub DrawBitmapCreator (Source As BitmapCreator, SrcRect As B4XRect, TargetX As Int, TargetY As Int, SkipBlending As Boolean)
This means that every pixel in the source BC needs to be checked for transparency and then be blended with the target BC. It is much slower and is too slow to be used every cycle.

CompressedBC (CBC) is a more sophisticated data structure. It is created from a BitmapCreator by calling BitmapCreator.ExtractCompressedBC.
CBC is an immutable object.
CBC only stores the non-transparent parts. This means that when we draw a CBC on a BitmapCreator, the transparent parts in the source image will not be copied to the target BC (which is what we want in most cases).

Extracting a CBC from a BitmapCreator requires a cache object. You should use X2.GraphicCache.CBCCache:
B4X:
bc.ExtractCompressedBC(bc.TargetRect, X2.GraphicCache.CBCCache)

I've added a parallax background to the 'clumsy bird' example. The background is made of 4 images.
The furthest image is stored as a BitmapCreator and the others are stored as CBCs.
This makes sense as the furthest is copied as-is to the target BC and then the other layers are added above it. We don't need to erase previous drawings as the furthest image will replace all pixels anyway.
 
Top