XNA on Linux and Mac withMonoGame

A slightly updated version of this tutorial is available (MonoGame 2.5.1).

XNA is a great development framework that lets you create games in C# for Windows, Xbox 360 and Windows Phone 7. A drawback of this framework is that it does not provide support for other platforms. This issue may be about to vanish thanks to MonoGame (and soon ExEn), a portable and open source framework replicating XNA and running on multiple platforms thanks to the Mono and/or .NET runtimes. Thanks to MonoGame, you will be able to develop games for:

The good thing about MonoGame is that it uses the same namespaces XNA uses. Actually, it's a total rewrite of the Microsoft framework so that it works with OpenGL. In theory, an XNA code sample may be recompiled directly with MonoGame without any modification (only the project references need to be modified). Nonetheless, proceed with caution, as for now there is only a 2D support as well as a partial 3D support (ever since version 2.1). It might also be lacking some elements like Sound (which is not totally supported), networking (another networking layer has been implemented for Microsoft Live service is proprietary), etc...

Prerequisites

Setting up a MonoGame development environment is both simple and complex at the same time... The main issue lies in the many dependencies the framework requires in order to run. Indeed, MonoGame needs:

Tao.Sdl.dll (which requires the native SDL library)

OpenTK.dll (which requires OpenGL in order to work, as well as OpenAL)

Libgren.Network

That and of course the Mono runtime and its development packages (except for Windows, on which the .NET framework will/can be used.Once all dependencies installed, a base project can easily be set up. On Linux or Mac, MonoDevelopcan be used to provide practical tools such as completion, compiling with a single click. On Windows, you can use Visual Studio, MonoDevelop or SharpDevelop.

Compiling MonoGame

The first thing to do is compiling MonoGame for your platform. The procedure I will be detailing works on both Windows and Linux (and probably Mac, but I don't have one).

MonoGame is released as an archive, thus it first needs to be extracted. Once done, the same must be done with OpenTK (which is released already compiled). I advise using a nightly build of OpenTK (follow the link I provided). When everything is downloaded and extracted, open up the MonoGame solution that's best for you.The issue you are going to encounter is that the OpendTK reference is missing and you simply need to replace it with the OpenTK version you just extracted. The procedure in pictures:

The missing reference is to be deleted.

Use the OpenTK version you just downloaded.

Proceed with caution, using MonoDevelop you need to click "add" so that the reference be added to the solution. Now we only need to build MonoGame. I advise you to build it in release mode (debug mode isn't required in this particular case, since we won't be debugging the MonoGame library).Now let's move on to the bin/Release folder where you will find 6 .dll files that need to be added to the game project solution.GamepadBridge.dll, GamepadConfigControls.dll and Tao.Sdl.dll are used for joystick and Xbox 360 game pad management.Lidgren.Network.dll provides some networking features.MonoGame.Framwork.{Linux/Windows/Mac}.dll is the .dll that will make it possible for your game to run on Linux, Windows, iOS and Android.OpenTK.dll is the library that makes it possible to use OpenGL with C# and it is used by MonoGame to handle display (among other things).

Creating a standard project

There is nothing more simple than creating a basic project. First of all, create a new "console" project, then add the following references:

GamepadBridge.dll (provided by MonoGame)

GamepadConfigControls.dll (provided by MonoGame)

Lidgren.Network.dll (provided by MonoGame)

MonoGame.Framwork.{Linux/Windows/Mac}.dll

OpenTK.dll

Tao.Sdl.dll

System (provided by Microsoft/Mono by default)

The good practice consists in creating a folder called "externals" at your project root level, containing all .dll files, and then adding them through your IDE reference editor.The next step consists in creating 2 C# classes, i.e. 2 files. We shall proceed as Microsoft and and create:

As you can see, that's exactly the same content that can be found in the base file generated by Visual Studio when creating a new XNA game project.The class inherits from Game and can be seen as the very core of the game. It contains several methods that we will study more in depth now.The Initialize() method initializes your objects.The LoadContent() method is used in order to load your content. The content may be sound, textures, XML files, and so on... All this is stored in the Content folder that needs to be placed on the same folder as your game installer. It doesn't exist by default and you will need to create it after compiling (placing your resources inside, of course).The UnloadContent() method is called when the game exits. That's when all the resources that stem from your objects (graphics, sound, etc...) will be freed.The Update(GameTime gameTime) method will be used to run your game logic. Typically, that's where we'll run tests about character movement for instance. This method is called in a loop.And finally, the Draw(GameTime gameTime) method is called in a loop as well (right after Update) and that's where everything visual about the game (sprites, 3D models, effects, etc...) shall be displayed.