Easy multi-platform managment with MonoGame

So MonoGame is a great technology that allows us write common C# code and deploy our games to Windows-Desktop,  Windows-Metro, Windows-Phone, MacOS, Linux, IOS, Android, Ouya and even more. Being able to write portable code is great for us indie developers, though still your code may need some platform specific parts.

With our latest game Frenzied (which can run on Windows, Windows-Metro, Windows Phone 7, Windows Phone 8, Android, MacOS and Linux atm), we introduced our in-house platform-managment concept which we decided to blog about, so that others can also utilize.

Here’s how we are managing multiple-platforms for Frenzied;

First of all we created per-platform solution files;

Solution Files

Each project defines it’s own conditional compilation symbols so we can distinguish them;

Conditional Symbols

Then we defined some enums, the first one is Platforms;

GraphicsAPI enum;

Frameworks enums;

Then we implemented our static PlatformManager class, that will be responsible for identifying current platform and running appropriate code.

To get PlatformManager working we have to make two basic calls, the PlatformManager.Startup() and PlatformManager.Initialize(). Startup() call should be made from Program.cs for platforms that have a Main() entrance point where you run the game.

And in Game.cs (or what-ever you call the Game class) within Initialize() function you have to call PlatformManager.Initialize().

So PlatformManager will be doing all the hard-work for us. Note that how we handle it specific PlatformHandler and PlatformHelpers instances for each platform.

PlatformHandler is the base class that we can extend to implement our platform specific initialization code and so;

Note the PlatformConfig field that allows us to use platform-specific configuration. So you can for example enable mouse for a platform and disable it for mobiles.

PlatformHelper is the base class that we can extend to implement common functionality that needs platform specific code. The best example that comes our mind probably is launching default web-browser.

Let’s start implementing our handlers and helpers for each platforms;

Windows-Desktop

Windows-Metro

 Windows Phone 7

 Windows Phone 8

Android

 IOS

 Linux

 MacOS

That’s all! Now we can;

  • Manage our target platforms all together and have seperate platform specific code.
  • Have platform specific configuration (ie, hiding the mouse for specific platforms).
  • Have platform specific code to run behind scenes to implement common functionality (like opening a given URI in default web-browser).

I hope you enjoyed the article, if so please spread/tweet the word 🙂 If you have any questions please don’t hesitate to send a comment!

Note: I did not implement PlatformHelpers for IOS, Android, MacOS, Linux yet but should be pretty easy to do so!

8 comments

  1. Please publish this code on github and include a permissive license. Otherwise, nobody can use it since you have not explicitly stated a license or terms of use.

  2. @sleepydaddy, I’ve removed the copyright texts, you are free to use the code.

    @Bryan, yes we have seperate csproj files for each platform.

  3. Thanks for the post!

    How are you sharing projects between XNA and MonoGame solutions?  Do you have separate project files for XNA/MonoGame? (I imagine that would be a nightmare to work with when adding/removing files)

  4. Yes, you have to have seperate project files for each target platform. I mostly tend to have development in Desktop platform, and eventually spread progress to other platforms.

  5. How do you create the

    ‘per-platform solution files’

    I understand everything else except how to do that,

    Thanks!

  6. @josh, create new monogame projects in xamarin studio / visual studio and eventually add / link your source files.

Leave a Reply

Your email address will not be published. Required fields are marked *