A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.7 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. namespace SemiColinGames {
  6. public static class Debug {
  7. struct DebugRect {
  8. public Rectangle Rect;
  9. public Color Color;
  10. public DebugRect(Rectangle rect, Color color) {
  11. Rect = rect;
  12. Color = color;
  13. }
  14. }
  15. struct DebugLine {
  16. public Point Start;
  17. public Point End;
  18. public Color Color;
  19. public DebugLine(Point start, Point end, Color color) {
  20. Start = start;
  21. End = end;
  22. Color = color;
  23. }
  24. }
  25. public static bool Enabled = false;
  26. // This is a LinkedList instead of a List because SetFpsText() adds to its front.
  27. static readonly LinkedList<string> toasts = new LinkedList<string>();
  28. // Lines in excess of MAX_LINES get dropped on the floor.
  29. const int MAX_LINES = 2000;
  30. const int MAX_LINE_VERTICES = MAX_LINES * 2;
  31. static int lineIdx = 0;
  32. static VertexPositionColor[] lineVertices;
  33. static VertexBuffer vertexBuffer;
  34. [Conditional("DEBUG")]
  35. public static void Initialize(GraphicsDevice graphics) {
  36. lineVertices = new VertexPositionColor[MAX_LINE_VERTICES];
  37. vertexBuffer?.Dispose();
  38. vertexBuffer = new VertexBuffer(
  39. graphics, typeof(VertexPositionColor), MAX_LINE_VERTICES, BufferUsage.WriteOnly);
  40. }
  41. [Conditional("DEBUG")]
  42. public static void WriteLine(string s) {
  43. System.Diagnostics.Debug.WriteLine(s);
  44. }
  45. [Conditional("DEBUG")]
  46. public static void WriteLine(string s, params object[] args) {
  47. System.Diagnostics.Debug.WriteLine(s, args);
  48. }
  49. [Conditional("DEBUG")]
  50. public static void Clear(bool paused) {
  51. toasts.Clear();
  52. if (!paused) {
  53. lineIdx = 0;
  54. }
  55. }
  56. [Conditional("DEBUG")]
  57. public static void AddToast(string s) {
  58. toasts.AddLast(s);
  59. }
  60. // FPS text is always displayed as the first toast (if set).
  61. [Conditional("DEBUG")]
  62. public static void SetFpsText(string s) {
  63. toasts.AddFirst(s);
  64. }
  65. [Conditional("DEBUG")]
  66. public static void AddRect(Rectangle rect, Color color) {
  67. AddLine(rect.Left, rect.Top + 1, rect.Right, rect.Top + 1, color);
  68. AddLine(rect.Left + 1, rect.Top + 1, rect.Left + 1, rect.Bottom, color);
  69. AddLine(rect.Right, rect.Top + 1, rect.Right, rect.Bottom, color);
  70. AddLine(rect.Left + 1, rect.Bottom, rect.Right, rect.Bottom, color);
  71. }
  72. [Conditional("DEBUG")]
  73. public static void AddRect(AABB box, Color color) {
  74. Rectangle rect = new Rectangle(
  75. (int) (box.Position.X - box.HalfSize.X), (int) (box.Position.Y - box.HalfSize.Y),
  76. (int) (box.HalfSize.X * 2), (int) (box.HalfSize.Y * 2));
  77. AddRect(rect, color);
  78. }
  79. [Conditional("DEBUG")]
  80. public static void AddPoint(Point p, Color color) {
  81. AddLine(p.X, p.Y - 2, p.X, p.Y + 1, color);
  82. AddLine(p.X - 2, p.Y, p.X + 1, p.Y, color);
  83. }
  84. [Conditional("DEBUG")]
  85. public static void AddLine(int p1x, int p1y, int p2x, int p2y, Color color) {
  86. if (lineIdx >= MAX_LINE_VERTICES) {
  87. return;
  88. }
  89. lineVertices[lineIdx] = new VertexPositionColor(new Vector3(p1x, p1y, 0), color);
  90. lineVertices[lineIdx + 1] = new VertexPositionColor(new Vector3(p2x, p2y, 0), color);
  91. lineIdx += 2;
  92. }
  93. [Conditional("DEBUG")]
  94. public static void AddLine(Point start, Point end, Color color) {
  95. AddLine(start.X, start.Y, end.X, end.Y, color);
  96. }
  97. [Conditional("DEBUG")]
  98. public static void AddLine(Vector2 start, Vector2 end, Color color) {
  99. AddLine(start.ToPoint(), end.ToPoint(), color);
  100. }
  101. [Conditional("DEBUG")]
  102. public static void DrawToasts(SpriteBatch spriteBatch) {
  103. if (!Enabled) {
  104. return;
  105. }
  106. // UI should start at least 48px from the left edge of the screen and 27 from the top, as per:
  107. // https://docs.microsoft.com/en-us/windows/uwp/design/devices/designing-for-tv#tv-safe-area
  108. // Can test this on actual Xbox via:
  109. // Settings > Launch Settings > General > TV & Display Options > Resolution > 720p.
  110. int y = 27;
  111. foreach (var toast in toasts) {
  112. spriteBatch.DrawString(Textures.DebugFont, toast, new Vector2(48, y), Color.Teal);
  113. y += 30;
  114. }
  115. }
  116. [Conditional("DEBUG")]
  117. public static void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
  118. if (!Enabled) {
  119. return;
  120. }
  121. if (lineIdx == 0) {
  122. return;
  123. }
  124. graphics.SetVertexBuffer(vertexBuffer);
  125. vertexBuffer.SetData(lineVertices);
  126. foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
  127. pass.Apply();
  128. graphics.DrawPrimitives(PrimitiveType.LineList, 0, lineIdx / 2);
  129. }
  130. }
  131. }
  132. }