XNA Games On Windows Phone 8 with Monogame
This tutorial shows how to use the MonoGame open source project to create XNA games on Windows Phone 8.
Article Metadata
Code Example
Tested with
Compatibility
Article
Contents |
Introduction
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. 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.
Unfortunately, Microsoft has deprecated support for XNA on the Windows Phone 8 platform (as well as on Windows 8 Store apps). 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 use any of the awesome new features WP8 brought to the table.
MonoGame allows developers to target Windows Phone 8 while still using XNA and C#, our favorite 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 Windows Phone 8. An amazing aspect of MonoGame is that it opens the doors not only to be able to continue to use XNA, but also to port your games to other platforms.
Getting started
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. If you as many developers are not yet ready to install Windows 8 on your production machine, you can consider the possibility to install it on a Virtual Machine. If that's the case, take a look at this article. 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, or
- 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
- Change to
x:Class="XamlPhoneGame1.MainPage
x:Class="XamlPhoneGame1.GamePage
- Open GamePage.xaml.cs
- Change to
public partial class MainPage : PhoneApplicationPage
andpublic partial class GamePage : PhoneApplicationPage
topublic MainPage()
public GamePage()
- 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 favorite 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?
MonoGame has a very active community. The Windows Phone discussion of MonoGame can be found here. The official pages of the MonoGame projects are here and here.
Files
You can download the Final Template (which is the same you would get if you followed the steps above) from below. 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 800x480(wvga). Get the game from below also.



Contents
Hamishwillee - Review/feedback
Hi kariosgames
There are some minor grammatical errors, but this is pretty interesting. I'm sure that a lot of developers probably do want to extend their WP7 XNA apps with new features of WP8, and this is one way to do it.
In terms of "Room for improvement", I would love to see a basic game example provided (buildable, in a zip) which demonstrates the addition of a new WP8 feature to an XNA game. If not, maybe just the "basic template project" you created above, so that people can run it "out of the box".
Thank you for this contribution - I hope the judges also value it highly.
Regards
Hamishhamishwillee 07:37, 14 December 2012 (EET)
Hamishwillee - Subedited for wiki style
Just FYI, did a basic style edit. Still think your article would be improved with a buildable example showing the "key benefit" - using WP8 features in your XNA app.hamishwillee 02:21, 19 December 2012 (EET)
Kariosgames - Thanx
Thank you so much for the comments and the edits. I could not decide which one to do, so I did both :) I have a final template ready to download and test as well as a simple game that is running in HD resolution in WP8, which is one of the most requested feature for Windows Phone games. I've added links for both at the end of the articles.
I hope this makes the article even more useful for the community.kariosgames 03:25, 25 December 2012 (EET)
Panickev - Error
I downloaded the sample game en when I open it in Visual Studio 2012 and try to run it it says "The referenced project 'D:\Github\MonoGame\MonoGame.Framework\MonoGame.Framework.WindowsPhone.csproj' does not exist."
I tried solving this by copying the Monogame.Framwork Folder into the MonoGame folder. Then I reloaded the MonoGame.Framework.WindowsPhone project but the error keeps coming up. Do you have any idea of what I'm doing wrong?Panickev 03:24, 28 January 2013 (EET)
Kariosgames - Hmm
It should work. All you need is to have the project loaded in the solution and referenced in your game's project. Alternatively, you could compile the MonoGame Windows Phone project and reference the library instead in your game.kariosgames 17:36, 29 January 2013 (EET)
Panickev - Other Error :P
I tried referring to my game from the MonoGame Windows Phone Project but then it says that my game can't find the Microsoft.XNA.Framework and Microsoft.XNA.Framework.GameServices. I tried to add the reference from: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\WindowsPhone\v8.0\Microsoft.Xna.Framework.dll, but it still says It can't find it. Any Ideas?Panickev 20:28, 31 January 2013 (EET)
Kariosgames - XNA
The point of using MonoGame is to use it instead of XNA, so the reference you used is wrong:) What you have to do is refer MonoGame from your game, not the other way around. The namespace will then be available (MonoGame uses the exact same namespace as XNA, with Microsoft's blessings) so your game project should be able to find it. Alternatively, you can compile MonoGame and then reference the resulting DLL from your game, so you don't have to include the whole MonoGame project in your solution. This is a simpler solution, but everytime there are fixes in MonoGame (and there are quite often) you will have to re-compile the MonoGame project.kariosgames 21:24, 31 January 2013 (EET)
Panickev - XNA
Thank you for your help :) I created a new project (named Test) and folowed the steps one more time and now I'm at the point where the project launches with no errors. But when i try to use: int screenWidth = _graphics.GraphicsDevice.Viewport.Width; i get this error: An exception of type 'System.NullReferenceException' occurred in Test.DLL but was not handled in user code Is there still a refference that I'm missing?
I also have a problem with the content. I've added the folder Content (wich has a folder in it called tiles and that one has a file in it named 1.xnb) in the directory wich also has the Test.csproj file. Then i changed the Content tag in the Test.csproj file. But when i try to loadthe content with the code: Texture2D t = Content.Load<Texture2D>("tiles/1");
I get this error: An exception of type 'Microsoft.Xna.Framework.Content.ContentLoadException' occurred in MonoGame.Framework.DLL but was not handled in user codePanickev 02:32, 1 February 2013 (EET)
Kariosgames - Content
For your error, I am guessing that the _graphics object has not be initialized at the point you are trying to get the graphicsdevice, thus the null exception. I would need to see more of your code to know what's wrong. What I can tell you is that the Viewport of the Graphics device has been implemented and works as expected in MonoGame normally (line 1451 on the current GraphicsDevice.cs file) Regarding your content issues, make sure you have done the following: -Content must be compiled through a "normal" XNA project beforehand, you can only use the resulting XNB file in your WP8 MonoGame project (I guess you've done that already). -Content must be in the Content directory which is added to your main project, not a separate content project as is normal for XNA solutions. -The build action for content must be sent to "Content" and not "Compile"
-The Copy to Output Directory must be set to "Copy if newer" or "Copy always"kariosgames 16:06, 1 February 2013 (EET)
Cyber k9 -
Hi; first of all, thank you for the tutorial - I found the last build of MonoGame had many of the "minor issues" already taken care of, but this article was still a great help! :)
I've run into a problem, though, and was hoping you could provide some more guidance.
A sample XNA project I was working on in WP7 can catch the "Back" button with:
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) {
System.Diagnostics.Debug.WriteLine("Back pressed!"); this.Exit();}
My hope was I'd be able to do the same - ultimately catch the "Back" button to pause a game, return to the main screen, then exit... but right now, I always get "The name 'GamePad' does not exist in the current context". I can't seem to find any documentation on how to do the same in MonoGame on WP8.
(Similarly, information on handle touch events would be helpful - but I think I can do that with "TouchCollection"? - it's the Back-button handling I'm fighting with at the moment).
I'd appreciate the help!
Thanks. :)cyber_k9 09:31, 3 February 2013 (EET)
Kariosgames - Gamepad
For these kind of issues we are using the git system and discussions. The particular problem you mention has not been pulled to the main repository yet, but has been solved here: https://github.com/mono/MonoGame/pull/1109 (essentially Nezz created a gamepad class just to solve the back button compatibility situation). If you are in a hurry, you can get the file here: (https://github.com/Nezz/MonoGame/blob/b8bc43d0af2de9f8b0522d26d70288ec3f93b42a/MonoGame.Framework/WindowsPhone/GamePad.cs). If not, you can wait until it gets pulled into the develop3d branch.
In general I suggest that you monitor these discussions (https://github.com/mono/MonoGame/pulls) and even participate to help solve these kind of issues for everyone.kariosgames 14:12, 3 February 2013 (EET)
Cyber k9 -
Thanks kariosgames;
I was able to download the GamePad.cs file, put it in the right folder (and added it to the MonoGame.Framework.WindowsPhone project in my solution), but unfortunately I'm still getting that error. (in fact, I downloaded Nezz's whole branch and used that). It's odd. I tried a Rebuild of the MonoGame.Framework.WindowsPhone project, thinking maybe it needed that before the GamePad class would be available, but it throws errors (RoutedEventArgs could not be found" in "MediaPlayer.cs", for example).
If you have any further pointers on it, I'd appreciate it. However, I will search through the discussions on GitHub that you mentioned and may pose the question there shortly if I can't solve it. Thank you for pointing me in the right direction! In the meantime, there's certainly other stuff I can tackle on the game itself(!). :)
Thanks again.cyber_k9 00:30, 4 February 2013 (EET)
Kariosgames - WP8 Working
Hey guys! Managed to get a WP8 only game on the Marketplace using MonoGame. I had to use this: http://monogame.codeplex.com/discussions/439446 which has everything working correctly, and the game I published is MonsterUp Memory with HD graphics (available for free here: http://www.monsterupgame.com/app/monsterup-memory/). I guess at some point these changes will be merged in the original MonoGame github repository as well.
I hope this helps!kariosgames 17:39, 9 May 2013 (EEST)
Hamishwillee - Kariosgames - great job!
Thanks for sharing your success.hamishwillee 08:51, 14 May 2013 (EEST)