Game Development Community

Unable to draw GUIText

by David Helmer · in Torque X 2D · 06/22/2008 (12:49 pm) · 10 replies

I'm attempting to draw some GUIText and it's just not showing up.

Here's the definition of the GUIText
GUIText cardText = new GUIText();
cardText.Position = cardPosition + new Vector2(5, cardHeight / 2);
cardText.Style = cardTextStyle;
cardText.Text = "testasdgasdgadf";
cardText.HorizSizing = HorizSizing.Left;
cardText.VertSizing = VertSizing.Top;
cardText.Visible = true;
TorqueObjectDatabase.Instance.Register(cardText);

cardText.Position when registered has the position 892.5, 307.5 where the camera has an extent of 1024x768

Here is the cardTextStyle definition

cardTextStyle = new GUITextStyle();
cardTextStyle.FontType = "Arial14";
cardTextStyle.IsOpaque = true;
cardTextStyle.SizeToText = true;
cardTextStyle.TextColor[CustomColor.ColorBase] = Color.Black;

There is no sign of it showing. Am I missing something?

#1
06/22/2008 (3:26 pm)
Possibly try this:
GUIControl root = new GUIControl();
 root.Style = cardTextStyle ;
 cardText.Folder = root;

I also have this in my code, its been a while:

GUICanvas.Instance.PushDialogControl(root, 0);

Not sure if that is still required in 2.0 or not.
#2
06/23/2008 (11:08 am)
Thanks, but I'm still not getting anything. I create a GUIControl and assign the GUITexts's folders to it

rootControl = new GUIControl();
rootControl.Style = cardTextStyle;

rootControl.Position = new Vector2(0, 0);
GUICanvas.Instance.PushDialogControl(rootControl, 0);

---

GUIText cardText = new GUIText();
cardText.Position = cardPosition + new Vector2(5, cardHeight / 2);
cardText.Style = cardTextStyle;
cardText.Text = "testasdgasdgadf";
cardText.HorizSizing = HorizSizing.Left;
cardText.VertSizing = VertSizing.Top;
cardText.Size = new Vector2(cardWidth / 2, 50);
cardText.Visible = true;
cardText.Folder = rootControl;

GUICanvas.Instance.PushDialogControl(rootControl, 0);

and it still doesn't work

when I try to set the content control of the GUICanvas to rootControl, it crashes with the following exception,
A valid vertext declaration must be set on the device before any draw operations can be performed.

Thanks again!
#3
06/23/2008 (11:09 am)
Also I'm trying to use FontRenderer as another work around for this but haven't figured out to change the layer I'm drawing on since all my other objects are drawing over the text
#4
06/23/2008 (2:17 pm)
Try writing the entire thing into a class, similar to FPSText, in the FPS demo. ]

That should do it, or at least get you started. The main difference with the class, is it implements IGUIScreen, which has an automatic redundancy that hooks it up to the GUI, If i'm not mistaken.

Will
#5
06/23/2008 (7:12 pm)
LOL - I just coded this last night - good timing. For fun, try adding the following to your Game.cs file...

1. Add a new class member variable to hold a SpriteFont resource
public Resource spriteFont;

2. Initialize the font resource in your BeginRun() method. As a bonus, you can use one of the built-in fonts that comes with Torque X.
spriteFont = ResourceManager.Instance.LoadFont("Arial14");

3. Write the text you want to see on the screen in the Draw(GameTime gameTime) method.
FontRenderer.Instance.DrawString(spriteFont, new Vector2(20,20), Color.White, "Hello Folks", "");

Like David mentioned, I haven't tried to set the layering of the text, this will just draw text on top of everything else.

John K.
#6
06/24/2008 (12:45 pm)
When I try to emulate the FPS demo and use

GUICanvas.Instance.SetContentControl(ViewContainer);

That line of code causes me to get a blank black screen. when I comment it out, all of my objects show up again

when I do the DrawString method, it seems to be off slightly, where it seems like it's drawing according to a different projection, so it draws the text just fine at 0,0 but once it gets to 900,190 it draws 30 pixels to the left

Is there a way I could define a non GUI object that renders text at a certain spot

thanks guys, you're being very helpful!
#7
06/25/2008 (3:11 am)
Are you using a 1024 window? I ran this test code:

string message = "Hello Folk!";
            float xPosition = Convert.ToSingle(Window.ClientBounds.Width) - spriteFont.Instance.MeasureString(message).X;
            FontRenderer.Instance.DrawString(spriteFont, new Vector2(xPosition, 190), Color.Beige, message, "");

That will flush it out to the right of the screen , as much as the length of the text is. The width was 1024, the text length 90, so 934 is the position it calculates. If you set it to 900, it would seem to match up to your observation it was off slightly. Just some thoughts.
#8
07/07/2008 (7:31 am)
Real quick, I had all of my position stuff correct, I discovered that when in wide screen, the FontRenderer.Instance.DrawString, actually had a weird error where it didn't draw stuff in the proper position, I imagine someone else must have noticed it
#9
07/07/2008 (7:34 am)
Here's how I ended up incorporating text with all of my objects, I drew the text on the textures I was using.
See this thread about how you can use this drawing on texture method


public static Texture2D DrawTextOnTexture(Texture2D destTexture, XyneText xyneText, Vector2 destSize)
        {
            RenderTarget2D myTarget = new RenderTarget2D(Game.Instance.GraphicsDevice, ((int)destSize.X), ((int)destSize.Y), 1, destTexture.Format);
            TorqueEngineComponent.Instance.Game.GraphicsDevice.SetRenderTarget(0, myTarget);
            TorqueEngineComponent.Instance.Game.GraphicsDevice.Clear(ClearOptions.Target, new Vector4(0f, 0f, 0f, 0f), 0.0f, 0);

            // Blit to the other texture, or so I thought :(
            SpriteBatch spriteBatch = new SpriteBatch(TorqueEngineComponent.Instance.Game.GraphicsDevice);

            spriteBatch.Begin();
            spriteBatch.Draw(destTexture, new Rectangle(0, 0, ((int)destSize.X), ((int)destSize.Y)), Color.White);
            spriteBatch.DrawString(xyneText.Font.Instance, xyneText.Text, xyneText.Position, xyneText.Color);
            spriteBatch.End();

            TorqueEngineComponent.Instance.Game.GraphicsDevice.SetRenderTarget(0, null);

            return myTarget.GetTexture();
        }
#10
05/03/2009 (10:13 pm)
*edit* I seem to just get a purple screen now that I got it running, not sure what that means..