Datasheet

To load this texture (CityGroundSmall.jpg) into your game, just drag it into your project on the
Solution Explorer. Then click the file in the project and select Properties. You should now see the screen
shown in Figure 1-13.
Normally you would just see the build action and some options for copying the content file to the output
directory (e.g., if you want to include a .dll or .xml file). But if XNA Studio detects one of the supported
content file formats, you will see the advanced XNA properties. There are three important new settings:
Asset Name, Content Importer, and Content Processor. The Asset Name is used to load the content later;
each content file must have a unique Asset Name. For the Content Importer you can select a Texture, as
in this case, or a Model Importer for .x files or an Effect Importer for .fx files.
The Content Processor contains more options; for example, you could select DXT, DXT mip-mapped, or
Sprite 32bpp for your texture here. DXT is a compression format, which is also used in .dds files and it is
very good for textures in games because they get compressed down in a 1:6 ratio (or 1:4 if they contain
transparent pixels). This means you can have up to six times as many textures for the same space on the
hard disk and graphic memory. For 2D sprites it is usually better not to compress them, because you see
them in their full size, and using 32 bpp (bits per pixel) guarantees the best possible sprite quality. For
more information about the content pipeline, read the next part of this chapter.
Figure 1-13
If you press F5 or build the project (F6) the content is processed and a new file,
CityGroundSmall.xnb,
will be created in your output directory. The build output window also shows the following new line:
Building CityGroundSmall.jpg -> bin\x86\Debug\CityGroundSmall.xnb
The final thing you have to do is load the imported texture. You do that in the Initialize method and
use a new variable,
backgroundTexture, in your class. To load the texture, you use the asset name you
specified earlier (it was actually generated automatically from the filename of the file you dragged into
your project). To render the texture on the screen, you need a
SpriteBatch, which is discussed in the
next chapter. Basically it sets up the alpha blending, then draws textures into the sprite and finally draws
everything on the screen:
Texture2D backgroundTexture;
SpriteBatch sprites;
18
Part I: XNA Framework Basics
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 18