sneak/Shared/Debug.cs
Colin McMillen 34e7ab3ee1 make Toasts a list & implement FPS counter as a toast
GitOrigin-RevId: bae35697a0329f7d5c1c45cdbc13c3be500b8c73
2020-02-13 14:50:39 -05:00

105 lines
2.9 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace SemiColinGames {
static class Debug {
struct DebugRect {
public Rectangle rect;
public Color color;
public DebugRect(Rectangle rect, Color color) {
this.rect = rect;
this.color = color;
}
}
struct DebugLine {
public Point start;
public Point end;
public Color color;
public DebugLine(Point start, Point end, Color color) {
this.start = start;
this.end = end;
this.color = color;
}
}
public static bool Enabled;
// This is a LinkedList instead of a List because SetFpsText() adds to its front.
static LinkedList<string> toasts = new LinkedList<string>();
static List<DebugRect> rects = new List<DebugRect>();
static List<DebugLine> lines = new List<DebugLine>();
static Texture2D whiteTexture;
public static void Initialize(GraphicsDevice graphics) {
whiteTexture = new Texture2D(graphics, 1, 1);
whiteTexture.SetData(new Color[] { Color.White });
}
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 Clear() {
toasts.Clear();
rects.Clear();
lines.Clear();
}
public static void AddToast(string s) {
toasts.AddLast(s);
}
// FPS text is always displayed as the first toast (if set).
public static void SetFpsText(string s) {
toasts.AddFirst(s);
}
public static void AddRect(Rectangle rect, Color color) {
rects.Add(new DebugRect(rect, color));
}
public static void AddLine(Point start, Point end, Color color) {
lines.Add(new DebugLine(start, end, color));
}
public static void DrawToasts(SpriteBatch spriteBatch, SpriteFont font) {
int y = 10;
foreach (var toast in toasts) {
spriteBatch.DrawString(font, toast, new Vector2(10, y), Color.Teal);
y += 30;
}
}
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);
}
}
}
}