2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
|
|
|
static class Debug {
|
|
|
|
struct DebugRect {
|
2020-01-23 19:31:00 +00:00
|
|
|
public Rectangle Rect;
|
|
|
|
public Color Color;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
public DebugRect(Rectangle rect, Color color) {
|
2020-01-23 19:31:00 +00:00
|
|
|
Rect = rect;
|
|
|
|
Color = color;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 16:40:32 +00:00
|
|
|
struct DebugLine {
|
2020-01-23 19:31:00 +00:00
|
|
|
public Point Start;
|
|
|
|
public Point End;
|
|
|
|
public Color Color;
|
2020-01-23 16:40:32 +00:00
|
|
|
|
|
|
|
public DebugLine(Point start, Point end, Color color) {
|
2020-01-23 19:31:00 +00:00
|
|
|
Start = start;
|
|
|
|
End = end;
|
|
|
|
Color = color;
|
2020-01-23 16:40:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
public static bool Enabled;
|
2020-01-23 17:19:16 +00:00
|
|
|
// This is a LinkedList instead of a List because SetFpsText() adds to its front.
|
2020-01-25 02:04:30 +00:00
|
|
|
static readonly LinkedList<string> toasts = new LinkedList<string>();
|
|
|
|
static readonly List<DebugRect> rects = new List<DebugRect>();
|
|
|
|
static readonly List<DebugLine> lines = new List<DebugLine>();
|
2020-01-23 17:19:16 +00:00
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
static Texture2D whiteTexture;
|
|
|
|
|
2020-01-23 17:19:16 +00:00
|
|
|
public static void Initialize(GraphicsDevice graphics) {
|
|
|
|
whiteTexture = new Texture2D(graphics, 1, 1);
|
|
|
|
whiteTexture.SetData(new Color[] { Color.White });
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2020-01-23 17:19:16 +00:00
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
public static void Clear() {
|
2020-01-23 17:19:16 +00:00
|
|
|
toasts.Clear();
|
2020-01-18 03:41:45 +00:00
|
|
|
rects.Clear();
|
2020-01-23 16:40:32 +00:00
|
|
|
lines.Clear();
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 17:19:16 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-18 03:41:45 +00:00
|
|
|
public static void AddRect(Rectangle rect, Color color) {
|
|
|
|
rects.Add(new DebugRect(rect, color));
|
|
|
|
}
|
|
|
|
|
2020-01-23 16:40:32 +00:00
|
|
|
public static void AddLine(Point start, Point end, Color color) {
|
|
|
|
lines.Add(new DebugLine(start, end, color));
|
|
|
|
}
|
|
|
|
|
2020-01-23 17:19:16 +00:00
|
|
|
public static void DrawToasts(SpriteBatch spriteBatch, SpriteFont font) {
|
2020-01-23 21:18:28 +00:00
|
|
|
if (!Enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-23 17:19:16 +00:00
|
|
|
int y = 10;
|
|
|
|
foreach (var toast in toasts) {
|
|
|
|
spriteBatch.DrawString(font, toast, new Vector2(10, y), Color.Teal);
|
|
|
|
y += 30;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Draw(SpriteBatch spriteBatch, Camera camera) {
|
|
|
|
if (!Enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach (var debugRect in rects) {
|
2020-01-23 19:31:00 +00:00
|
|
|
var rect = debugRect.Rect;
|
2020-01-18 03:41:45 +00:00
|
|
|
rect.Offset(-camera.Left, 0);
|
2020-01-23 19:31:00 +00:00
|
|
|
var color = debugRect.Color;
|
2020-01-18 03:41:45 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2020-01-23 21:15:55 +00:00
|
|
|
foreach (var line in lines) {
|
|
|
|
Point[] points = Line.Rasterize(line.Start, line.End);
|
|
|
|
foreach (var point in points) {
|
|
|
|
spriteBatch.Draw(
|
2020-01-23 21:18:28 +00:00
|
|
|
whiteTexture, new Rectangle(point.X, point.Y, 1, 1), line.Color);
|
2020-01-23 21:15:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|