Chapter 1. What’s New in XNA 4.0?

In this chapter, I review major changes that have been made in XNA 4.0. If you’re familiar with XNA 3.x or other versions, this is a great place to get started in this book. If you’re new to XNA, you should probably skip ahead to the next chapter. You won’t be missing out on anything, as all of the information covered in this chapter will be covered throughout the rest of the book.

Revised Project Folder Structure

The first thing you might notice when creating a game project in XNA 4.0 is that the structure of the solution has changed. In previous versions of XNA, the content pipeline picked up game content from a subfolder named Content, which was located within the game project you created, as seen in Solution Explorer (see Figure 1-1).

Sample XNA 3.x Solution
Figure 1-1. Sample XNA 3.x Solution

In XNA 4.0, the Content folder has been moved to a new project within your solution. The content project will be listed under the name GameProjectNameContent (with the name of your project replacing “GameProjectName”) and will be followed with the word “Content” in parentheses (see Figure 1-2).

Sample XNA 4.0 Solution
Figure 1-2. Sample XNA 4.0 Solution

You’ll add all your game content (audio, textures, models, etc.) to the content project in the same way you added them to the content folder in XNA 3.x. You will also still use the same code to access the content from the content pipeline, just as you did in XNA 3.x. This move makes working with multiple versions of the same project (e.g., if you built a project for both Windows and Xbox 360) much more user friendly and less confusing.

Develop Games for Windows Phone 7 Series

Just as in previous versions of the XNA Framework, XNA 4.0 is designed for cross-platform development. Developers using XNA 4.0 can write games for Windows, Xbox 360, and now Windows Phone 7 Series. Not only that, but aside from some resource management and screen resolution issues, nearly 100% of the code written for any of those platforms will also work on the other platforms. This gives game developers incredible flexibility to easily create games for each platform.

To learn more about developing games for Windows Phone 7 Series, see Chapter 17.

Graphics Profiles

Between today’s top-of-the-line graphics cards, which support DirectX 10 and higher, the powerful Xbox 360, and the introduction of Windows Phone 7 Series, the XNA Framework supports development on a wide range of devices with different hardware capabilities. In previous versions of XNA, these hardware differences had to be dealt with at runtime, which often led to platform-dependent code. To help facilitate development on these different devices, XNA 4.0 introduces the concept of profiles. These profiles allow developers to target certain hardware devices by supporting a specific set of graphics API designated by the profile they choose.

There are two profiles in XNA 4.0: Reach and HiDef. HiDef is designed for high-powered, top-of-the-line hardware, whereas Reach is designed to support a wider range of hardware devices. The Reach profile offers a limited set of graphic features and is a subset of the HiDef profile. When choosing to write a game using the Reach profile, you sacrifice some of the more powerful graphics API, but you’ll be assured that your game will work on a variety of devices (specifically Windows, Xbox 360 and Windows Phone), assuming those devices meet the minimum specifications for XNA 4.0.

The HiDef profile is designed to support today’s most powerful graphic devices. You can use the HiDef profile to target Xbox 360 hardware as well as Windows-based computers with graphics cards supporting at least DirectX 10.

You can identify which profile is supported by your hardware at runtime by using the GraphicsAdapter.IsProfileSupported method. A chart illustrating more detail on the differences between the Reach and HiDef profiles is shown in Table 1-1.

Table 1-1. XNA profile comparison
 ReachHiDef
Supported PlatformsWindows Phone 7 Series, Xbox 360, and any Windows PC with a DirectX 9 GPU that supports at least shader model 2.0Xbox 360, and any Windows PC with a DirectX 10 (or higher) GPU
Shader Model2.0 (but Windows Phone does not support custom shaders)3.0+ (Xbox 360 supports custom shader extensions such as vfetch, which are not available on Windows)
Maximum Texture Size2,0484,096
Maximum Cubemap Size5124,096
Maximum Volume Texture SizeVolume textures are not supported256
Non Power of Two TexturesYes, but with limitations: no wrap addressing mode, no mipmaps, no DXT compression on non power of two texturesYes
Non Power of Two CubemapsNoYes
Non Power of Two Volume TexturesVolume textures are not supportedYes
Maximum Number of Primitives per Draw Call65,5351,048,575
Maximum Number of Vertex Streams1616
Maximum Vertex Stream Stride25255
Index Buffer Formats16-bit16- and 32-bit
Vertex Element FormatsColor, Byte4, Single, Vector2, Vector3, Vector4, Short2, Short4, NormalizedShort2, NormalizedShort4All of the Reach vertex element formats, plus HalfVector2, HalfVector4
Texture FormatsColor, Bgr565, Bgra5551, Bgra4444, NormalizedByte2, NormalizedByte4, Dxt1, Dxt3, Dxt5All of the Reach texture formats, plus Alpha8, Rg32, Rgba64, Rgba1010102, Single, Vector2, Vector4, HalfSingle, HalfVector2, HalfVector4; floating-point texture formats do not support filtering
Vertex Texture FormatsVertex texturing is not supportedSingle, Vector2, Vector4, HalfSingle, HalfVector2, HalfVector4
Render Target FormatsCall QueryRenderTargetFormat() to find out what is supportedCall QueryRenderTargetFormat() to find out what is supported
Multiple Render TargetsNoUp to 4; must all have the same bit depth; supports alpha blending and independent write masks per render target
Occlusion QueriesNoYes
Separate Alpha BlendNoYes

Configurable Effects

In previous versions of XNA, the BasicEffect class was a very basic effect, implemented mainly to allow new game developers to build games without in-depth knowledge of complex shader code. The idea was that serious game developers would implement their own shaders and not rely on the BasicEffect class.

Much of that thinking had to change with the onset of Windows Phone 7, which does not support custom shaders. As a result, new configurable effects were added, available on both the Reach and HiDef profiles. These are:

Basic Effects

The BasicEffect class has been tweaked to include more pixel and vertex shaders to support more realistic lighting and fog effects.

Dual Texture Effects

This effect allows you to use two different textures with independent texture coordinates. The two textures will be blended together for added complexity and detail.

Alpha Test Effects

This effect uses a reference alpha and an alpha function to implement alpha testing. This can improve performance by updating only those pixels that are drawn in the scene.

Skinned Effects

This effect uses bones and weights to determine the vertex positions. The effect is powerful when used for animation and instancing.

Environment Map Effect

This effect uses textures and a cube map texture to shade objects based on the environment, reflecting the light from objects in the scene.

Built-in State Objects

Some of the state properties that were used in previous versions of XNA to modify the way scenes were drawn on the screen and the way the graphics device processes data sent to it by your game have been moved into state object classes. These classes are:

BlendState

Controls how color and alpha values are blended

DepthStencilState

Controls how the depth buffer and the stencil buffer are used

RasterizerState

Determines how to convert vector data (shapes) into raster data (pixels)

SamplerState

Determines how to sample texture data

Scalars and Orientation

Scalars in XNA 4.0 allow developers to write their games without worrying about the native resolution or screen orientation. The scalars will automatically scale your desired resolution to the resolution supported by the device on which you’re playing your game. This enhancement dramatically improves game performance.

Screen orientation is most important on Windows Phone 7 devices, which switch rotation from varieties of landscape to portrait and back when the device rotates. The scalars automatically map your game from one orientation to another, and rotate input such as touch panel input accordingly.

Revised Input API

The Microsoft.Xna.Framework.Input.Touch namespace contains classes that enable access to multitouch input on Windows and Windows Phone devices. The namespace adds the TouchPanel class and TouchLocation class, which enable access to touch points from input devices (such as the touch panel in Windows Phone 7 series devices).

Additional Audio API

Two new classes within the Microsoft.Xna.Framework.Audio namespace have been added to improve audio support:

Microphone

This class provides methods, fields, and events for capturing audio with microphones

DynamicSoundEffectInstance

This class provides methods and events for playback of the audio buffer, giving developers the ability to play back synthesized or buffered audio

Music and Picture Enumeration and Video Playback

New audio support has been added to allow developers to use Uniform Resource Identifiers (URI) to play songs and to select, edit, and publish photos.

Use the Song.FromUri method to construct a Song object based on the specified URI. Use the MediaLibrary.SavePicture method to save images to the media library.

Modified and Added Classes and Methods

A fairly large number of classes and methods have been added or modified. You can view a full list on Microsoft’s MSDN website at http://msdn.microsoft.com/en-us/library/bb417503.aspx.

Test Your Knowledge: Quiz

  1. What significant change was made to the XNA folder structure in XNA 4.0?

  2. What game platforms are supported with XNA 4.0?

  3. What is the difference between the Reach and HiDef profiles in XNA 4.0?

  4. Why do the Japanese tourists end up sleeping in a chest of drawers in Kramer’s apartment?

Get Learning XNA 4.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.