Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon


Home > >

9. June 2015

 

In this tutorial we are going to look at getting a MonoGame development environment up and running on MacOS, create and run our first MonoGame application and finally take a quick look at the Content Pipeline tool.

 

For those that prefer video, there is a video version of this tutorial available here or embedded below.

 

The Installation Process

In order to get up and running there are a couple things you need to install, Mono itself then MonoGame.  We will also be installing Xamarin Platform, which includes Xamarin’s cross platform Mono based IDE Xamarin Studio.  It is not strictly required, but will make your life a great deal simpler.

 

The process is as straight forward as installs can get, simple download an run each installer.  There is not ambiguity to the install, however the install order is extremely important.  Download the following in this order.  In each case be sure to install the most recent version if given an option.

 
As I said earlier, there should be nothing complicated about any of those installs, except the order.  If you install out of order the proper templates for MonoGame won’t get added to Xamarin Studio.
 
 

Our First Project

Now let’s fire up Xamarin Studio and create our first application.  Once Xamarin loads, select New Solution...
 
Ss1
 
 
In the resulting dialog, next select the project template to get started.  Along the left hand side scroll down and locate Other->Miscellaneous. On the right hand side you should see a number of preconfigured MonoGame templates.  For this example we are going to target only Mac OS using Xamarin’s Mac libraries, so select MonoGame Mac Application (Xamarin.Mac), then click Next.
 
Ss2
 
 
All that remains is to pick a location and name for your application as well as if you want Git control or not.  When ready click Create.
Ss3
 
 
This should create a simple application for us with the following code:
#region Using Statements
using System;

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

#endregion

namespace HelloMonoGame
{
  /// <summary>
  /// This is the main type for your game.
  /// </summary>
  public class Game1 : Game
  {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public Game1 ()
    {
      graphics = new GraphicsDeviceManager (this);
      Content.RootDirectory = "Content";
      graphics.IsFullScreen = true;
    }

    /// <summary>
    /// 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.
    /// </summary>
    protected override void Initialize ()
    {
      // TODO: Add your initialization logic here
      base.Initialize ();
        
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to 
    load
    /// all of your content.
    /// </summary>
    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 
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.<
                    /param>
    protected override void Update (GameTime gameTime)
    {
      // For Mobile devices, this logic will close the Game when the 
      Back button is pressed
      // Exit() is obsolete on iOS
      #if !__IOS__
      if (GamePad.GetState (PlayerIndex.One).Buttons.Back == 
          ButtonState.Pressed ||
          Keyboard.GetState ().IsKeyDown (Keys.Escape)) {
        Exit ();
      }
      #endif
      // TODO: Add your update logic here 
      base.Update (gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.<
                    /param>
    protected override void Draw (GameTime gameTime)
    {
      graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
    
      //TODO: Add your drawing code here
            
      base.Draw (gameTime);
    }
  }
}

 
We aren’t going to go into any specifics of what this code does, not yet anyways, but we will make sure that it runs.  Hit the Play icon in the top control bar:
 
Ss4 
 
You should now see a full screen cornflower blue window.  If so, congratulations, you just successfully made your first Monogame application!
 

The Content Pipeline

 

The content pipeline was a major component missing from MonoGame, but they have recently implemented one.  The Content Pipeline takes external assets for your game, be them videos, textures, sounds, fonts, etc…  converts them for your platform of choice, generating a XNB file, a binary format that XNA used internally.  Let’s take a look at the process in MonoGame.

 

If you look under the Content folder of your Solution, you will see a file called Content.mgcb. 

Ss5

 

Double click this and the Content Pipeline tool will load:

Ss6

 

This is the tool you use to assemble the assets your game is going to use.  Let’s take a quick example of importing a texture.

 

First select Edit->Add->Existing Item...

Ss7

 

Browse to the image you wish to import as a texture then select OK.

Screen Shot 2015 06 09 at 12 35 35 PM

 

Now you will be asked how you wish to import the file.  I want a copy to be made so the original is unaffected.

Screen Shot 2015 06 09 at 12 36 41 PM

 

Your texture should now be added:

Screen Shot 2015 06 09 at 12 37 09 PM

 

You now have a few options about how the content will be processed on import.  With the image selected, notice the options available below it:

Screen Shot 2015 06 09 at 12 38 17 PM

 

Each different importer has a different set of parameters you can modify.  Here you can set the automatic generation of mip maps, resize to power of 2 dimensions, change the compression settings, etc.

 

By selecting the Importer drop down you can get an idea of the various type of assets supported by the Content Pipeline:

Screen Shot 2015 06 09 at 12 40 04 PM

 

Now select the Content node and the properties available will change again.  This time we simply want to change the target platform to MacOS:

Screen Shot 2015 06 09 at 12 41 11 PM

 

You can have the Content Pipeline automatically process content for a variety of different platforms.  Now that this task is done let’s actually build our content.  Simply select Build->Build or hit F6:

Screen Shot 2015 06 09 at 12 42 08 PM

 

In the directory [Content]/bin/MacOSX there will now be an XNB file available for use.  Let’s add it to our project.

 

In Xamarin Studio, right click the Content folder and select Add->Add Files...

Screen Shot 2015 06 09 at 12 44 15 PM

 

Navigate to the XNB file in Content/bin/MacOSX that you just created and select it.  Then select Override Build action and choose ‘Content'

Screen Shot 2015 06 09 at 12 46 11 PM

 

This time when prompted how to add the file, choose the Link option:

Screen Shot 2015 06 09 at 12 47 57 PM

 

Congratulations, you have now added an asset to your game.  In future tutorials we will look at using them in more detail.

The Video Version

, , ,

blog comments powered by Disqus

Month List

Popular Comments