Microsoft XNA Game Studio 3.0 : Displaying Images - Resources and Content (part 1)

2/27/2011 10:45:22 AM
In the early days of computers, a program simply read in numbers and printed out results. Things have moved on a bit since then, and now computer programs can work with images, video, and sound. This is especially useful where games are concerned; a large part of the enjoyment of a game results from an attractive game environment. And sometimes the graphics themselves form part of the game play. If you want to become a game developer, you need to know how these resources are made part of your program. In fact, many programs today have significant graphical content in the form of splash screens, icons, and the like. So the first thing you need to do is get some images and incorporate them into your project. Later, I’ll show you how to use other kinds of resources, including fonts (for writing text) and sounds.

Unfortunately, I won’t be able to help you create your graphics for use in computer games. I have no artistic abilities whatsoever, although I do know how to use a camera. If you need artistic resources, my advice is to find someone who is good at art and commission him or her to do the drawings for you. The same goes for any music or sounds that you might need.

This means that you can concentrate on what you are supposed to be good at: creating the game itself. This is what professional game developers do. They have a team of programmers who make the game work and a team of artists and sound technicians who work on the sensory aspects of the game. Having said that, you might be good at graphic design as well as programming, in which case you can do both. However, I still advise getting an artist involved, as it helps spread the work around and provides you with a useful sounding board for ideas. It also makes it more fun.

1. Getting Some Pictures

At this point, you need some pictures. You need to tailor your images to fit the screen of the XNA device you are going to use. The Xbox screen is capable of showing high-resolution images. A high-resolution image is made up of a large number of dots, or pixels. Modern digital cameras can create images that are thousands of pixels in height and width. However, from a game point of view, you want to make the images as small as you can. This reduces the amount of memory they consume and also reduces the work required to move them around the screen. You won’t usually need very high resolution for your games, so your pictures need be no more than 600 pixels in each direction. The Zune display is 240 pixels wide and 320 pixels high, so you should use even smaller images for XNA games intended to run on this device.

There are a number of different formats for storing pictures on computers. Your pictures should be in the Portable Network Graphics (PNG), Windows Bitmap (BMP), or Joint Photographic Experts Group (JPEG) format. The PNG and BMP formats are lossless, in that they always store an exact version of the image that is being held. PNG files can also have transparent regions, which is important when you want to draw one image on top of another. The JPEG format is lossy, in that the image is compressed in a way that makes it much smaller, but at the expense of precise detail. The games that you create should use JPEG images for the large backgrounds and PNG images for the smaller objects that are drawn on top of them.

If you have no pictures of your own (which I consider highly unlikely), you can use the ones that I have provided with the sample files for this article, but the games will work best if you use your own pictures. Figure 4-1 shows my picture of Jake. I will be using this for my first XNA graphics programs. You can use another picture if you wish.

Figure 1. Jake

I have saved the image in the JPEG file format with a width of 600 pixels. If you need to convert into this format, you can load an image using the Microsoft Paint program and then save it in this format. With Paint, you can also scale and crop images if you want to reduce the number of pixels in the image. For more advanced image manipulation, I recommend the program Paint.Net, which you can obtain for free from

2. Content Management Using XNA

As far as XNA is concerned, content (images, sounds, 3-D models, and video) is what makes games more interesting. XNA treats items of content in the same way that variables are created in programs. XNA can import a content item of a particular type (for example, my file containing a picture of Jake) and give it an identifier. When the game program is running, XNA fetches the game content items as they are requested by name. These content items are sometimes referred to as assets. In the same way that a company has assets, such as buildings, machinery, and staff, a game has assets such as sounds and images.

3. Working with Content Using XNA Game Studio

You use XNA Game Studio to put content into your game. When the finished program is constructed, XNA Game Studio makes sure that the assets are available to your game. The good news is that you don’t need to worry about any of this; you need only know how to load assets into XNA Game Studio and get hold of them from within your game programs.

4. XNA Game Studio Solutions and Projects

You start making a game by creating a brand-new project. I called mine JakeDisplay. You create the project using the New Project dialog box as you’ve done for all your previous projects. Remember that the project you are creating is either a Windows Game (3.0), an Xbox Game (3.0), or a Zune Game (3.0). Note that the Create Directory For Solution option is selected in this dialog box. Whenever you create a project, you should ensure that this option is selected. This creates a directory structure that contains the program and all the other items that are required to make the game work.

Figure 2 shows what is created when I make a new project called JakeDisplay.

Figure 2. The JakeDisplay solution directory

However, the file JakeDisplay that you can see in the directory is a solution. This might be confusing. You’ve used the New Project command in XNA Game Studio and have ended up with a solution. In this case, XNA Game Studio has created a solution called JakeDisplay and then added a single project to that solution. The project is also called JakeDisplay. You can think of a solution as a "shopping list" of projects. Figure 3 shows how this works. The solution holds a list of the names of project files. Each of the project files holds a list of the names of the files used in that project. Each item on the list is often referred to as a reference to that item, in that it tells XNA Game Studio how to get to it.

Figure 3. The JakeDisplay solution

The solution file holds the name of the JakeDisplay project. The project file holds the names of the C# files in the project (Game1.cs and Program1.cs) and other resources used by the project including the Content directory. At present, the only two resources are GameThumbnail.png, which is an image used as a thumbnail on the display when the game is stored on the Xbox or Zune, and Game.ico, which is the icon used for the game program file on a Microsoft Windows PC. When you add your image of Jake to the project, you add the name of the file to the project file so that XNA Game Studio knows where to go to get the asset. XNA Game Studio displays the contents of the solution and project files as a diagram in Solution Explorer, as shown in Figure 4. Note that the solution file and project files also contain other settings (the Properties and References) that you’ll use later.

Figure 4. JakeDisplay in XNA Game Studio Solution Explorer

You have already seen that a single game solution can hold projects for deployment to an Xbox, a Zune, or a Windows PC. Sometimes you might want to add more projects to a solution so that you can separate your code into reusable portions or because you want to reuse code that you already separated that way. For example, you might make a project called HighScoreManager, which would be in charge of displaying high-score tables for your game. High scores work the same way in many games, so it makes sense to write the code only once and then use it in those games. You would do this by creating a library project to deal with the high scores and then add this project to the "shopping list" of those projects. However, for now, you simply create games that are single projects.


Our Great Programmer is very keen on using projects to reuse code. The way she sees it, that way she can get paid several times for writing the same piece of software. When she starts work on a new system, she takes a lot of time to try to structure things into projects so that different parts of the system are in separate projects.

  •  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
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Using the Keyboard
  •  iPhone 3D Programming : Blending and Augmented Reality - Stencil Alternatives for Older iPhones
  •  iPhone 3D Programming : Blending and Augmented Reality - Poor Man’s Reflection with the Stencil Buffer
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Reading a Gamepad
  •  iPhone 3D Programming : Blending and Augmented Reality - Shifting Texture Color with Per-Vertex Color
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Extensions and Their Uses
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Caveats
  •  Building LOB Applications : Using Visual Studio 2010 WCF RIA Data Services Tooling
  •  Building LOB Applications : Implementing CRUD Operations in WCF Data Services
  •  iPhone 3D Programming : Blending and Augmented Reality - Wrangle Premultiplied Alpha
    Top 10
    Windows Server 2003 : Domain Name System - Command-Line Utilities
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 2)
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 1)
    Brother MFC-J4510DW - An Innovative All-In-One A3 Printer
    Computer Planet I7 Extreme Gaming PC
    All We Need To Know About Green Computing (Part 4)
    All We Need To Know About Green Computing (Part 3)
    All We Need To Know About Green Computing (Part 2)
    All We Need To Know About Green Computing (Part 1)
    Master Black-White Copying
    Most View
    Microsoft Tries To Flatten Competition With Surface (Part 3) - Dropbox drops Public Folders, SSD Prices Way Down, AMD Adopts Arm for Armor
    SQL Server 2008 R2 : Dropping Indexes, Online Indexing Operations, Indexes on Views
    NZXT Source 210 Elite - Finest Cases For Frugal Gamers
    OLED Me Be the One
    Toshiba Tecra R850
    Windows 8's Unexpected Features (Part 1)
    Computing Yourself Fit (Part 4)
    Crucial Ballistix Tactical LP 16GB Kit
    100 Windows Speed-Up Tips (Part 1) - Clean up your hard drive & Defrag your computer
    Organize Windows With Virtual Desktops
    Developing Applications for the Cloud on the Microsoft Windows Azure Platform : DNS Names, Certificates, and SSL in the Surveys Application
    Improve Your Mac (Part 2) - Add Music To You Movies
    LG Optimus L3 E400 Review (Part 1)
    Roku 2 XS
    Get An Awesome Satnav For Free
    Windows 7 : Syncing with Network Files (part 2) - Dealing with Conflict
    KWA 150 SE – The Most Expensive Amplifier Of ModWright
    Kindle Fire - The Second Coming (Part 1)
    Essential Wedding Kit (Part 3) - Shoot-saving kit
    Web Security : Attacking AJAX - Checking for Cross-Domain Access, Reading Private Data via JSON Hijacking