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.

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