Using XNA Games Studio 3.x you could build a game that would run across Windows, Xbox and Zune. Now, unless you travelled to America or lived there you were unlikely to have a Zune or Zune HD. But now with Windows Phone 7 Series we have the promise of a device far more available for our mobile gaming.
With the release at Mix of the Windows Phone Developer Tools preview we now have XNA Games Studio 4.0 support in Visual Studio 2010 and the ability to build games for Windows, Xbox 360 and Windows Phone!
So lets look at moving the XNA 3.x Platformer starter kit to XNA 4.0 so we can run it on our Windows Phone emulator.
The easiest way to do this is to create a new Platformer game using the starter kit under XNA 3.x. Then to port it to XNA 4.0 in VS2010, you need to create a new XNA 4.0 game in VS2010 and then add to this the code and content files from your platformer XNA 3.x game.
With the Zune version of the platformer game you had to have special low resolution graphics, but with Windows Phone I was able to have a playable game using the same graphics assets as the Windows and Xbox versions! The Windows Phone resolution of 800 x 480 really makes a difference.
Having moved the code other there a only a small number of changes required to get the game running on Windows Phone. This is due to the changes in the XNA 4.0 API, some specific to Windows Phone support.
First I had to change the screen size and target frame rate code:
private const int TargetFrameRate = 30; private const int BackBufferWidth = 480; private const int BackBufferHeight = 800;
Because the screen resolution had changed there are some hardcoded screen offset values in Level.cs file that need altering to fit the new screen dimensions:
Vector2 screenOffset = new Vector2(0, -60);
A value of –60 worked for ok.
Then to access the file system where the text file level definitions are stored I had to change the code to use a TitleContainer instead of the StorageContainer:
Stream fs = TitleContainer.OpenStream("Content/" + levelPath);
The final change for the default game was the ability to play the sound effects. When I added the sound files to the content project, the sound effects where loaded with a content processor of Song, not soundeffect. Once I’d corrected this, the game compiled and ran.
I ported my enhanced ZuneHD platformer game which made use of Nick Gravelyn’s ZuenLib to give me landscape orientation and touch screen support. I also use the accelerators in the ZuneHD to move the player from left to right (a touch of the screen causing jump).
In the Windows Phone Developer Tools preview the emulator doesn’t project a route to emulate accelerometers, although the touch screen should work if you have a touch screen pc with Windows 7!
To make the accelerometer code to compile I just had to change the references to include:
And then change the data type for the accelerometer data to AccelerometerReading. Not having a device yet I haven’t been able to test the code.
To make the touch screen functionality compile and work under mouse emulation in the emulator I just had to change the references to:
Now the platformer game compiles and plays on the Windows Phone emulator. The source to this point is here. Although it is still not optimised for the Windows Phone platform screen dimensions because the level definitions are a lot wider than the screen and the basic platformer game does not include horizontal scrolling. This, as well as additional pick ups and vertical scrolling, is something I implemented in my Enhanced Platformer game.
And is next on my blogging to-do list.