Game Development Community

Is there a way to handle Fonts in Torque X?

by Steve Marshall · in Torque X 2D · 10/07/2008 (5:04 pm) · 5 replies

First off, I'm a newbie to game programming...so please keep that in mind :-).

Ok, so I already know how to implement Fonts in straight XNA, however, I'm having a tough time implmenting it with my game that is using torque X.

The main problem that I'm having is that I'm having trouble accessing Game.Draw(). Since its a protected method. I've tried using "Game.Instance", but since the Draw() methos is protected...its not available.



Below is how I've done it in XNA.

spriteBatch.DrawString(font, "My Score: " + score.ToString(),
new Vector2(scoreDrawPoint.X * viewportRect.Width,
scoreDrawPoint.Y * viewportRect.Height), Color.Aqua);

spriteBatch.End();

Game.Draw(gameTime);




Any ideas or links to documentation explaining this? (I've looked for 2-3 days now and found nothing.

#1
10/08/2008 (3:32 pm)
Hi Steve

I had the same issue too.. the way I solved it was to keep all my font drawing code under the Draw() method in Game, which I found gets called anyway, rather than you having to explicitely call it.

I had to add methods for LoadContent() to load the SpriteFont, an Initialize() method which in my case just sets the screen centre and scaling for the font and does some camera stuff as I'm having the worlds most crazy fun getting the camera to work with my widescreen TV on the XBOX and my laptop.

The only other thing of note was that the text always displays on top of everything else, and so within the Draw() method I had to set a state variable so that the text didn't display at the wrong time. For example I don't want to put some score text up if I'm in the game menu.

I think there is a way to do this properly with TorqueX now, and I'm sure there's a chapter in John's book that covers it off, but since I got the XNA way working even if it's somewhat messy, I just left it at that. Plus the book hasn't arrived yet from Amazon! :-(

Come back to me if you want to see the code I used - I'm also new to XNA and TorqueX and C#, so it won't be the best programming example you ever saw, but at least it works.
#2
10/08/2008 (6:48 pm)
Paul, thanks for the response.

I'll go ahead and try that and see if I could get it working.

Btw, I also ordered John's book from Amazon and I'm waiting for it also :-).
#3
10/08/2008 (11:29 pm)
Hey guys, since you prefer going the SpriteBatch.DrawString() approach instead of using the GUI Framework (and the GUIText control). Give this a try... Open you Game.cs file and override the Draw method like this:

protected override void Draw(GameTime gameTime)
{
   base.Draw(gameTime);
   
   FontRenderer.Instance.DrawString(spriteFont, new Vector2(100,100), 
      Color.White, "Hello", "");
}

The FontRenderer ultimately uses the SpriteBatch.DrawString() calls behind the scenes. But now that you've overriden the Draw() method, you can really do anything XNA-ish that you want. You can also override Update() too, if you need to. Hope that helps...

John K.
#4
10/09/2008 (12:30 pm)
Hi John

What you've written is what my Draw() function looks like, except that I was using SpriteBatch.Drawstring like Steve but in a function, and I'm sure I couldn't call on the Draw method from outside Game - in my case the Movement Component and I'm sure there's a very good reason for that!

I thought at the time I tried stuff like making the function public, and still got nowhere, but I put this mainly down to my being very new to all of this, so there was probably something fundamental I don't understand (and probably still is), and so I just went with what I could make work. I figure if it looks pretty to the guys playing for it, and it's not buggy, then it's good enough for me!

I'd read about GUIText, but I'm sure when I tried it (somewhere between v1 the beta and v2 of XNA), it didn't work for me.. probably would now, and is probably the way to do this properly!

All of this is exactly why I can't wait to read your book, so thanks for helping us newbs out so much. For me reading your answers have always been a great help, and often very timely!!

All the best

Paul
#5
10/10/2008 (6:57 pm)
I actually haven't come across the GUIText control until now.

I also am looking forward to reading the book as these are the things that have been taking up a lot of my time...figuring out how to do some of the simpler, but necessary things.

And yes, I'm a total newbie! :)