Ejemplo n.º 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;
        }
Ejemplo n.º 2
0
        public override void Refresh()
        {
            base.Refresh();

            using (RenderTarget2D target = new RenderTarget2D(device, Bounds.Width, Bounds.Height))
            {
                SpriteBatch sb = new SpriteBatch(device);

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

                sb.Begin();
                Draw(sb);
                sb.End();

                device.SetRenderTarget(null);
                image = ToDColorBmp(target.GetColorData(), Bounds.Width, Bounds.Height);
            }
        }
Ejemplo n.º 3
0
        public static Texture2D FromLabels(KeyValuePair<string, Color>[] labels, SpriteFont font, Size size, GraphicsDevice device)
        {
            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 = 0; i < labels.Length; i++)
            {
                string prevString = string.Join(" ", labels.Select(l => l.Key).ToArray(), 0, i);
                if (i > 0) prevString += " ";
                float x = 1 + font.MeasureString(prevString).X;

                sb.DrawString(font, labels[i].Key, new Vector2(x, 1), labels[i].Value, 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();

            return texture;
        }
Ejemplo n.º 4
0
        public static Texture2D Stretch(this Texture2D texture, Size size)
        {
            RenderTarget2D target = new RenderTarget2D(texture.GraphicsDevice, size.Width, size.Height);
            SpriteBatch sb = new SpriteBatch(texture.GraphicsDevice);

            texture.GraphicsDevice.SetRenderTarget(target);
            texture.GraphicsDevice.Clear(Color.Transparent);
            sb.Begin();
            sb.Draw(texture, new Rectangle(0, 0, size.Width, size.Height), Color.White);
            sb.End();

            texture.GraphicsDevice.SetRenderTarget(null);
            Texture2D result = new Texture2D(texture.GraphicsDevice, size.Width, size.Height);
            result.SetData(target.GetColorData());

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

            return result;
        }
Ejemplo n.º 5
0
        public static Texture2D RenderOnto(this Texture2D texture, Size size, Vector2 position = default(Vector2), float rotation = 0, Vector2 scale = default(Vector2))
        {
            RenderTarget2D target = new RenderTarget2D(texture.GraphicsDevice, size.Width, size.Height);
            SpriteBatch sb = new SpriteBatch(texture.GraphicsDevice);

            texture.GraphicsDevice.SetRenderTarget(target);
            texture.GraphicsDevice.Clear(Color.Transparent);
            sb.Begin();
            sb.Draw(texture, position, null, Color.White, rotation, Vector2.Zero, scale == default(Vector2) ? Vector2.One : scale, SpriteEffects.None, 1f);
            sb.End();

            texture.GraphicsDevice.SetRenderTarget(null);
            Texture2D result = new Texture2D(texture.GraphicsDevice, size.Width, size.Height);
            result.SetData(target.GetColorData());

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


            return result;
        }