This tutorial shows how to use the MonoGame open source project to create XNA games on Windows Phone 8.
This tutorial aims to get you started creating your first Windows Phone 8 XNA game using MonoGame. XNA has been an awesome framework for creating games on the Windows Phone 7.x, Windows and XBOX 360 platforms. It provided very powerful features such as a content pipeline, spritebatches, shaders and 3D, sound, music, game controllers and so much more. Unfortunately, since this year, Microsoft decided to drop support for XNA on its new Windows Phone 8 platform (as well as on Windows 8 Store apps). Many game developers (including myself) have been using XNA for many years now and have significant codebase, tools, UI libraries and more developed with XNA in mind. While XNA games still work on Windows Phone 8, they work in a “compatibility mode” which essentially means that the developer is targeting WP7.x and cannot utilize any of the awesome new features WP8 brought to the table. By utilizing MonoGame, we can now target WP8 while still using XNA and C#, our favourite combination for making games. If you are a seasoned XNA game developer or a newbie looking to make your first XNA game, MonoGame is an awesome choice. I’ve used MonoGame myself in a couple of commercially available projects, the latest of which is MonsterUp Memory for Windows 8, currently available at the store.
What is MonoGame
MonoGame is an Open Source implementation of the Microsoft XNA 4 Framework. Without disassembling any of the original XNA libraries, the community is trying to build a compatible open source library (using even the same namespaces) that targets multiple platforms. MonoGame utilizes the power of other open source projects, like SharpDX, which delivers the full DirectX API under the .NET platform for 2D and 3D graphics as well as sound and Lidgren.Network which provides networking support for MonoGame. But the good news is that you don’t have to worry about these, since in the end MonoGame provides you, the XNA game developer, with a familiar XNA compatible game development framework that can be used to target WP8.
Before getting started, we have to setup our development environment. First of all, you need a Windows 8 x64 Pro based system. If you also want to use the WP8 emulator, you need to make sure your system supports SLAT. If you download the WP8 SDK from the link you can find to the side of this article, you will get Visual Studio 2012 Express installed at the same time. This is enough, but if you want to use the full version of Visual Studio 2012, make sure you install this before installing the SDK. After VS and WP8 SDK are installed, we need to get MonoGame itself. There are a few ways to do this.
- Download the full source code of the project from here and unzip it in a directory of your choice.
- Since MonoGame is hosted in GitHub, you can use your favourite Git client to clone the repository. I use the official GitHub app which you can download from here for free. Install the client, then go to the MonoGame GitHub page and click on the “Clone with Windows” button.
- You can also use Windows PowerShell and GitHub command line interface to achieve the same results. Just go to the directory you want MonoGame to reside and use the following commands.
git clone https://github.com/mono/MonoGame.git cd .\MonoGame git submodule init git submodule update
Most likely, after you experience the power and simplicity of XNA, you will want to make lots of games with it. So, we should better make a Visual Studio 2012 template for them. Luckily, MonoGame comes with these templates and all you need is to copy them to the correct place. Go to
C:\Users\[your username]\Documents\Visual Studio 2012\Templates\ProjectTemplates\Visual C#
and create a MonoGame directory. Then go to the place you downloaded the MonoGame project source, open ProjectTemplates and then VisualStudio2012. Copy everything in here to the directory you created before. If you do this right, the next time you open Visual Studio 2012 and click on New Project you will see a new MonoGame category under Visual C#. Well done. Double click on Windows Phone 8 Game to create your first XNA WP8 MonoGame game.
Linking to the MonoGame framework
The template is setup and you have created your solution. But it still does not know where you downloaded MonoGame itself! This is very easy to fix. Just follow these steps.
- Right click on your solution
- Choose Add -> Existing Project
- Go to the directory your downloaded MonoGame
- Open MonoGame.FrameWork
- Choose MonoGame.Framework.WindowsPhone.csproj file
- Click Open
Now you have added the MonoGame framework project to your solution,you need to reference it in your game.
- Open References in your project in solution explorer
- Delete the reference that has a yellow triangle named MonoGame.Framework.WindowsPhone
- Right click on References and choose Add Reference
- Click on Projects
- You should see the MonoGame.Framework project on the list. Tick the box next to it.
- Click OK
Now MonoGame is added and linked to your game project.
Fixing minor issues
At this point, you might notice that the project has issues compiling and running. We need to fix a few minor things and we are set to go.
- In the solution explorer, click on the Show All Files button.
- Find Game1.cs, right click and choose Include in Project
- Open GamePage.xaml
- Open GamePage.xaml.cs
public partial class MainPage : PhoneApplicationPage
public partial class GamePage : PhoneApplicationPage
- Finally open Properties -> WMAppManifest.xml in your game project and change the Navigation Page from MainPage.xaml to GamePage.xaml
At this point, you have a working XAML, XNA compatible, WP8 MonoGame game project that compiles and runs on the emulators and on awesome Nokia WP8 devices. Congratulations!
How about game content?
One of the many awesome features of XNA is its content pipeline. This allowed you to use many of the popular formats (png, jpg, wav, mp3, x and many more) in your games. The XNA content pipeline took all these files and created .xnb files which in turn could be drawn, played or rendered in your games on any of the supported platforms. For MonoGame, I have bad news and good news. The bad news are that a similar content pipeline is not available but the good news is that there is a pretty simple and straightforward workaround. All you have to do is follow these steps.
- Create a new XNA project targeting WP 7.x.
- Add all your content in the content project of the solution.
- Build the solution.
At this point, you will have all the .xnb files ready to be used in your WP8 game. To make things easier and to avoid adding content files and changing their properties all the time, do the following.
- Go to your WP8 project directory and create a new directory called Content.
- Add this directory to your project.
- Copy all your .xnb files from the bin directory of your WP7.x project into the Content directory of your WP8 project.
- Add only one .xnb file in your WP8 project. Any one will do. Save everything.
- Edit the vsproj file of your game project using your favourite text editor.
- Replace the Content tag with the following
<Content Include="Content\**\*.*"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content>
This will automatically add any .xnb file you have in the Content directory in your project and set their properties correctly (basically it sets the Copy to Output to Copy if Newer so you don’t have to for each file). If you add more .xnb files while developing your game, they will automatically be added too. If you feel more adventurous you may want to use file system soft links to create the .xnb files from the WP7.x project directly inside the Content directory of your WP8 project or even run the XNA content processing automatically every time you build your WP8 MonoGame project but I will not cover these here since they are a bit advanced for a “Getting Started” tutorial.
I’ve followed the steps, build my first XNA compatible MonoGame game and ran it. Now what?
Now utilize your XNA skills to make awesome game that can use the new awesome features of WP8. Make freemium games using in app purchases (IAP). Make HD games using 720p or WXGA resolutions. Make multiplayer games using NFC, bluetooth or wi-fi. This tutorial does not aim to teach you XNA, there are awesome tutorials out there for that, since XNA is quite mature at this point. But I hope I helped bootstrapping your first XNA game on Windows Phone 8.
I need help! Where do I go?
You can download the Final Template (which is the same you would get if you followed the steps above) directly from here. Follow the instructions in the ReadMe file to test it out on the emulator or on a real device. You can also download a simple sample WP8 game using XNA and MonoGame featuring the green monster from my hit game MonsterUp Adventures :) This simple game runs in ultra high resolution (all 3 supported from WP8) which is impossible using just XNA for WP7.1 which supports only 800×480(wvga). Get the game directly right here.