Old Post - Scaler

Old Post - Scaler

Monogame

So I thought I'd start this blog off with a little bit about MonoGame. Its been a few months since I've used it consistently, but I thought I'd talk about it anyway. For part of a university project me and a friend decided to use the MonoGame framework, which at the time we fought with a fair bit, but in reality it was actually pretty good for desktop development. The MonoGame framework has been put together by a set of lovely contributors, to replace the now dead XNA framework, and it does a pretty good job of this. The timing of the project was a bit off when we started, this was just after Microsoft had abandoned the XNA framework and MonoGame was still rather early in its life, it was missing a lot of features which were needed to make the whole process easy.

The main feature that was missing at the time was the Content Pipeline from XNA. The page I linked to has the image i'm putting in below, but for mobile viewers i'll stick it here anyway too.

Content Pipeline

Image

The pipeline allowed developers to pre process their assets for a game, such as HLSL shaders, images, sounds, fonts, these would all be processed into .xnb intermediate format. The image above explains this process, its not that simple, and it makes sense that it would take a while for the MonoGame framework to implement it in a stable way. The point of this is so that when the asset is loaded it won't need to be processed at run time into a format which the game engine can understand. I'm not going to go into this feature too much during this post, if there is interest then I might post a bit more about the content pipeline as I convert my old game to use it. I'm only bringing this up as one of the main reasons I've moved away from using MonoGame was the bad taste left from the process which used to be pre processing assets into MonoGame, this was necessary as elements such as fonts had to be pre processed. Back when I used the framework this required you have the XNA framework and visual studio 2010 installed, using these it was possible to pre process the asset with XNA, then import it to MonoGame, this was a bit frustrating.

Anyway I digress. The MonoGame framework now supports a form of a Content Pipeline, it is still early in its development, but is coming along nicely. Heres some documentation of the tool if you're interested. I'll hopefully write a blog post on how to use this if people are interested, there are a fair few topics i'd like to talk about at this stage, but I'll get to them when I get to them, for now i'm just giving a high level overview of the framework.

I've put together a really simple little MonoGame project so that you can check it out, to get this going simply grab MonoGame (you can do this in VS2013 with NuGet, that'll get you everything you need to create a new MonoGame OpenGL project). The code below simply creates a little sprite, moving across the screen until it reaches the bottom (like a really rubbish physics engine, the little favicon is being thrown across the screen until it hits the ground). I'll supply the sample at the end. Hopefully this can help you get going in MonoGame, its not that complicated, although theres a few gotchas. I'll probably go through these at some stage as well, as even having used MonoGame for six months solid I still get caught by some of the weird elements of it.

class Main : Game {
	private readonly GraphicsDeviceManager _graphics;
	private SpriteBatch _spriteBatch;
	private readonly Entity _entity = new Entity();

    public Main()
    {
        _graphics = new GraphicsDeviceManager(this) {PreferredBackBufferWidth = 800, PreferredBackBufferHeight = 480};
    }

	protected override void LoadContent()
    {
        // set the location of the content
        Content.RootDirectory = "Content";
	    // Load assets
		_spriteBatch = new SpriteBatch(_graphics.GraphicsDevice);
        _entity.LoadContent(Content);
	}
	
    protected override void Update(GameTime gameTime)
	{
	    // Update cycle of the game
		_entity.Update(gameTime.ElapsedGameTime.Milliseconds);
	}
	
	protected override void Draw(GameTime gameTime)
	{
        _graphics.GraphicsDevice.Clear(Color.Black);
	    // Draw cycle of the game
		_spriteBatch.Begin();
		// Put the elements of your game to be drawn here
		_entity.Draw(_spriteBatch);
		_spriteBatch.End();
	}
}

class Entity {
	private Vector2 _position;
	private Texture2D _texture;
	private float _xSpeed;
	private float _timeDiff;
    public void LoadContent(ContentManager content)
    {
        _position = new Vector2(0, 0);
	    _texture = content.Load<Texture2D>("sprite.png");
        _xSpeed = 3;
    }

	public void Update(float timeDifference)
	{
		_timeDiff -= timeDifference;
		if (_timeDiff >= 100) return;
		_position.X += _xSpeed;
		if (_position.Y < 480 - _texture.Height)
		    _position.Y += 2;
		_timeDiff = 100;
		if (_xSpeed > 0)
		    _xSpeed -= (timeDifference/1000);
	}

	public void Draw(SpriteBatch spriteBatch)
	{
	    spriteBatch.Draw(_texture, _position, Color.White);
	}
}

So, I'll just do a quick run through. We have our Main class, which inherits from the Game class supplied by XNA/MonoGame. This is super simple and in my opinion pretty awesome, you can overload any functions from the Game class, and in turn the game loop created by MonoGame will call them (such as Update, LoadContent and Draw) when I post again i'll talk about how the game loop works and such, but for now just take my word for it, when creating a new MonoGame project this will be handled in the background.

The first thing to look at is the constructor, in this cause I have handled the GraphicsDeviceManager, this looks after your graphics hardware, the benefit of MonoGame is that you will not need to worry about this, other than you need to do this in your constructor. I've set the backBuffer height and width, this is just the size of the window. Here we could also set whether the game is fullscreen by setting graphics.IsFullScreen to true.

We then have the LoadContent, this configures the root directory from which our assets will be accessed (the Content directory) and our spritebatch. The spritebatch handles drawing of objects to the screen, again I will talk about this in more detail at some point, but for now I'll just leave it at this handles the most efficient way to render textures in the background for you.

We then have the Update and Draw functions. Oddly enough these do what they say on the tin, Update will be called before Draw and can be used to handle game logic, Draw will handle rendering textures. In the sample I've used the GameTime to decide what speed the sprite moves at (in a bad way, which I will explain later in a post about delta time movement, you shouldn't move by a set number of pixels!). Each frame Update is called a check will be carried out to see if the position of the sprite should be updated. In the Draw function we have a clear, this clears the graphics buffer and makes sure there is nothing left from the previous Draw call. We then have the begin and end, when I talk about spritebatchs I'll explain these, but for now just know you should render after the begin and before the end.

We then have a little simple Entity, this is just a little sprite class with a sprite and texture. I'll do a more complex example with a little particle engine at some stage, but for now this is a super simple little entity, we can see in the LoadContent it loads a texture using the content.Load function (content.Load can be used in a generic way to load lots of different assets) and we can see in the Draw function the spritebatch.Draw function being called, this is quite a complex bugger of a function, so I'll go into detail at some stage about it, but for now I've shown this in its simplest form, with position and texture.

So that's how you get started! I'm going to go into a bit more detail at some other stage, but for now I'll show you what me and a friend were able to make with MonoGame. I'll just attach a few images as I'm still working on the Game (going to add a proper lighting engine now that putting shaders into the game is a lot easier, again I'll talk about this in a later post). The game is called Scaler, and at some stage I'll likely upload an alpha if people want to play with it, I'm also working on an Android version, but this was set back initially as MonoGame has a dependency on Xamarian for mobile development, and I didn't have a license (however there is now a student license!).

Scaler-1

Scaler-2

Scaler-3

Scaler-4

Scaler-5

There used to be a download here for the game, but its not gonna run well on Windows 10 since this was written around 4 years ago, so I'm not gonna supply it anymore, email me if you really want to try it!

Michael Drury

View Comments
Navigation