Exemple #1
0
        public static Texture2D FromText(string text, SpriteFont font, Color color, Size size, bool multiLine, int lineStart, GraphicsDevice device)
        {
            string[] drawAbleText = multiLine ? text.Split(new string[1] { "\n" }, StringSplitOptions.None) : new string[1] { text };
            RenderTarget2D target = new RenderTarget2D(device, size.Width, size.Height);
            SpriteBatch sb = new SpriteBatch(device);

            device.SetRenderTarget(target);
            device.Clear(Color.Transparent);

            sb.Begin();
            for (int i = lineStart; i < drawAbleText.Length; i++)
            {
                float y = 1 + (i - lineStart) * font.GetHeight();
                sb.DrawString(font, drawAbleText[i], new Vector2(1, y), color, 0f, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }
            sb.End();

            device.SetRenderTarget(null);

            Texture2D texture = new Texture2D(device, size.Width, size.Height);
            Color[] colorData = target.GetColorData();
            texture.SetData(colorData);

            target.Dispose();
            sb.Dispose();

            return texture;
        }