XNA3x to XNA40

From XNAWiki
Jump to: navigation, search

Introduction

An overarching goal of these API tweaks was that even though we are breaking things, we still want the new API to feel like the XNA Framework you know and love. Some of the details may be different, but existing developer knowledge and design patterns should be easy to move across.

Another goal was to prefer simplicity over complexity. I am a big believer in minimalist design. It’s easy to add new things each time we encounter a new problem, but I think more valuable to hone them down to their core essence, trying to find the minimum surface area necessary to communicate between developer and runtime. It makes me happy that the XNA Framework 4.0 API surface is significantly smaller than the 3.1 version.

The release of XNA Game Studio 4.0 brought about a great deal of API "tweaks" that saw the removal or modification of several portions of the XNA framework. Despite coming with a built in upgrade functionality, Visual Studio 2010 cannot entirely account for every possible API change. In his blog, Shawn (who was directly involved with the development of the XNA framework) points out that while they were adding support for Windows Phone, a great push was made for simplifying the XNA framework. It should be noted that a comprehensive list of resolved errors that may occur in the migration from XNA 3 to XNA 4 is already posted on Nelxon's website. This article will attempt to elaborate on some of those errors, as well as list a few that may not have been previously posted.

Changes

For organizational purposes, the changes will be split up by which portion of the XNA framework they occurred in. Miscellaneous changes will be categorized as such.

Graphics

Microsoft.Xna.Framework.Graphics

SamplerState

Error

'Microsoft.Xna.Framework.Graphics.SamplerState' does not contain a definition for 'MinFilter' (or MagFilter, MipFilter)

The old MinFilter, MagFilter, and MipFilter properties are collapsed into a single Filter property, which specifies all three options using a single TextureFilter enum value. The old API allowed too many permutations of filter values, many of which were not actually legal. For instance you cannot set MinFilter = None and MipFilter = Anisotropic, but that didn't stop many beginners from trying! With the new API, the TextureFilter enum only contains legal filter settings, so it is no longer possible to get this wrong.

// XNA 3.1
// NOTE: Per Shawn's post (and the current TextureEnumerations available), the below code might as well be illegal in XNA 3.1 too.
GraphicsDevice.SamplerStates[0].MinFilter = TextureFilter.Anisotropic;
GraphicsDevice.SamplerStates[0].MagFilter = TextureFilter.Anisotropic;
GraphicsDevice.SamplerStates[0].MipFilter = TextureFilter.Linear;
// XNA 4.0
// It should also be mentioned that SamplerState(s) become read only after the graphics device is initialized -
// it is therefor necessary that a new sampler state object is used if new properties are desired.
GraphicsDevice.SamplerStates[0] = SamplerState.AnisotropicClamp;

VertexElement/VertexElementMethod

The VertexElement and VertexDeclaration types still exist, but are used somewhat differently:

VertexDeclaration constructor no longer requires a GraphicsDevice
Vertex stride is now specified as part of the VertexDeclaration
Removed VertexElement.Stream property (see below)
Removed VertexElementMethod enum (because no hardware actually supported it)

And the important one:

No more GraphicsDevice.VertexDeclaration property

This is no longer necessary, because whenever you set a VertexBuffer, the device can automatically look up its associated declaration. I find it great fun porting from Game Studio 3.1 to 4.0, because I can simply delete everywhere that used to set GraphicsDevice.VertexDeclaration, and everything still magically "just works" ™.

Error

The name 'VertexElementMethod' does not exist in the current context

// XNA 3.1
new VertexElement(0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0)
// XNA 4.0
// Note that the only changes here are that the first parameter (stream, int) was removed, and the VertexElementMethod parameter was removed.
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0)

See Also

Nexlon's Converting XNA 3.1 project to XNA 4.0 Cheatsheet