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.

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