sneak/Jumpy.Shared/Debug.cs
Colin McMillen fc02b768a1 remove stale TODO
GitOrigin-RevId: b6ea9c98a19625ceaaa8ac4148d8b3b1a6a8c05f
2020-02-13 14:48:22 -05:00

80 lines
2.2 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace Jumpy {
static class Debug {
struct DebugRect {
public Rectangle rect;
public Color color;
public DebugRect(Rectangle rect, Color color) {
this.rect = rect;
this.color = color;
}
}
public static bool Enabled;
static List<DebugRect> rects = new List<DebugRect>();
static Texture2D whiteTexture;
static string toast = null;
public static void Toast(string s) {
toast = s;
}
public static void WriteLine(string s) {
System.Diagnostics.Debug.WriteLine(s);
}
public static void WriteLine(string s, params object[] args) {
System.Diagnostics.Debug.WriteLine(s, args);
}
public static void Initialize(GraphicsDevice graphics) {
whiteTexture = new Texture2D(graphics, 1, 1);
whiteTexture.SetData(new Color[] { Color.White });
}
public static void Clear() {
rects.Clear();
}
public static void AddRect(Rectangle rect, Color color) {
rects.Add(new DebugRect(rect, color));
}
public static void DrawToast(SpriteBatch spriteBatch, SpriteFont font) {
// TODO: this use of toast isn't thread-safe.
if (toast == null) {
return;
}
spriteBatch.DrawString(font, toast, new Vector2(10, 40), Color.Teal);
toast = null;
}
public static void Draw(SpriteBatch spriteBatch, Camera camera) {
if (!Enabled) {
return;
}
foreach (var debugRect in rects) {
var rect = debugRect.rect;
rect.Offset(-camera.Left, 0);
var color = debugRect.color;
// top side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Left, rect.Top, rect.Width, 1), color);
// bottom side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Left, rect.Bottom - 1, rect.Width, 1), color);
// left side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Left, rect.Top, 1, rect.Height), color);
// right side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Right - 1, rect.Top, 1, rect.Height), color);
}
}
}
}