diff --git a/MLEM/Extensions/SpriteBatchExtensions.cs b/MLEM/Extensions/SpriteBatchExtensions.cs index 2b3678a..bf6b67a 100644 --- a/MLEM/Extensions/SpriteBatchExtensions.cs +++ b/MLEM/Extensions/SpriteBatchExtensions.cs @@ -83,6 +83,33 @@ namespace MLEM.Extensions { return tex; } + /// + /// Generates a texture with the given size that contains a gradient between the four specified corner colors. + /// If the same color is specified for two pairs of corners, a horizontal, vertical or diagonal gradient can be achieved. + /// This texture is automatically disposed of when the batch is disposed. + /// + /// The sprite batch + /// The color of the texture's top left corner + /// The color of the texture's top right corner + /// The color of the texture's bottom left corner + /// The color of the texture's bottom right corner + /// The width of the resulting texture, or 256 by default + /// The height of the resulting texture, or 256 by default + /// A new texture with the given data + public static Texture2D GenerateGradientTexture(this SpriteBatch batch, Color topLeft, Color topRight, Color bottomLeft, Color bottomRight, int width = 256, int height = 256) { + var tex = new Texture2D(batch.GraphicsDevice, width, height); + using (var data = tex.GetTextureData()) { + for (var x = 0; x < width; x++) { + var top = Color.Lerp(topLeft, topRight, x / (float) width); + var btm = Color.Lerp(bottomLeft, bottomRight, x / (float) width); + for (var y = 0; y < height; y++) + data[x, y] = Color.Lerp(top, btm, y / (float) height); + } + } + AutoDispose(batch, tex); + return tex; + } + /// public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) { var source = sourceRectangle ?? new Rectangle(0, 0, texture.Width, texture.Height); diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index aa98ed3..6eea08a 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -128,6 +128,7 @@ namespace Sandbox { var res = this.Content.LoadJson("Test"); Console.WriteLine("The res is " + res); + var gradient = this.SpriteBatch.GenerateGradientTexture(Color.Green, Color.Red, Color.Blue, Color.Yellow); this.OnDraw += (game, time) => { this.SpriteBatch.Begin(); this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), new Rectangle(640 - 4, 360 - 4, 8, 8), Color.Green); @@ -137,6 +138,8 @@ namespace Sandbox { font.DrawString(this.SpriteBatch, font.TruncateString("This is a very long string", 200, 1, ellipsis: "..."), new Vector2(200, 450), Color.White); font.DrawString(this.SpriteBatch, font.TruncateString("This is a very long string", 200, 1, true), new Vector2(200, 500), Color.White); font.DrawString(this.SpriteBatch, font.TruncateString("This is a very long string", 200, 1, true, "..."), new Vector2(200, 550), Color.White); + + this.SpriteBatch.Draw(gradient, new Rectangle(300, 100, 200, 200), Color.White); this.SpriteBatch.End(); };