Skip to content

Tutorial: "Hello, World"

Roman Shapiro edited this page Jul 15, 2017 · 17 revisions
  1. Create MonoGame project.
  2. Add reference to the Myra.
  3. Add following fields to your Game class:
private SpriteBatch _batch;
private BitmapFont _font;

BitmapFont is MonoGame.Extended's class for the font storage and text rendering.

  1. Override LoadContext method of your Game class and add the following code:
protected override void LoadContent()
{
  base.LoadContent();

  MyraEnvironment.Game = this;
  Window.AllowUserResizing = true;
  		
  _batch = new SpriteBatch(GraphicsDevice);
  _font = DefaultAssets.Font;
}

The line MyraEnvironment.Game = this; should be called before accessing any Myra functionaly.

Note. Myra assumes that there's only one Game during the entire game cycle. Therefore Myra stores it as singleton and accesses all its services(such as GraphicsDevice).

In the line _font = DefaultAssets.Font; we initialize our font reference by the font from Myra's default assets.

  1. Override Draw method and add the following code:
protected override void Draw(GameTime gameTime)
{
  base.Draw(gameTime);

  var device = GraphicsDevice;
  device.Clear(Color.Black);

  _batch.Begin();

  _batch.DrawString(_font, "Hello, World!", Vector2.Zero, Color.White);

  _batch.End();
}
  1. Run the application and observe the result:

Clone this wiki locally