Debug: draw rects as 4 GPU-accelerated lines.

GitOrigin-RevId: 3286d0476b5032d785f74191609db4fbd768433c
This commit is contained in:
Colin McMillen 2020-02-15 16:31:59 -05:00
parent 48a9297a57
commit 1ffe1444b2
2 changed files with 16 additions and 43 deletions

View File

@ -27,20 +27,16 @@ namespace SemiColinGames {
} }
public static bool Enabled = true; 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. // This is a LinkedList instead of a List because SetFpsText() adds to its front.
static readonly LinkedList<string> toasts = new LinkedList<string>(); static readonly LinkedList<string> toasts = new LinkedList<string>();
static readonly List<DebugRect> rects = new List<DebugRect>(); // 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 int lineIdx = 0;
static readonly VertexPositionColor[] lineVertices = new VertexPositionColor[MAX_LINE_VERTICES]; static readonly VertexPositionColor[] lineVertices = new VertexPositionColor[MAX_LINE_VERTICES];
static VertexBuffer vertexBuffer; static VertexBuffer vertexBuffer;
static Texture2D whiteTexture; public static void Initialize(GraphicsDevice graphics) {
public static void Initialize(GraphicsDevice graphics, Texture2D white) {
whiteTexture = white;
vertexBuffer = new VertexBuffer( vertexBuffer = new VertexBuffer(
graphics, typeof(VertexPositionColor), MAX_LINE_VERTICES, BufferUsage.WriteOnly); graphics, typeof(VertexPositionColor), MAX_LINE_VERTICES, BufferUsage.WriteOnly);
} }
@ -56,7 +52,6 @@ namespace SemiColinGames {
public static void Clear(bool paused) { public static void Clear(bool paused) {
toasts.Clear(); toasts.Clear();
if (!paused) { if (!paused) {
rects.Clear();
lineIdx = 0; lineIdx = 0;
} }
} }
@ -71,7 +66,10 @@ namespace SemiColinGames {
} }
public static void AddRect(Rectangle rect, Color color) { 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) { public static void AddRect(AABB box, Color color) {
@ -109,31 +107,14 @@ namespace SemiColinGames {
} }
} }
public static void Draw( public static void Draw(GraphicsDevice graphics, BasicEffect lightingEffect) {
SpriteBatch spriteBatch, GraphicsDevice graphics, BasicEffect lightingEffect) {
if (!Enabled) { if (!Enabled) {
return; return;
} }
if (lineIdx == 0) {
// Draw rects. return;
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);
} }
// Draw lines.
graphics.SetVertexBuffer(vertexBuffer); graphics.SetVertexBuffer(vertexBuffer);
vertexBuffer.SetData(lineVertices); vertexBuffer.SetData(lineVertices);
foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) { foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {

View File

@ -33,7 +33,6 @@ namespace SemiColinGames {
int framesToSuppress = 2; int framesToSuppress = 2;
Texture2D grasslandBg1; Texture2D grasslandBg1;
Texture2D grasslandBg2; Texture2D grasslandBg2;
Texture2D whiteTexture;
Player player; Player player;
World world; World world;
@ -57,6 +56,8 @@ namespace SemiColinGames {
display.Initialize(Window, graphics); display.Initialize(Window, graphics);
display.SetFullScreen(fullScreen); display.SetFullScreen(fullScreen);
Debug.Initialize(GraphicsDevice);
sceneTarget = new RenderTarget2D( sceneTarget = new RenderTarget2D(
GraphicsDevice, camera.Width, camera.Height, false /* mipmap */, GraphicsDevice, camera.Width, camera.Height, false /* mipmap */,
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24); GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
@ -79,6 +80,7 @@ namespace SemiColinGames {
// Called once per game. Loads all game content. // Called once per game. Loads all game content.
protected override void LoadContent() { protected override void LoadContent() {
base.LoadContent();
spriteBatch = new SpriteBatch(GraphicsDevice); spriteBatch = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("font"); font = Content.Load<SpriteFont>("font");
@ -87,20 +89,13 @@ namespace SemiColinGames {
linesOfSight = new LinesOfSight(GraphicsDevice); linesOfSight = new LinesOfSight(GraphicsDevice);
grasslandBg1 = Content.Load<Texture2D>("grassland_bg1"); grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
grasslandBg2 = Content.Load<Texture2D>("grassland_bg2"); grasslandBg2 = Content.Load<Texture2D>("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. // Called once per game. Unloads all game content.
protected override void UnloadContent() { protected override void UnloadContent() {
whiteTexture.Dispose(); base.UnloadContent();
updateTimer.DumpStats(); updateTimer.DumpStats();
drawTimer.DumpStats(); drawTimer.DumpStats();
base.UnloadContent();
} }
// Updates the game world. // Updates the game world.
@ -193,10 +188,7 @@ namespace SemiColinGames {
linesOfSight.Draw(player, world.CollisionTargets, GraphicsDevice, lightingEffect); linesOfSight.Draw(player, world.CollisionTargets, GraphicsDevice, lightingEffect);
// Draw debug rects & lines on top. // Draw debug rects & lines on top.
spriteBatch.Begin( Debug.Draw(GraphicsDevice, lightingEffect);
SpriteSortMode.Deferred, null, SamplerState.LinearWrap, null, null, null, transform);
Debug.Draw(spriteBatch, GraphicsDevice, lightingEffect);
spriteBatch.End();
// Draw sceneTarget to screen. // Draw sceneTarget to screen.
GraphicsDevice.SetRenderTarget(null); GraphicsDevice.SetRenderTarget(null);