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.

126 lines
3.8 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System.Collections.Generic;
  4. namespace SemiColinGames {
  5. 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 readonly VertexPositionColor[] lineVertices = new VertexPositionColor[MAX_LINE_VERTICES];
  32. static VertexBuffer vertexBuffer;
  33. public static void Initialize(GraphicsDevice graphics) {
  34. vertexBuffer = new VertexBuffer(
  35. graphics, typeof(VertexPositionColor), MAX_LINE_VERTICES, BufferUsage.WriteOnly);
  36. }
  37. public static void WriteLine(string s) {
  38. System.Diagnostics.Debug.WriteLine(s);
  39. }
  40. public static void WriteLine(string s, params object[] args) {
  41. System.Diagnostics.Debug.WriteLine(s, args);
  42. }
  43. public static void Clear(bool paused) {
  44. toasts.Clear();
  45. if (!paused) {
  46. lineIdx = 0;
  47. }
  48. }
  49. public static void AddToast(string s) {
  50. toasts.AddLast(s);
  51. }
  52. // FPS text is always displayed as the first toast (if set).
  53. public static void SetFpsText(string s) {
  54. toasts.AddFirst(s);
  55. }
  56. public static void AddRect(Rectangle rect, Color color) {
  57. AddLine(rect.Left, rect.Top, rect.Right, rect.Top, color);
  58. AddLine(rect.Right, rect.Top, rect.Right, rect.Bottom, color);
  59. AddLine(rect.Right, rect.Bottom, rect.Left, rect.Bottom, color);
  60. AddLine(rect.Left, rect.Bottom, rect.Left, rect.Top, color);
  61. }
  62. public static void AddRect(AABB box, Color color) {
  63. Rectangle rect = new Rectangle(
  64. (int) (box.Position.X - box.HalfSize.X), (int) (box.Position.Y - box.HalfSize.Y),
  65. (int) (box.HalfSize.X * 2), (int) (box.HalfSize.Y * 2));
  66. AddRect(rect, color);
  67. }
  68. public static void AddLine(Point start, Point end, Color color) {
  69. if (lineIdx >= MAX_LINE_VERTICES) {
  70. return;
  71. }
  72. lineVertices[lineIdx] = new VertexPositionColor(new Vector3(start.X, start.Y, 0), color);
  73. lineVertices[lineIdx + 1] = new VertexPositionColor(new Vector3(end.X, end.Y, 0), color);
  74. lineIdx += 2;
  75. }
  76. public static void AddLine(int p1x, int p1y, int p2x, int p2y, Color color) {
  77. AddLine(new Point(p1x, p1y), new Point(p2x, p2y), color);
  78. }
  79. public static void AddLine(Vector2 start, Vector2 end, Color color) {
  80. AddLine(start.ToPoint(), end.ToPoint(), color);
  81. }
  82. public static void DrawToasts(SpriteBatch spriteBatch) {
  83. if (!Enabled) {
  84. return;
  85. }
  86. int y = 10;
  87. foreach (var toast in toasts) {
  88. spriteBatch.DrawString(Textures.DebugFont, toast, new Vector2(10, y), Color.Teal);
  89. y += 30;
  90. }
  91. }
  92. public static void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
  93. if (!Enabled) {
  94. return;
  95. }
  96. if (lineIdx == 0) {
  97. return;
  98. }
  99. graphics.SetVertexBuffer(vertexBuffer);
  100. vertexBuffer.SetData(lineVertices);
  101. foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
  102. pass.Apply();
  103. graphics.DrawPrimitives(PrimitiveType.LineList, 0, lineIdx / 2);
  104. }
  105. }
  106. }
  107. }