O'Reilly logo

Learning XNA 4.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Transparency and Other Options

When drawing multiple sprites on the screen, you can (and should, for speed reasons) draw as many of them as possible within one SpriteBatch Begin and End block. In fact, typically you’ll want to have one SpriteBatch object for your game and draw all your 2D images within that block.

As you saw in Figure 3-4, the XNA logo that I used has a big, ugly white background that would probably look better if it were transparent.

There are two ways to render portions of images transparently: either the image file itself must have a transparent background, or the portion of the image that you want to be transparent must be solid magenta (255, 0, 255) because XNA will automatically render solid magenta as transparent. Transparency can be saved to certain file formats (such as .png) that make use of an alpha channel. These formats do not contain only RGB values; instead, each pixel has an additional alpha channel (the A in RGBA) that determines the transparency of the pixel.

If you have an image-editing tool at your disposal, you can use it to create a transparent background on an image of your choice. Paint.net is a great free image-editing tool that is actually written in .NET and is available at http://www.getpaint.net. For the rest of this example I’ll be using another XNA logo image file with a transparent background, which is provided with the source code for this section of the book. The file is named logo_trans.png.

Add the transparent XNA logo image to your ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required