In this blog I’m going to cover MonoGame development for Windows8 apps
(Metro apps).
What is Monogame
Monogame
is a component used by game developers to make
their games. Monogame is open source
implementation of Microsoft XNA 4 Framework. It can target many more platforms,
including Android, iOS, Linux, Mac, and Windows 8. On the other hand, its
development is a bit more fragmented and there are some bugs and some feature
not available. Overall, it's a pretty good project, and a pretty good
alternative to XNA.
(Microsoft XNA is a set of tools with a
managed runtime environment provided by Microsoft
that facilitates game development and management. XNA is based on the .NET
Framework .It can target several platforms like “Windows”,”
Windows Phone
“and the” Xbox”)
Game Life Cycle
A running game usually goes through the
following steps:
- Initialization/Load – Sets default and preliminary values to the game, queries and initializes user-based information, loads graphic and non-graphic contents, et cetera.
- Game Loop – Performs in-game repeating logic and layout calculations and render.
- Unload – Saves current state, releases and unloads contents, et cetera.
XNA provides a skeleton that is valid for
any type of game. The skeleton is implemented within the Game class, which is a part of the Microsoft.Xna.Framework
namespace. Once inherited, the Game
class provides the complete game life cycle, enabling the programmer to
override the required methods in order to add specific game logic.
Monogame Loop

Game Class Methods (Monogame Loop)
The Game
class contains, among other things, virtual methods that cover the various
aspects of the listed game steps. An inherited Game class may override some or all of these methods, and define a
constructor as described below.
·
Class constructor – Used to instantiate and set
default values to required elements. For example, instantiate the graphics
device manager, define the game frame rate, et cetera.
·
Initialize- Sets default and
preliminary values to the game shell, queries and initializes user-based
information, et cetera.
·
LoadContent- Loads all graphics and
other content required to run the game. For example, LoadContent to load game component as an object like background
images, sound, and so on.
·
Update- Performs on-going game
logic: collects input information from the various input devices. Calculates
current positions, collisions and states; plays audio, and so on.
·
Draw- Draws the current view of
the game: backgrounds, sprites, et cetera.
·
UnloadContent- Unloads all game content
and content managers.
The Update and Draw methods are called repeatedly by
the XNA Framework¾not necessarily in sequence, but multiple times every second¾according
to the frame rate defined in the initialization phase
Each pass through the game loop is called a frame. XNA games on
Windows and Xbox 360 target 60 frames per second (fps), and Windows Phone games
run at 30 fps.
Preparing the System
Before moving to the setup of MonoGame, first we need to install the
following
- Install OS Windows 8 or 8.1 Release Preview
- Install Visual Studio 2012 Express for Windows 8 or Visual Studio 2012 Express for Windows Phone
- Download and install monogame installer from the flowing path http://monogame.codeplex.com/releases/view/102870
- Launch Visual Studio 2012 for Windows 8 (or Visual Studio 2012 if you have Pro or above) and create a New Project.
- Under Visual C#, you should now have MonoGame template(s). Pick the Game template and click OK.
- Press F5 to compile and run the project. If you see a Cornflower Blue screen, you have successfully set up MonoGame.
2D game Example
Initialize Game Class
Constructor
Initializes a new instance of this class,
which provides basic graphics device initialization, define the game frame
rate, et cetera.
/// to handle the
configuration and management of the graphics device.
GraphicsDeviceManager
_graphics;
/// to handle
Texture objects and write the position
SpriteBatch
_spriteBatch;
// This is a texture we can render.
Texture2D _sourcePlan;
// This is a background texture we can render.
Texture2D _background;
// To set the default position of the Plane
Vector2 sourcePlane
= new Vector2(0, 0);
/// <summary>
/// initializes
a new instance of this class, to handle the configuration and management of the
graphics device.
/// </summary>
public Game1()
{
// Initialize the GraphicsDeviceManager
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// to set the default position of the Plane
sourcePlane = new Vector2(0, 0);
}
Initialize()
The Initializemethod
automatically enumerate through any game components that has been added to Game. Components and call their Initializemethods.
We can initialize any game assets
protected
override void Initialize()
{
base.Initialize();
}
LoadContent()
This
method is called by Initialize.
Also, it is called any time the game content needs to be reloaded, such as when
the DeviceReset
event occurs. You should not access the GraphicsDevice
until LoadContent
is called.
protectedoverridevoid LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
_spriteBatch = new SpriteBatch(GraphicsDevice);
_background = Content.Load<Texture2D>("air_strike");
_sourcePlane = Content.Load<Texture2D>("Plane3");
}
Before start your
projects we need to convert game assets file like Images, effects and sprites as an .xnb file.
Monogame doesn’t provide the option to convert the file as an .xnb. So we need
to use XNAContentCompiler for compile the game assets to .Xnb file
XNA Framework calls Game.UnloadContent() is
when your game is closing down. The only time it calls
GameComponent.UnloadContent()is where any game assets can be released. You do
not need to unload content managed by a ContentManager (such as Models and
Effects)
Update ()
The Update loop is the best place to
update your game logic: move objects around, take player input, and decide the
outcome of collisions between objects
protectedvirtualvoid Update (GameTime gameTime)
{
UpdateSprite(gameTime);
base.Update(gameTime);
}
Create new method and adds a little bit of logic that will move the
sprite around each frame and cause the sprite to change direction if it hits
the edges of the game window.
void
UpdateSprite(GameTime gameTime)
{
KeyboardState
keyboardState = Keyboard.GetState();
if
(keyboardState.IsKeyDown(Keys.Up))
{
if(sourcePlane.Y
> 0)
sourcePlane.Y -= (float)(30 *
3.0f * gameTime.ElapsedGameTime.TotalSeconds);
}
else if
(keyboardState.IsKeyDown(Keys.Down))
{
if
(sourcePlane.Y < Window.ClientBounds.Height)
sourcePlane.Y += (float)(30 *
3.0f * gameTime.ElapsedGameTime.TotalSeconds);
}
else if
(keyboardState.IsKeyDown(Keys.Left))
{
if
(sourcePlane.X > 0)
sourcePlane.X -= (float)(30 *
3.0f * gameTime.ElapsedGameTime.TotalSeconds);
}
else if
(keyboardState.IsKeyDown(Keys.Right))
{
if
(sourcePlane.X < Window.ClientBounds.Width)
sourcePlane.X += (float)(30 *
3.0f * gameTime.ElapsedGameTime.TotalSeconds);
}}
Draw()
protectedoverridevoid Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
_spriteBatch.Draw(_background, new Rectangle(0, 0,
Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White);
_spriteBatch.Draw(_sourcePlane, new Rectangle((int)sourcePlane.X,
(int)sourcePlane.Y, 80, 80),
new Rectangle(0, 0,
_sourcePlane.Width, _sourcePlane.Height), Color.White,
0f,
new Vector2(_sourcePlane.Width
/ 2, _sourcePlane.Width / 2), SpriteEffects.None, 0);
_spriteBatch.End();
base.Draw(gameTime);
}
This code draws the sprite on the screen each frame.
Notice the parameter passed by the Begin method, BlendState.AlphaBlend. This parameter tells the Draw method to use the alpha
channel of the source color to create a transparency effect so that the
destination color appears through the source color.