From 1ffe1444b25811a1bf65461118ac454305a99b99 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Sat, 15 Feb 2020 16:31:59 -0500 Subject: [PATCH] Debug: draw rects as 4 GPU-accelerated lines. GitOrigin-RevId: 3286d0476b5032d785f74191609db4fbd768433c --- Shared/Debug.cs | 41 +++++++++++------------------------------ Shared/SneakGame.cs | 18 +++++------------- 2 files changed, 16 insertions(+), 43 deletions(-) diff --git a/Shared/Debug.cs b/Shared/Debug.cs index df02aa8..b4cc752 100644 --- a/Shared/Debug.cs +++ b/Shared/Debug.cs @@ -27,20 +27,16 @@ namespace SemiColinGames { } public static bool Enabled = true; - // Lines in excess of MAX_LINES get dropped on the floor. - const int MAX_LINES = 1000; - const int MAX_LINE_VERTICES = MAX_LINES * 2; // This is a LinkedList instead of a List because SetFpsText() adds to its front. static readonly LinkedList toasts = new LinkedList(); - static readonly List rects = new List(); + // Lines in excess of MAX_LINES get dropped on the floor. + const int MAX_LINES = 2000; + const int MAX_LINE_VERTICES = MAX_LINES * 2; static int lineIdx = 0; static readonly VertexPositionColor[] lineVertices = new VertexPositionColor[MAX_LINE_VERTICES]; static VertexBuffer vertexBuffer; - static Texture2D whiteTexture; - - public static void Initialize(GraphicsDevice graphics, Texture2D white) { - whiteTexture = white; + public static void Initialize(GraphicsDevice graphics) { vertexBuffer = new VertexBuffer( graphics, typeof(VertexPositionColor), MAX_LINE_VERTICES, BufferUsage.WriteOnly); } @@ -56,7 +52,6 @@ namespace SemiColinGames { public static void Clear(bool paused) { toasts.Clear(); if (!paused) { - rects.Clear(); lineIdx = 0; } } @@ -71,7 +66,10 @@ namespace SemiColinGames { } public static void AddRect(Rectangle rect, Color color) { - rects.Add(new DebugRect(rect, color)); + AddLine(rect.Left, rect.Top, rect.Right, rect.Top, color); + AddLine(rect.Right, rect.Top, rect.Right, rect.Bottom, color); + AddLine(rect.Right, rect.Bottom, rect.Left, rect.Bottom, color); + AddLine(rect.Left, rect.Bottom, rect.Left, rect.Top, color); } public static void AddRect(AABB box, Color color) { @@ -109,31 +107,14 @@ namespace SemiColinGames { } } - public static void Draw( - SpriteBatch spriteBatch, GraphicsDevice graphics, BasicEffect lightingEffect) { + public static void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) { if (!Enabled) { return; } - - // Draw rects. - foreach (var debugRect in rects) { - var rect = debugRect.Rect; - var color = debugRect.Color; - // top side - spriteBatch.Draw( - whiteTexture, new Rectangle(rect.Left, rect.Top, rect.Width, 1), color); - // bottom side - spriteBatch.Draw( - whiteTexture, new Rectangle(rect.Left, rect.Bottom - 1, rect.Width, 1), color); - // left side - spriteBatch.Draw( - whiteTexture, new Rectangle(rect.Left, rect.Top, 1, rect.Height), color); - // right side - spriteBatch.Draw( - whiteTexture, new Rectangle(rect.Right - 1, rect.Top, 1, rect.Height), color); + if (lineIdx == 0) { + return; } - // Draw lines. graphics.SetVertexBuffer(vertexBuffer); vertexBuffer.SetData(lineVertices); foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) { diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index ca648ef..1414097 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -33,7 +33,6 @@ namespace SemiColinGames { int framesToSuppress = 2; Texture2D grasslandBg1; Texture2D grasslandBg2; - Texture2D whiteTexture; Player player; World world; @@ -57,6 +56,8 @@ namespace SemiColinGames { display.Initialize(Window, graphics); display.SetFullScreen(fullScreen); + Debug.Initialize(GraphicsDevice); + sceneTarget = new RenderTarget2D( GraphicsDevice, camera.Width, camera.Height, false /* mipmap */, GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24); @@ -79,6 +80,7 @@ namespace SemiColinGames { // Called once per game. Loads all game content. protected override void LoadContent() { + base.LoadContent(); spriteBatch = new SpriteBatch(GraphicsDevice); font = Content.Load("font"); @@ -87,20 +89,13 @@ namespace SemiColinGames { linesOfSight = new LinesOfSight(GraphicsDevice); grasslandBg1 = Content.Load("grassland_bg1"); grasslandBg2 = Content.Load("grassland_bg2"); - - whiteTexture = new Texture2D(GraphicsDevice, 1, 1); - whiteTexture.SetData(new Color[] { Color.White }); - Debug.Initialize(GraphicsDevice, whiteTexture); } // Called once per game. Unloads all game content. protected override void UnloadContent() { - whiteTexture.Dispose(); - + base.UnloadContent(); updateTimer.DumpStats(); drawTimer.DumpStats(); - - base.UnloadContent(); } // Updates the game world. @@ -193,10 +188,7 @@ namespace SemiColinGames { linesOfSight.Draw(player, world.CollisionTargets, GraphicsDevice, lightingEffect); // Draw debug rects & lines on top. - spriteBatch.Begin( - SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform); - Debug.Draw(spriteBatch, GraphicsDevice, lightingEffect); - spriteBatch.End(); + Debug.Draw(GraphicsDevice, lightingEffect); // Draw sceneTarget to screen. GraphicsDevice.SetRenderTarget(null);