1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-16 18:53:12 +01:00
MLEM/MLEM/Extensions/SpriteBatchExtensions.cs
2019-08-10 21:37:10 +02:00

34 lines
No EOL
1.3 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MLEM.Extensions {
public static class SpriteBatchExtensions {
private static Texture2D blankTexture;
public static Texture2D GetBlankTexture(this SpriteBatch batch) {
if (blankTexture == null) {
blankTexture = new Texture2D(batch.GraphicsDevice, 1, 1);
blankTexture.SetData(new[] {Color.White});
batch.Disposing += (sender, args) => {
if (blankTexture != null) {
blankTexture.Dispose();
blankTexture = null;
}
};
}
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);
}
}
}