2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
2020-03-09 16:22:33 +00:00
|
|
|
public static class Debug {
|
2020-01-18 03:41:45 +00:00
|
|
|
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-29 18:56:08 +00:00
|
|
|
|
2020-02-19 20:25:22 +00:00
|
|
|
public static bool Enabled = false;
|
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>();
|
2020-02-15 21:31:59 +00:00
|
|
|
// Lines in excess of MAX_LINES get dropped on the floor.
|
|
|
|
const int MAX_LINES = 2000;
|
|
|
|
const int MAX_LINE_VERTICES = MAX_LINES * 2;
|
2020-03-10 19:58:56 +00:00
|
|
|
|
2020-02-15 20:47:05 +00:00
|
|
|
static int lineIdx = 0;
|
2020-03-10 19:58:56 +00:00
|
|
|
static VertexPositionColor[] lineVertices;
|
2020-02-15 20:47:05 +00:00
|
|
|
static VertexBuffer vertexBuffer;
|
2020-01-23 17:19:16 +00:00
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-02-15 21:31:59 +00:00
|
|
|
public static void Initialize(GraphicsDevice graphics) {
|
2020-03-10 19:58:56 +00:00
|
|
|
lineVertices = new VertexPositionColor[MAX_LINE_VERTICES];
|
|
|
|
vertexBuffer?.Dispose();
|
2020-02-15 20:47:05 +00:00
|
|
|
vertexBuffer = new VertexBuffer(
|
|
|
|
graphics, typeof(VertexPositionColor), MAX_LINE_VERTICES, BufferUsage.WriteOnly);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-01-18 03:41:45 +00:00
|
|
|
public static void WriteLine(string s) {
|
|
|
|
System.Diagnostics.Debug.WriteLine(s);
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-01-18 03:41:45 +00:00
|
|
|
public static void WriteLine(string s, params object[] args) {
|
|
|
|
System.Diagnostics.Debug.WriteLine(s, args);
|
|
|
|
}
|
2020-01-23 17:19:16 +00:00
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-01-29 21:33:55 +00:00
|
|
|
public static void Clear(bool paused) {
|
2020-01-23 17:19:16 +00:00
|
|
|
toasts.Clear();
|
2020-01-29 21:33:55 +00:00
|
|
|
if (!paused) {
|
2020-02-15 20:47:05 +00:00
|
|
|
lineIdx = 0;
|
2020-01-29 21:33:55 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
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).
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-01-23 17:19:16 +00:00
|
|
|
public static void SetFpsText(string s) {
|
|
|
|
toasts.AddFirst(s);
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-01-18 03:41:45 +00:00
|
|
|
public static void AddRect(Rectangle rect, Color color) {
|
2020-02-25 14:57:31 +00:00
|
|
|
AddLine(rect.Left, rect.Top + 1, rect.Right, rect.Top + 1, color);
|
|
|
|
AddLine(rect.Left + 1, rect.Top + 1, rect.Left + 1, rect.Bottom, color);
|
|
|
|
AddLine(rect.Right, rect.Top + 1, rect.Right, rect.Bottom, color);
|
|
|
|
AddLine(rect.Left + 1, rect.Bottom, rect.Right, rect.Bottom, color);
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-01-30 21:58:42 +00:00
|
|
|
public static void AddRect(AABB box, Color color) {
|
2020-01-29 20:42:13 +00:00
|
|
|
Rectangle rect = new Rectangle(
|
|
|
|
(int) (box.Position.X - box.HalfSize.X), (int) (box.Position.Y - box.HalfSize.Y),
|
|
|
|
(int) (box.HalfSize.X * 2), (int) (box.HalfSize.Y * 2));
|
|
|
|
AddRect(rect, color);
|
|
|
|
}
|
|
|
|
|
2020-03-11 16:44:14 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
|
|
|
public static void AddPoint(Point p, Color color) {
|
|
|
|
AddLine(p.X, p.Y - 2, p.X, p.Y + 1, color);
|
|
|
|
AddLine(p.X - 2, p.Y, p.X + 1, p.Y, color);
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-03-11 16:48:55 +00:00
|
|
|
public static void AddLine(int p1x, int p1y, int p2x, int p2y, Color color) {
|
2020-02-15 20:47:05 +00:00
|
|
|
if (lineIdx >= MAX_LINE_VERTICES) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-11 16:48:55 +00:00
|
|
|
lineVertices[lineIdx] = new VertexPositionColor(new Vector3(p1x, p1y, 0), color);
|
|
|
|
lineVertices[lineIdx + 1] = new VertexPositionColor(new Vector3(p2x, p2y, 0), color);
|
2020-02-15 20:47:05 +00:00
|
|
|
lineIdx += 2;
|
2020-01-23 16:40:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-03-11 16:48:55 +00:00
|
|
|
public static void AddLine(Point start, Point end, Color color) {
|
|
|
|
AddLine(start.X, start.Y, end.X, end.Y, color);
|
2020-01-28 02:48:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-01-31 19:12:02 +00:00
|
|
|
public static void AddLine(Vector2 start, Vector2 end, Color color) {
|
2020-02-15 20:47:05 +00:00
|
|
|
AddLine(start.ToPoint(), end.ToPoint(), color);
|
2020-01-31 19:12:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-02-19 16:19:23 +00:00
|
|
|
public static void DrawToasts(SpriteBatch spriteBatch) {
|
2020-01-23 21:18:28 +00:00
|
|
|
if (!Enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-26 00:18:58 +00:00
|
|
|
// UI should start at least 48px from the left edge of the screen and 27 from the top, as per:
|
2020-02-21 15:02:09 +00:00
|
|
|
// https://docs.microsoft.com/en-us/windows/uwp/design/devices/designing-for-tv#tv-safe-area
|
|
|
|
// Can test this on actual Xbox via:
|
|
|
|
// Settings > Launch Settings > Display & Sound > Resolution > 720p.
|
|
|
|
int y = 27;
|
2020-01-23 17:19:16 +00:00
|
|
|
foreach (var toast in toasts) {
|
2020-02-21 15:02:09 +00:00
|
|
|
spriteBatch.DrawString(Textures.DebugFont, toast, new Vector2(48, y), Color.Teal);
|
2020-01-23 17:19:16 +00:00
|
|
|
y += 30;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:58:56 +00:00
|
|
|
[System.Diagnostics.Conditional("DEBUG")]
|
2020-02-15 21:31:59 +00:00
|
|
|
public static void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
|
2020-01-18 03:41:45 +00:00
|
|
|
if (!Enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-15 21:31:59 +00:00
|
|
|
if (lineIdx == 0) {
|
|
|
|
return;
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
2020-02-15 20:47:05 +00:00
|
|
|
|
|
|
|
graphics.SetVertexBuffer(vertexBuffer);
|
|
|
|
vertexBuffer.SetData(lineVertices);
|
|
|
|
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
|
|
|
|
pass.Apply();
|
|
|
|
graphics.DrawPrimitives(PrimitiveType.LineList, 0, lineIdx / 2);
|
2020-01-23 21:15:55 +00:00
|
|
|
}
|
2020-01-18 03:41:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|