1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 22:18:34 +01:00

added a method for generating square and circle textures

This commit is contained in:
Ellpeck 2020-07-12 19:41:25 +02:00
parent a17301504d
commit 98937ee83f
3 changed files with 73 additions and 22 deletions

View file

@ -1,3 +1,4 @@
using System;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Misc; using MLEM.Misc;
@ -21,18 +22,14 @@ namespace MLEM.Extensions {
if (blankTexture == null) { if (blankTexture == null) {
blankTexture = new Texture2D(batch.GraphicsDevice, 1, 1); blankTexture = new Texture2D(batch.GraphicsDevice, 1, 1);
blankTexture.SetData(new[] {Color.White}); blankTexture.SetData(new[] {Color.White});
batch.Disposing += (sender, args) => { AutoDispose(batch, blankTexture);
if (blankTexture != null) {
blankTexture.Dispose();
blankTexture = null;
}
};
} }
return blankTexture; return blankTexture;
} }
/// <summary> /// <summary>
/// Generates a <see cref="NinePatch"/> that has a texture with a given color and outline color /// Generates a <see cref="NinePatch"/> that has a texture with a given color and outline color.
/// This texture is automatically disposed of when the batch is disposed.
/// </summary> /// </summary>
/// <param name="batch">The sprite batch</param> /// <param name="batch">The sprite batch</param>
/// <param name="color">The fill color of the texture</param> /// <param name="color">The fill color of the texture</param>
@ -46,15 +43,47 @@ namespace MLEM.Extensions {
outli, color, outli, outli, color, outli,
outli, outli, outli outli, outli, outli
}); });
batch.Disposing += (sender, args) => { AutoDispose(batch, tex);
if (tex != null) {
tex.Dispose();
tex = null;
}
};
return new NinePatch(tex, 1); return new NinePatch(tex, 1);
} }
/// <summary>
/// Generates a 1x1 texture with the given color.
/// This texture is automatically disposed of when the batch is disposed.
/// </summary>
/// <param name="batch">The sprite batch</param>
/// <param name="color">The color of the texture</param>
/// <returns>A new texture with the given data</returns>
public static Texture2D GenerateSquareTexture(this SpriteBatch batch, Color color) {
var tex = new Texture2D(batch.GraphicsDevice, 1, 1);
tex.SetData(new[] {color});
AutoDispose(batch, tex);
return tex;
}
/// <summary>
/// Generates a texture with the given size that contains a circle.
/// The circle's center will be the center of the texture, and the circle will lead up to the edges of the texture.
/// This texture is automatically disposed of when the batch is disposed.
/// </summary>
/// <param name="batch">The sprite batch</param>
/// <param name="color">The color of the texture</param>
/// <param name="size">The width and height of the texture, and the diameter of the circle</param>
/// <returns>A new texture with the given data</returns>
public static Texture2D GenerateCircleTexture(this SpriteBatch batch, Color color, int size) {
var tex = new Texture2D(batch.GraphicsDevice, size, size);
using (var data = tex.GetTextureData()) {
for (var x = 0; x < tex.Width; x++) {
for (var y = 0; y < tex.Height; y++) {
var dist = Vector2.Distance(new Vector2(size / 2), new Vector2(x, y));
data[x, y] = dist <= size / 2 ? color : Color.Transparent;
}
}
}
AutoDispose(batch, tex);
return tex;
}
/// <inheritdoc cref="SpriteBatch.Draw(Texture2D,Rectangle,Rectangle?,Color,float,Vector2,SpriteEffects,float)"/> /// <inheritdoc cref="SpriteBatch.Draw(Texture2D,Rectangle,Rectangle?,Color,float,Vector2,SpriteEffects,float)"/>
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) { 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); var source = sourceRectangle ?? new Rectangle(0, 0, texture.Width, texture.Height);
@ -72,5 +101,14 @@ namespace MLEM.Extensions {
batch.Draw(texture, destinationRectangle, null, color); batch.Draw(texture, destinationRectangle, null, color);
} }
private static void AutoDispose(SpriteBatch batch, Texture2D texture) {
batch.Disposing += (sender, ars) => {
if (texture != null) {
texture.Dispose();
texture = null;
}
};
}
} }
} }

View file

@ -10,6 +10,7 @@ namespace MLEM.Extensions {
/// <summary> /// <summary>
/// Returns a new instance of <see cref="TextureData"/> which allows easily managing a texture's data with texture coordinates rather than indices. /// Returns a new instance of <see cref="TextureData"/> which allows easily managing a texture's data with texture coordinates rather than indices.
/// When this is used in a using statement, the texture data is automatically stored back in the texture at the end.
/// </summary> /// </summary>
/// <param name="texture">The texture whose data to get</param> /// <param name="texture">The texture whose data to get</param>
/// <returns>The texture's data</returns> /// <returns>The texture's data</returns>
@ -20,7 +21,7 @@ namespace MLEM.Extensions {
/// <summary> /// <summary>
/// A struct that represents the data of a texture, accessed through <see cref="TextureExtensions.GetTextureData"/>. /// A struct that represents the data of a texture, accessed through <see cref="TextureExtensions.GetTextureData"/>.
/// </summary> /// </summary>
public struct TextureData { public class TextureData : IDisposable {
private readonly Texture2D texture; private readonly Texture2D texture;
private readonly Color[] data; private readonly Color[] data;
@ -92,6 +93,11 @@ namespace MLEM.Extensions {
return x >= 0 && y >= 0 && x < this.texture.Width && y < this.texture.Height; return x >= 0 && y >= 0 && x < this.texture.Width && y < this.texture.Height;
} }
/// <inheritdoc />
public void Dispose() {
this.Store();
}
} }
} }

View file

@ -61,10 +61,11 @@ namespace Sandbox {
}; };
var tex = this.rawContent.Load<Texture2D>("Textures/Test"); var tex = this.rawContent.Load<Texture2D>("Textures/Test");
var data = tex.GetTextureData(); using (var data = tex.GetTextureData()) {
data[1, 9] = Color.Pink; var textureData = data;
data[data.FromIndex(data.ToIndex(25, 9))] = Color.Yellow; textureData[1, 9] = Color.Pink;
data.Store(); textureData[textureData.FromIndex(textureData.ToIndex(25, 9))] = Color.Yellow;
}
//var font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")); //var font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
var font = new GenericBitmapFont(LoadContent<BitmapFont>("Fonts/Regular")); var font = new GenericBitmapFont(LoadContent<BitmapFont>("Fonts/Regular"));
@ -134,12 +135,18 @@ namespace Sandbox {
this.tokenized = formatter.Tokenize(font, strg); this.tokenized = formatter.Tokenize(font, strg);
this.tokenized.Split(font, 400, sc); this.tokenized.Split(font, 400, sc);
var square = this.SpriteBatch.GenerateSquareTexture(Color.Yellow);
var round = this.SpriteBatch.GenerateCircleTexture(Color.Green, 1024);
this.OnDraw += (g, time) => { this.OnDraw += (g, time) => {
this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp); this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
this.SpriteBatch.FillRectangle(new RectangleF(400, 20, 400, 1000), Color.Green); this.SpriteBatch.Draw(square, new Rectangle(10, 10, 400, 400), Color.White);
font.DrawString(this.SpriteBatch, this.tokenized.DisplayString, new Vector2(400, 20), Color.White * 0.25F, 0, Vector2.Zero, sc, SpriteEffects.None, 0); this.SpriteBatch.Draw(round, new Rectangle(10, 10, 400, 400), Color.White);
this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, sc, 0);
this.SpriteBatch.DrawGrid(new Vector2(30, 30), new Vector2(40, 60), new Point(10, 5), Color.Yellow, 3); //this.SpriteBatch.FillRectangle(new RectangleF(400, 20, 400, 1000), Color.Green);
//font.DrawString(this.SpriteBatch, this.tokenized.DisplayString, new Vector2(400, 20), Color.White * 0.25F, 0, Vector2.Zero, sc, SpriteEffects.None, 0);
//this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, sc, 0);
//this.SpriteBatch.DrawGrid(new Vector2(30, 30), new Vector2(40, 60), new Point(10, 5), Color.Yellow, 3);
this.SpriteBatch.End(); this.SpriteBatch.End();
}; };
this.OnUpdate += (g, time) => { this.OnUpdate += (g, time) => {