1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-21 04:29:14 +02:00
MLEM/MLEM/Extensions/SpriteBatchExtensions.cs

34 lines
1.3 KiB
C#
Raw Normal View History

2019-08-06 14:20:11 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MLEM.Extensions {
public static class SpriteBatchExtensions {
private static Texture2D blankTexture;
2019-08-06 14:46:43 +02:00
public static Texture2D GetBlankTexture(this SpriteBatch batch) {
2019-08-06 14:20:11 +02:00
if (blankTexture == null) {
2019-08-10 21:37:10 +02:00
blankTexture = new Texture2D(batch.GraphicsDevice, 1, 1);
2019-08-06 14:20:11 +02:00
blankTexture.SetData(new[] {Color.White});
2019-08-09 14:26:20 +02:00
batch.Disposing += (sender, args) => {
if (blankTexture != null) {
blankTexture.Dispose();
blankTexture = null;
}
};
2019-08-06 14:20:11 +02:00
}
return blankTexture;
}
public static void DrawCenteredString(this SpriteBatch batch, SpriteFont font, string text, Vector2 position, float scale, Color color, bool horizontal = true, bool vertical = false, float addedScale = 0) {
var size = font.MeasureString(text);
var center = new Vector2(
horizontal ? size.X * scale / 2F : 0,
vertical ? size.Y * scale / 2F : 0);
batch.DrawString(font, text,
position + size * scale / 2 - center,
color, 0, size / 2, scale + addedScale, SpriteEffects.None, 0);
}
}
}