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.

129 lines
3.7 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 = true;
  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. static readonly List<DebugRect> rects = new List<DebugRect>();
  28. static readonly List<DebugLine> lines = new List<DebugLine>();
  29. static Texture2D whiteTexture;
  30. public static void Initialize(Texture2D white) {
  31. whiteTexture = white;
  32. }
  33. public static void WriteLine(string s) {
  34. System.Diagnostics.Debug.WriteLine(s);
  35. }
  36. public static void WriteLine(string s, params object[] args) {
  37. System.Diagnostics.Debug.WriteLine(s, args);
  38. }
  39. public static void Clear(bool paused) {
  40. toasts.Clear();
  41. if (!paused) {
  42. rects.Clear();
  43. lines.Clear();
  44. }
  45. }
  46. public static void AddToast(string s) {
  47. toasts.AddLast(s);
  48. }
  49. // FPS text is always displayed as the first toast (if set).
  50. public static void SetFpsText(string s) {
  51. toasts.AddFirst(s);
  52. }
  53. public static void AddRect(Rectangle rect, Color color) {
  54. rects.Add(new DebugRect(rect, color));
  55. }
  56. public static void AddRect(AABB box, Color color) {
  57. Rectangle rect = new Rectangle(
  58. (int) (box.Position.X - box.HalfSize.X), (int) (box.Position.Y - box.HalfSize.Y),
  59. (int) (box.HalfSize.X * 2), (int) (box.HalfSize.Y * 2));
  60. AddRect(rect, color);
  61. }
  62. public static void AddLine(Point start, Point end, Color color) {
  63. lines.Add(new DebugLine(start, end, color));
  64. }
  65. public static void AddLine(int p1x, int p1y, int p2x, int p2y, Color color) {
  66. lines.Add(new DebugLine(new Point(p1x, p1y), new Point(p2x, p2y), color));
  67. }
  68. public static void AddLine(Vector2 start, Vector2 end, Color color) {
  69. lines.Add(new DebugLine(start.ToPoint(), end.ToPoint(), color));
  70. }
  71. public static void DrawToasts(SpriteBatch spriteBatch, SpriteFont font) {
  72. if (!Enabled) {
  73. return;
  74. }
  75. int y = 10;
  76. foreach (var toast in toasts) {
  77. spriteBatch.DrawString(font, toast, new Vector2(10, y), Color.Teal);
  78. y += 30;
  79. }
  80. }
  81. public static void Draw(SpriteBatch spriteBatch) {
  82. if (!Enabled) {
  83. return;
  84. }
  85. foreach (var debugRect in rects) {
  86. var rect = debugRect.Rect;
  87. var color = debugRect.Color;
  88. // top side
  89. spriteBatch.Draw(
  90. whiteTexture, new Rectangle(rect.Left, rect.Top, rect.Width, 1), color);
  91. // bottom side
  92. spriteBatch.Draw(
  93. whiteTexture, new Rectangle(rect.Left, rect.Bottom - 1, rect.Width, 1), color);
  94. // left side
  95. spriteBatch.Draw(
  96. whiteTexture, new Rectangle(rect.Left, rect.Top, 1, rect.Height), color);
  97. // right side
  98. spriteBatch.Draw(
  99. whiteTexture, new Rectangle(rect.Right - 1, rect.Top, 1, rect.Height), color);
  100. }
  101. foreach (var line in lines) {
  102. Point[] points = Line.Rasterize(line.Start, line.End);
  103. foreach (var point in points) {
  104. spriteBatch.Draw(
  105. whiteTexture, new Rectangle(point.X, point.Y, 1, 1), line.Color);
  106. }
  107. }
  108. }
  109. }
  110. }