Transparent Blitting
A version of this page is also available for
4/8/2010
Transparent blitting enables you to create the illusion of nonrectangular blits when animating sprites. A sprite image is usually nonrectangular, but blits are always rectangular, so every pixel within the sprite's bounding rectangle becomes part of the data transfer.
With transparent blitting, each pixel that is not part of the sprite image is treated as transparent when the blitter is moving the image to its destination, so that it does not overwrite the color in that pixel on the background image.
The artist creating the sprite chooses an arbitrary color or range of colors to be used as the transparency color key. This is typically a single uncommon color that the artist does not use for anything but transparency, and it is used to fill in all parts of the sprite rectangle that are not part of the desired image.
At run time you set the color key for the surface containing the sprite. If you want, you can automatically set it to the color of the pixel in the upper left corner of the image.
Subsequent blits can take advantage of that color key, ignoring the pixels that match it. This type of color key is known as a source color key.
You can also use a color key on the destination surface, provided the hardware supports destination color keying. This destination color key is used for pixels that can be overwritten by a sprite.
For example, the artist might be working on a foreground image that sprites are supposed to pass behind, such as the wall of a room with a window to the outside. The artist chooses an arbitrary color — one that is not used elsewhere in the image — to represent the sky outside the window. When you set this color key for the destination surface and then blit a sprite to that surface, the sprite's pixels will overwrite only pixels that are using the destination color key.
In the example, the sprite appears only in the window, but not on the wall or window frame. As a result, the sprite seems to be outside the room.
Source and destination color keys can be combined. In the example, the sprite could use a source color key so that its entire bounding rectangle does not block out the sky background.
The following topics provide further information on the implementation details behind transparent blitting:
- Color Key Format
- Setting Color Keys
- Blitting with Color Keys
- For information about blitting using alpha channels, see Alpha Blitting.