Alpha Channel Transparency

You can make an entire game using just color-keyed transparency, but there is a limitation on the quality when using this technique because you must have discrete pixels in such an image unless some sort of render-time blending is performed. Although it is possible to do alpha blending at runtime, it’s not a good way to develop a game—it’s best to prepare your artwork in advance.

The preferred method for rendering with transparency (especially among artists) is using an alpha channel. One great advantage to alpha-blended images is support for partial transparency—that is, translucent blending. Rather than using a black border around a color-keyed sprite (the old-school way of highlighting a sprite), an artist will blend a border around a sprite’s edges using an alpha level for partial translucency, which looks fantastic in comparison! To do that, you must use a file format that supports 32-bit RGBA images. Targa is a good choice, and PNG files work well, too. Let’s take a look at the spaceship sprite again—this time with an alpha channel rather than a color-keyed background. Note the checkerboard pattern in the background; this is a common way of showing the alpha channel in graphic editors. Figure 3.6 shows the fatship sprite with a new alpha channel.

Figure 3.6. This sprite will be rendered with alpha channel transparency.


Advice

Oddly enough, the latest version of the Windows Bitmap format now supports 32-bit RGBA color with an alpha channel, too!


The AlphaDemo program (shown in Figure 3.7) is nearly identical to the ColorkeyDemo program, so I won’t list it here again. The only changes that have been made are the program’s title and the filename (using the fatship_alpha.tga file instead of fatship_colorkeyed.bmp). The output from the program is the same as the previous one, but this program now renders the sprite with an alpha—we’re not using it to its full potential, but we will in due time.

Figure 3.7. The AlphaDemo program demonstrates sprite rendering with an alpha channel.


Speaking of potential, these are very basic sprite demos. We can’t see movement, rotation, or animation, so what was the point of all this? It’s important, in my opinion, to focus on the technology first, and then when that is covered, to focus on use or implementation. We have just added some quite advanced vector and sprite support to the Advanced2D engine and verified that 2D rendering is working (with both color-keyed and alpha transparency). Next, we need to test the more advanced features of the Sprite class, which we’ll do in the next chapter.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset