MULTIMEDIA

Microsoft XNA Game Studio 3.0 : Displaying Images - Using Resources in a Game (part 1) - Loading XNA Textures

4/18/2011 11:44:32 AM
You’ve done a lot of hard work, but your program still can’t draw any pictures. If you run the solution that you’ve created, you get the familiar blue screen. Next, you have to write some C# code that fetches the image resource and draws it on the screen at a particular location.

1. Loading XNA Textures

Within XNA, images that you want to draw in your games are called textures. Textures can be drawn as flat pictures, and they can also be wrapped around 3-D models. You’ve already seen how to use the XNA Color type, which lets you manipulate color information. Now you’ll use another type of XNA data variable so that you can work with your picture as a texture. XNA provides a range of types that are used to deal with textures. The type you’ll use is called Texture2D. This holds a texture that you manipulate in two dimensions; that is, it is drawn on the screen as if it were a flat surface.

You use the same program structure that you used for previous games. Members of your game class represent the "game world." These are updated by the UpdateDraw method to draw the output. The game data takes the form of a single variable that holds the texture, as shown here: method and used by the

// The Game World

Texture2D jakeTexture;

The Draw method draws this texture on the screen, and you could use the Update method to make the image move around the screen by changing the draw position.

You also can use another method that lets the program take control when the graphics need to be loaded. Figure 1 shows how this works.  It shows that there is also a LoadContent method that is called by XNA when a game starts running.

Figure 1. The Game1 class with the LoadContent method


You can think of LoadContent as another person in the Game1 office. That person has his or her own telephone. When the phone rings, that person must load all the content and make it ready for use, as follows:

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here
}

In addition to loading the content that the game needs, the LoadContent method also creates a SpriteBatch for the program to use. You will use this later to draw the texture on the screen. You’ve even been given a comment to tell you where to place the code that loads your texture. This is the place where the program must ask the Content Manager to fetch the texture:

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

jakeTexture = this.Content.Load<Texture2D>("jake");
}

When the game starts, XNA calls the LoadContent method to fetch content for use in the game. The method then performs the statement that loads the texture content:

jakeTexture = this.Content.Load<Texture2D>("jake");

The Load method is a kind of multipurpose tool called a generic method. Because it’s generic, it can be used to fetch any kind of item, from textures to audio files to 3-D models. You tell Load to fetch a Texture2D by placing the name of the type you want after the method name. You then give the method the asset that you want it to fetch. If you select the Jake.jpg item in Solution Explorer, and then look in the XNA Game Studio Properties pane (which should be in the lower right of the XNA Game Studio window), you can see that the asset name has been taken from the file name of the resource. Figure 2 shows the property information for the Jake image resource.

Figure 2. Jake image resource properties


This property information tells XNA Game Studio where the image file is located, what to do with the file when the project is built, and the name to use in the program. So, once the Load method has completed, you have a copy of the image in the texture in your game. If the game had lots of different images, you would declare additional Texture2D items in your game world and assign them to textures using the LoadContent method as well.

If you get the name of the texture wrong, the game program fails in this method, as it is looking for an asset that is not there. The program fails by throwing an exception. Figure 3 shows the error that is produced if the asset name of a content item is incorrect.

Figure 3. The texture file not found exception


Later, you’ll find out how to take control when things go wrong like this; for now, you should make sure that the asset name you use in the call of Load matches the name of the content item.


Note:

Our Great Programmer spends a lot of time worrying about things that might go wrong. She figures that in a commercial application, such as one that might be used in a bank, she has to write at least as much program code to deal with all the potential errors as she writes to perform the actual job. Game programs are probably not as critical as bank code in that, if they go wrong, nobody actually loses any money, but if a game constantly crashes, it will never become popular. Later, you’ll see how to make sure that your program fails as seldom as possible.

Other  
  •  iPhone 3D Programming : Holodeck Sample (part 5) - Overlaying with a Live Camera Image
  •  iPhone 3D Programming : Holodeck Sample (part 4) - Replacing Buttons with Orientation Sensors
  •  iPhone 3D Programming : Holodeck Sample (part 3) - Handling the Heads-Up Display
  •  iPhone 3D Programming : Holodeck Sample (part 2) - Rendering the Dome, Clouds, and Text
  •  iPhone 3D Programming : Holodeck Sample (part 1) - Application Skeleton
  •  Building LOB Applications : Printing in a Silverlight LOB Application
  •  Building LOB Applications : Data Validation through Data Annotation
  •  Building LOB Applications : Implementing CRUD Operations in RIA Services
  •  Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 2) - Adding Resources to a Project
  •  Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 1)
  •  iPhone 3D Programming : Blending and Augmented Reality - Rendering Anti-Aliased Lines with Textures
  •  Programming with DirectX : Game Math - Bounding Geometry (part 2) - Bounding Spheres & Bounding Hierarchies
  •  Programming with DirectX : Game Math - Bounding Geometry (part 1) - Bounding Boxes
  •  Programming with DirectX : Game Math - Matrices
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 2) - Jittering
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 1) - A Super Simple Sample App for Supersampling
  •  Building LOB Applications : Navigating RIA LOB Data
  •  Building LOB Applications : Databinding in XAML
  •  Microsoft XNA Game Studio 3.0 : Program Bugs
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Adding Vibration
  •  
    Most View
    Golden Media Spark One - Plenty To Offer Out Of The Box (Part 1)
    Introducing UEFI BIOS (Part 2)
    How To Automate Your Web With ifttt (Part 1)
    Corsair Carbide 200r - Joy To Build
    Panasonic Lumix DMC-SZ9 - Lots Of Smart Features In A Very Small Camera
    HTC One - A Huge Leap For Android Smartphones
    Windows Server 2003 : Advanced Backup and Restore (part 1) - Backup Options, The Ntbackup Command
    Linux vs Windows 8 (Part 5)
    Gigabyte Osmium Aivia Mechanical Keyboard
    The Complete Guide To Photography On Your Mac! (Part 2)
    Top 10
    Does Microsoft Have An Image Problem? (Part 2)
    Does Microsoft Have An Image Problem? (Part 1)
    Time For A Bigger iPhone?
    99 Mac Secrets (Part 5) - Top ten third-party apps
    99 Mac Secrets (Part 4) - iMovie secrets, GarageBand secrets, iWork secrets
    99 Mac Secrets (Part 3) : Safari secrets, Mail secrets, Safari shortcuts, Mail shortcuts, iPhoto secrets
    99 Mac Secrets (Part 2) : Customizing, Best menu bar add-ons, Quick Look secrets
    99 Mac Secrets (Part 1) : General OS X tips, Security tips, System shortcuts
    iMovie Trailers And Audio Premastered
    PowerTraveller Powerchimp 4A Battery Charger