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.

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