Getting started with Monogame (XNA) for Android on Windows - Plunge Interactive

Getting started with Monogame (XNA) for Android on Windows

I have started into the game industry making XNA games, that was some years ago… when I become a professional game developer, I have had to abandon the platform, since the current platforms with the biggest user base (iOS and Android) are supposed to work with Objective-C, Java, or C++. So I went to Cocos2d-x with C++. That’s a good solution, and I’m happy with Cocos. I will actually keep working with cocos since I think is the most powerful option to go cross platform. But it’s good to recognize that today we have an alternative, and it is an option that is very important for developers that already know XNA and/or C#, this option is MonoGame, the technology that allows you to make games for multiple platforms using XNA and C#.

MonoGame isn’t new actually, I guess it is out there from several years go, but I think it has reached a point of maturation that worth consideration. The thing is, that you can develop games for Android (and other platforms, but this tutorial is centered on Android), using Windows + Visual Studio 2010 + XNA (based on C#). You don’t have to deal with Java or Eclipse anymore! And the best of all: everything works easily and smoothly, you can easily debug and deploy your games directly on an Android device.

So for me MonoGame is a good option to port games that are already done with XNA or when the developer only knows XNA or C#. In other cases, or for new games, I will keep working with my lovely Cocos2d-x :-) The only negative point is that you need to purchase a license of MonoDroid, that costs $399 (indies license), but I really think it worth the price, and you also have 30 day money back guarantee by Xamarin so… An evaluation version of MonoDroid can be downloaded for free, but in that case you can’t deploy to a real device, you will have to use emulator only (in Android the emulator is horrible, but it’s an option…).

Ok, so let’s start… to get started with MonoGame for Android I recommend you to follow this process:

  1. Install Visual Studio 2010, that can be downloaded for free
  2. Install Microsoft XNA Game Studio.
  3. Install Mono Droid, that costs $399, or get the free evaluation.
  4. Install Mono Game

And that’s it… now just go to Visual Studio and create a new project based on the MonoGame Android Project template. Try to compile it. If you don’t have any compilation error you are done.

If you get a compilation error, go to the references folder and check if any DLL is missing, if that is the case, you might need to download the MonoGame source code from github, and compile it, then reference the brand new DLLs and you are done. Compile, and should work like a charm.

Now you will notice that you have an Assets/Content folder in the project. It contains a spriteFont1.xnb file. The XNB format is a binary format generated by the XNA’s content pipeline. MonoGame currently doesn’t offer a Content Pipeline to generate the XNB files by itself (under development at the time of writing this post), so we need to add a XNA project to the Visual Studio solution. That way we will have a content pipeline project that when compiled, it will generate the XNB files for us.

To make it clear, a XNB file is the binary version of the processed assets of our game, it could be a graphic (jpg, tga or png), sounds (mp3 or wav), spritefonts, etc. So when we compile those files with the content pipeline processor provided by XNA, we get several XNB files.

So this is how my Visual Studio window looks like after adding a XNA project to the solution:

Notice that we have the XNAWindowsGame and XNAWindowsGameContent projects added to our solution. Now add an asset to the XNAWindowsGameContent project, in my case it is cat.jpg, an image that I grabbed from the internet. Then compile the project. Go to the folder that contains the XNAWindowsGameContent project and search for xnb files, you should discover the cat.xnb file.

Ok, now we have the compiled image that can be used in our MonoGame for Android project, so copy the cat.xnb file to the AndroidGame1 project, to the Assets/Content folder. You are now ready to load that asset from the Android project and run it in your device.

You could now replace your Game1.cs class for this code:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AndroidGame1
{
    ///      /// This is the main type for your game ///     
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont font;
        Texture2D cat;
        Vector2 textPosition;
        Vector2 speed;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            Content.RootDirectory = "Content";

            graphics.IsFullScreen = true;
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 480;
            graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
        }

        ///         /// Allows the game to perform any initialization it needs to before starting to run.       /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well.
        protected override void Initialize()
        {
            textPosition = Vector2.Zero;
            speed = new Vector2(1, 1);

            base.Initialize();
        }

        ///          /// LoadContent will be called once per game and is the place to load  /// all of your content. /// 
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            font = Content.Load("spriteFont1");
            cat = Content.Load("cat");
        }

        ///          /// Allows the game to run logic such as updating the world,         /// checking for collisions, gathering input, and playing audio. /// 
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Exit();
            }

            if (textPosition.Y < 0)
            {
                speed.Y *= -1;
            }
            else if (textPosition.Y > graphics.GraphicsDevice.Viewport.Height)
            {
                speed.Y *= -1;
            }

            if (textPosition.X < 0)
            {
                speed.X *= -1;
            }
            else if (textPosition.X > graphics.GraphicsDevice.Viewport.Width)
            {
                speed.X *= -1;
            }

            textPosition += speed;

            base.Update(gameTime);
        }

        ///          /// This is called when the game should draw itself. /// 
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(cat, Vector2.Zero, Color.White);
            spriteBatch.DrawString(font, "Hello World!", textPosition, Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

I don’t have the time to enter into many details, but we are just loading the cat.jpg file, and moving a “Hello World!” text around the screen. Try to run the project with F5 or the “Play” button in Visual Studio, if you connected an Android device and own a license of Mono Droid you should see the game running nicely on your android device, isn’t it nice? :-) If your computer/Visual Studio doesn’t detect your android device, you might need to install the specific driver for that device. In my case I’m using an HTC device so I have had to install HTC Sync.

Feel free to follow us on twitter @plungeint and @jboschaiguade to keep the discussion about game development and have a look at our corporate website  :-)

This site uses cookies to store information on your computer. Some cookies on this site are essential, and the site won't work as expected without them. Read more about them

ACCEPT