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.

111 lines
3.2 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 LinkedList<string> toasts = new LinkedList<string>();
  27. static List<DebugRect> rects = new List<DebugRect>();
  28. static 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 DrawToasts(SpriteBatch spriteBatch, SpriteFont font) {
  59. int y = 10;
  60. foreach (var toast in toasts) {
  61. spriteBatch.DrawString(font, toast, new Vector2(10, y), Color.Teal);
  62. y += 30;
  63. }
  64. }
  65. public static void Draw(SpriteBatch spriteBatch, Camera camera) {
  66. if (!Enabled) {
  67. return;
  68. }
  69. foreach (var debugRect in rects) {
  70. var rect = debugRect.Rect;
  71. rect.Offset(-camera.Left, 0);
  72. var color = debugRect.Color;
  73. // top side
  74. spriteBatch.Draw(
  75. whiteTexture, new Rectangle(rect.Left, rect.Top, rect.Width, 1), color);
  76. // bottom side
  77. spriteBatch.Draw(
  78. whiteTexture, new Rectangle(rect.Left, rect.Bottom - 1, rect.Width, 1), color);
  79. // left side
  80. spriteBatch.Draw(
  81. whiteTexture, new Rectangle(rect.Left, rect.Top, 1, rect.Height), color);
  82. // right side
  83. spriteBatch.Draw(
  84. whiteTexture, new Rectangle(rect.Right - 1, rect.Top, 1, rect.Height), color);
  85. }
  86. foreach (var line in lines) {
  87. Point[] points = Line.Rasterize(line.Start, line.End);
  88. foreach (var point in points) {
  89. spriteBatch.Draw(
  90. whiteTexture, new Rectangle(point.X, point.Y, 1, 1), line.Color);
  91. }
  92. }
  93. }
  94. }
  95. }