Microsoft released a big update to XNA Game Studio with version 4.0. The main improvement is Windows Phone 7 support. I will probably not develop for WP7 (see reasons below) but I still need to upgrade my current Xbox 360 project from XNA 3.1 to 4.0.
The process was not as smooth as I though it would be, so I wrote this article to help people transitionning from 3.1 to 4.0.
Upgrading to XNA 4.0
To install Game Studio 4.0, you normally need Windows Vista or Windows 7. However, Microsoft made a version for Windows XP working only for Windows and X360 projects. I don’t want to buy a new OS, therefore I am going with this “light” version. However, this version does not support Windows Phone 7. That’s a shame as I’d be interested to try things with a touch screen but upgrading to Windows 7 is expensive.
To install GS 4.0, you need Visual C# 2010 Express, and to get Visual C# 2010 Express on XP, you need the Service Pack 3. Problem: SP3 install failed for some strange reason. I could find this article on Microsoft’s site: When you try to install Windows XP Service Pack, you receive the error message “Access is denied” and the third solution consisting in running command line utilities solved my problem. I was not really sure what I was doing, but it finally worked
Converting the Project
When opening a GS3.1 project with Visual C# 2010, it automatically tries to convert it to GS4.0. Each project of the solution is converted. Of course, if you are using a compiled library (dll), you need to get or compile a 4.0 version. I had to re-compile Box2D.XNA and download the latest version of TiledLib‘s dlls.
However, I had a problem when compiling for the first time, with the following error: “The referenced assembly “Microsoft.Xna.Framework.Content.Pipeline, Version=18.104.22.168, Culture=neutral, PublicKeyToken=842cf8be1de50553″ could not be resolved because it has a dependency on “Microsoft.Build.Utilities.v4.0, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” which is not in the currently targeted framework “.NETFramework,Version=v4.0,Profile=Client”. Please remove references to assemblies not in the targeted framework or consider retargeting your project.”
Luckily, one guy had the same problem on Creators Club forums and the solution given by Stephen Styrchak solved my problem. It should be solved by changing the Target Framework in the application properties, but the option is greyed out. I had to edit my csproj manually with a text editor.
Ok, this is where the conversion really starts: fixing compiling errors due to numerous API changes in the framework. Here is a list of the main changes that affected my project:
Color struct: The Color struct has moved to XNA.Framework (from Xna.Framework.Graphics). A few constructors have also been removed, such as this one: Color( Color, float). I used it a lot. Easy to fix but Color is widely used in my project so that involved many changes.
Vertex buffers and Effects: There has been a few changes with vertex buffers and effects. There are many portions of code to update because of that but the new API is really simpler and better. For instance, you do not have to do Begin/End on effects and each pass, but you just use Pass.Apply: Less code to write for the same result. Also, when you have standard vertex types, you do not need to use VertexDeclarations. The GraphicsDevice.VertexDeclaration member has been removed. Again, less code to write!
Pixel/Vertex Shader compiler : I found the compiler more strict. For instance, I had a texture and a technique with the same name, it did compile with GS 3.1, but not with GS 4.0. I also had a pixel shader function called PixelShader and it does not compile on GS 4.0.
Point Sprites: They simply have been removed. I don’t use them in my current project, but my last game Spring Up Harmony used point sprites for the back ground effect. It takes some time to update to triangles.
Render Targets: In addition to a few simple API changes when setting render targets, the most notable change is the removal of the ResolveTexture2D class. You now need to use a RenderTarget2D and can’t directly get a texture from the backbuffer.
Storage changes: A few API changes have been made in the storage too. Biggest change is the OpenContainer method that is now asynchronous (Begin/EndOpenContainer). A few functions have been renamed too.
Executing of the Game
Ok, now that the game compiles, let’s run it! And another problem occured here: The game does not run at all and asks for a DX 10 graphics card. Easy to fix: modify the XNA project properties and choose “Reach” instead of “HiDef”. Difference is that Reach has a limited API for WP7 but should be enough for me.
Here is a list of resources/blogs that I found very useful while converting my game to GS 4.0. You should read them all
MSDN Page: What’s new in XNA Game Studio 4.0
Nick Gravelyn’s blog: article on storage in GS4.0
And voilà! Conversion is done and the game runs properly (I did not test thoroughly yet). I was worried the new default use of premultiplied alpha would cause problem but I haven’t found any. If you had a problem converting your project that is not mentioned here, feel free to comment!