Game Development Community

Using FontRenderer and DrawUtil in overrided Render method

by rwillis · in Torque X 2D · 04/01/2008 (9:14 pm) · 4 replies

I want to draw text above all T2dSceneObjects to the screen using either DrawUtil.JustifiedText or FontRenderer method but when I use these in a overrided Render method of a T2DSceneObject (or T2dStaticSprite) it renders all the text BELOW all the other T2dSceneObjects (no matter what layer I set it at). BUT when I use these text draw methods in the draw method of the game class then they render above T2DSceneObjects (which is what I want).

This causes text to be rendered below T2dSceneObjects no matter I set the layer at:

class T2dSceneObjectWithText: T2DSceneObject
    {
        GUITextStyle style = new GUITextStyle();
        Resource<SpriteFont> font;
        string text = "Not set";
        Color color;

        public T2dSceneObjectWithText()
        {           
            font = ResourceManager.Instance.LoadFont("arial12");
        }

        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        public override void Render(SceneRenderState srs)
        {

            //DrawUtil.JustifiedText(font, Game.Instance.ConvertFromTorqueUnits(Position), new Vector2(10, 10), TextAlignment.JustifyCenter, color, text); 
            FontRenderer.Instance.DrawString(font, Game.Instance.ConvertFromTorqueUnits(Position), color, text);
            base.Render(srs);
        }

But this causes text to draw on top of T2DSceneObjects (which is what I want except I don't want to put all the text render stuff in Draw of my game class):

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

            FontRenderer.Instance.DrawString(font, Game.Instance.ConvertFromTorqueUnits(Position), color, text);

        }

Is there a way I can get text to draw on top of everything when using the 1st code example (with T2DSceneObjects) above?

#1
04/01/2008 (9:41 pm)
Switch the order of the statements in Render:

public override void Render(SceneRenderState srs)        
{          
        base.Render(srs);  
        //DrawUtil.JustifiedText(font, Game.Instance.ConvertFromTorqueUnits(Position), new Vector2(10, 10), TextAlignment.JustifyCenter, color, text);   
        FontRenderer.Instance.DrawString(font, Game.Instance.ConvertFromTorqueUnits(Position), color, text);     
 }
#2
04/01/2008 (10:26 pm)
Done. Same thing happens. I actually had it like that at first but then I put base.Render on bottom to see if that fixed it.
#3
04/02/2008 (5:08 am)
Ah, the text will draw at the same layer as the sceneobject---screwed.

I suppose another approach might be to create a separate blank T2dSceneObjectWithText at layer 0 and update its position within the other sceneobject (via IAnimatedObject.UpdateAnimation).

Similar to what you tried with GUIText.
#4
04/03/2008 (7:10 pm)
Alright. Thanks very much for your help.