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.

146 lines
4.8 KiB

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