Game Development Community

Writing Text

by Charles "monkeyboy" Gibson · in Torque X 2D · 12/15/2008 (8:19 am) · 3 replies

Does anyone know of any resources to learn to write text to the screen? Thanks.

#1
12/15/2008 (4:23 pm)
There are a couple of ways to get text on the screen: Creating a GUI Screen that has a GUIText control and simply drawing the text in a draw call for each frame. The first is the best way to do it, for game HUD (like score, etc) because you can move it around the screen as a screen object. The second approach is a great "quick&dirty" method to throw up some debug info (like framerate, object state, etc.).

In a nutshell, the first option requires you to create your own screen...

public class GuiPlay : GUISceneview, IGUIScreen
{
}

then, create a text style...

GUITextStyle textStyle = new GUITextStyle();
textStyle.FontType = "arial22";
textStyle.TextColor[0] = Color.White;
textStyle.SizeToText = false;
textStyle.Alignment = TextAlignment.JustifyCenter;

then, create your text...

GUIText _playerScoreLabel = new GUIText();
_playerScoreLabel.Style = textStyleLarge;
_playerScoreLabel.Position = new Vector2(975, 100);
_playerScoreLabel.Size = new Vector2(200, 70);
_playerScoreLabel.Visible = true;
_playerScoreLabel.Folder = this;
_playerScoreLabel.Text = "Here is my sample text";

then, make sure your game code points to this new game screen...

GuiPlay playscreen = new GuiPlay();
GUICanvas.Instance.SetContentControl(playscreen);

That's it... you'll need to fill in a few gaps and there are a lot of other forum threads on this. But this should get you started. The Torque X programming book goes into much more detail.

The other, quick&dirty way looks more like this. First load a font...

Resource spriteFont = ResourceManager.Instance.LoadFont("Arial12");

Then, add a Draw method overrride to the Game class (game.cs) and manually draw the output text.

protected override void Draw(GameTime gameTime)
{
base.Draw(gameTime);

FontRenderer.Instance.DrawString(spriteFont, new Vector2(975,100), Color.White, "Here is my sample text", "");
}

That should also do it

John K.
#2
12/16/2008 (8:46 am)
Thanks bunches John. That was exactly what I am looking for.

After reading through a lot of posts yesterday I gathered I needed to order the book. SO I DID!! I'm so excited. This morning I downloaded some of the source from the site and saw how to do your first example.

Thanks again for your help!
#3
12/16/2008 (10:15 am)
Ach! I didn't even think of that. I should have pointed you to the source code on the website for Chapter 10. Good thinking! Hopefully that code and the comments above should set you in the right direction.

John K.