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.

152 lines
5.0 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 AddPoint(Point p, Color color) {
  80. AddLine(p.X, p.Y - 2, p.X, p.Y + 1, color);
  81. AddLine(p.X - 2, p.Y, p.X + 1, p.Y, color);
  82. }
  83. [System.Diagnostics.Conditional("DEBUG")]
  84. public static void AddLine(int p1x, int p1y, int p2x, int p2y, Color color) {
  85. if (lineIdx >= MAX_LINE_VERTICES) {
  86. return;
  87. }
  88. lineVertices[lineIdx] = new VertexPositionColor(new Vector3(p1x, p1y, 0), color);
  89. lineVertices[lineIdx + 1] = new VertexPositionColor(new Vector3(p2x, p2y, 0), color);
  90. lineIdx += 2;
  91. }
  92. [System.Diagnostics.Conditional("DEBUG")]
  93. public static void AddLine(Point start, Point end, Color color) {
  94. AddLine(start.X, start.Y, end.X, end.Y, color);
  95. }
  96. [System.Diagnostics.Conditional("DEBUG")]
  97. public static void AddLine(Vector2 start, Vector2 end, Color color) {
  98. AddLine(start.ToPoint(), end.ToPoint(), color);
  99. }
  100. [System.Diagnostics.Conditional("DEBUG")]
  101. public static void DrawToasts(SpriteBatch spriteBatch) {
  102. if (!Enabled) {
  103. return;
  104. }
  105. // UI should start at least 48px from the left edge of the screen and 27 from the top, as per:
  106. // https://docs.microsoft.com/en-us/windows/uwp/design/devices/designing-for-tv#tv-safe-area
  107. // Can test this on actual Xbox via:
  108. // Settings > Launch Settings > General > TV & Display Options > Resolution > 720p.
  109. int y = 27;
  110. foreach (var toast in toasts) {
  111. spriteBatch.DrawString(Textures.DebugFont, toast, new Vector2(48, y), Color.Teal);
  112. y += 30;
  113. }
  114. }
  115. [System.Diagnostics.Conditional("DEBUG")]
  116. public static void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
  117. if (!Enabled) {
  118. return;
  119. }
  120. if (lineIdx == 0) {
  121. return;
  122. }
  123. graphics.SetVertexBuffer(vertexBuffer);
  124. vertexBuffer.SetData(lineVertices);
  125. foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
  126. pass.Apply();
  127. graphics.DrawPrimitives(PrimitiveType.LineList, 0, lineIdx / 2);
  128. }
  129. }
  130. }
  131. }