Skip to content

Tutorial: "Hello, World"

Roman Shapiro edited this page Apr 6, 2017 · 17 revisions

Table of Contents

Default Font

Custom Font

Default Font

  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.

Note. Myra doesnt use MonoGame's SpriteFont due to it's closeness. I.e. you can't create SpriteFont in the runtime, but it has to pass the Content Pipeline. While one of the Myra's design principles is to avoid the Content Pipeline as much as possible and try to load everything during the runtime.

  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. Later I'll show how to load a custom font. But for now we'll use default one.

  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:

Custom Font

  1. Myra can load only fonts in AngelCode BMFont FNT format. So run AngelCode BMFont and select 'Mistral' font and mark "Latin + Latin Supplement":

  2. Open "Export Options" and make sure Font descriptor is set to Text.

Note. Right now, Myra can load only Text version of FNT. And only with single image file. Hopefully those constraints will be removed in future.

  1. Save it. It should generate two files: minstral.fnt and mistral_0.png(or tga depending on image output type).

  2. Add generated files to the project. The easiest way to do so is to create folder Assets in the project. Then copy generated files there. Finally add files to the project with "Build Action" set to "None" and with "Copy to Output Directory" set to "Copy if newer".

  3. Create the asset manager. Add following field to your game class:

private readonly AssetManager _assetManager = new AssetManager(new FileAssetResolver("Assets"));
  1. Change LoadContent method. It should have following code now:
protected override void LoadContent()
{
  base.LoadContent();

  MyraEnvironment.Game = this;

  _batch = new SpriteBatch(GraphicsDevice);
  _font = _assetManager.Load<BitmapFont>("mistral.fnt");
}
  1. Run the application and you should see following result:

The full sample code is available here: https://github.com/rds1983/Myra/blob/master/Source/Myra.Samples/CustomFontSample.cs

Clone this wiki locally