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.

145 lines
4.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 = true;
  25. // Lines in excess of MAX_LINES get dropped on the floor.
  26. const int MAX_LINES = 1000;
  27. const int MAX_LINE_VERTICES = MAX_LINES * 2;
  28. // This is a LinkedList instead of a List because SetFpsText() adds to its front.
  29. static readonly LinkedList<string> toasts = new LinkedList<string>();
  30. static readonly List<DebugRect> rects = new List<DebugRect>();
  31. static int lineIdx = 0;
  32. static readonly VertexPositionColor[] lineVertices = new VertexPositionColor[MAX_LINE_VERTICES];
  33. static VertexBuffer vertexBuffer;
  34. static Texture2D whiteTexture;
  35. public static void Initialize(GraphicsDevice graphics, Texture2D white) {
  36. whiteTexture = white;
  37. vertexBuffer = new VertexBuffer(
  38. graphics, typeof(VertexPositionColor), MAX_LINE_VERTICES, BufferUsage.WriteOnly);
  39. }
  40. public static void WriteLine(string s) {
  41. System.Diagnostics.Debug.WriteLine(s);
  42. }
  43. public static void WriteLine(string s, params object[] args) {
  44. System.Diagnostics.Debug.WriteLine(s, args);
  45. }
  46. public static void Clear(bool paused) {
  47. toasts.Clear();
  48. if (!paused) {
  49. rects.Clear();
  50. lineIdx = 0;
  51. }
  52. }
  53. public static void AddToast(string s) {
  54. toasts.AddLast(s);
  55. }
  56. // FPS text is always displayed as the first toast (if set).
  57. public static void SetFpsText(string s) {
  58. toasts.AddFirst(s);
  59. }
  60. public static void AddRect(Rectangle rect, Color color) {
  61. rects.Add(new DebugRect(rect, color));
  62. }
  63. public static void AddRect(AABB box, Color color) {
  64. Rectangle rect = new Rectangle(
  65. (int) (box.Position.X - box.HalfSize.X), (int) (box.Position.Y - box.HalfSize.Y),
  66. (int) (box.HalfSize.X * 2), (int) (box.HalfSize.Y * 2));
  67. AddRect(rect, color);
  68. }
  69. public static void AddLine(Point start, Point end, Color color) {
  70. if (lineIdx >= MAX_LINE_VERTICES) {
  71. return;
  72. }
  73. lineVertices[lineIdx] = new VertexPositionColor(new Vector3(start.X, start.Y, 0), color);
  74. lineVertices[lineIdx + 1] = new VertexPositionColor(new Vector3(end.X, end.Y, 0), color);
  75. lineIdx += 2;
  76. }
  77. public static void AddLine(int p1x, int p1y, int p2x, int p2y, Color color) {
  78. AddLine(new Point(p1x, p1y), new Point(p2x, p2y), color);
  79. }
  80. public static void AddLine(Vector2 start, Vector2 end, Color color) {
  81. AddLine(start.ToPoint(), end.ToPoint(), color);
  82. }
  83. public static void DrawToasts(SpriteBatch spriteBatch, SpriteFont font) {
  84. if (!Enabled) {
  85. return;
  86. }
  87. int y = 10;
  88. foreach (var toast in toasts) {
  89. spriteBatch.DrawString(font, toast, new Vector2(10, y), Color.Teal);
  90. y += 30;
  91. }
  92. }
  93. public static void Draw(
  94. SpriteBatch spriteBatch, GraphicsDevice graphics, BasicEffect lightingEffect) {
  95. if (!Enabled) {
  96. return;
  97. }
  98. // Draw rects.
  99. foreach (var debugRect in rects) {
  100. var rect = debugRect.Rect;
  101. var color = debugRect.Color;
  102. // top side
  103. spriteBatch.Draw(
  104. whiteTexture, new Rectangle(rect.Left, rect.Top, rect.Width, 1), color);
  105. // bottom side
  106. spriteBatch.Draw(
  107. whiteTexture, new Rectangle(rect.Left, rect.Bottom - 1, rect.Width, 1), color);
  108. // left side
  109. spriteBatch.Draw(
  110. whiteTexture, new Rectangle(rect.Left, rect.Top, 1, rect.Height), color);
  111. // right side
  112. spriteBatch.Draw(
  113. whiteTexture, new Rectangle(rect.Right - 1, rect.Top, 1, rect.Height), color);
  114. }
  115. // Draw lines.
  116. graphics.SetVertexBuffer(vertexBuffer);
  117. vertexBuffer.SetData(lineVertices);
  118. foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
  119. pass.Apply();
  120. graphics.DrawPrimitives(PrimitiveType.LineList, 0, lineIdx / 2);
  121. }
  122. }
  123. }
  124. }