Example #1
0
        void BuildTexture()
        {
            FontDef font = ThemeManager.FetchFont(this.font);

            PxVector tsize = font.MeasureLine(text);
            if (texture != null) texture.Dispose();

            if (tsize.X == 0 || tsize.Y == 0) {
                texture = new Texture2D(GraphicsManager.device, 1, 1);
                return;
            }
            RenderTarget2D rt = new RenderTarget2D(GraphicsManager.device, tsize.X, tsize.Y, false, SurfaceFormat.Color, DepthFormat.None, 1, RenderTargetUsage.PreserveContents);

            RenderTarget2D pop = DrawBatch.Target;
            PxVector popOff = DrawBatch.drawOffset;

            DrawBatch.Target = rt;
            DrawBatch.drawOffset = new PxVector(0, 0);
            DrawBatch.Clear(rt, new DrawColor(0f, 0f, 0f, 0f));

            float cursor = 0;
            for (int i = 0; i < text.Length; i++) {
                Vector2 cpos = new Vector2((float)Math.Round(cursor), 0); // pixel align on draw, but retain float accumulation
                DrawBatch.Draw(font.Atlas, cpos.FxVector(), new FxVector(0, 0), font.GetGlyph(text[i]).PxRect(), Microsoft.Xna.Framework.Color.White.DrawColor(), 0f, new FxVector(1, 1));
                cursor += font.MeasureGlyph(text, i);
            }

            DrawBatch.Target = null;
            DrawBatch.Target = pop;
            DrawBatch.drawOffset = popOff;

            texture = rt;
        }