Debug.DrawLines: use GPU to draw lines.

Fixes #19.

GitOrigin-RevId: d837e0ddafa0b7ea66fea4bba05ca7412a3c5726
This commit is contained in:
Colin McMillen 2020-02-15 15:47:05 -05:00
parent a21802e9f2
commit 5d21ff2a0f
3 changed files with 35 additions and 15 deletions

View File

@ -27,15 +27,22 @@ 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>(); static readonly List<DebugRect> rects = new List<DebugRect>();
static readonly List<DebugLine> lines = new List<DebugLine>(); static int lineIdx = 0;
static readonly VertexPositionColor[] lineVertices = new VertexPositionColor[MAX_LINE_VERTICES];
static VertexBuffer vertexBuffer;
static Texture2D whiteTexture; static Texture2D whiteTexture;
public static void Initialize(Texture2D white) { public static void Initialize(GraphicsDevice graphics, Texture2D white) {
whiteTexture = white; whiteTexture = white;
vertexBuffer = new VertexBuffer(
graphics, typeof(VertexPositionColor), MAX_LINE_VERTICES, BufferUsage.WriteOnly);
} }
public static void WriteLine(string s) { public static void WriteLine(string s) {
@ -50,7 +57,7 @@ namespace SemiColinGames {
toasts.Clear(); toasts.Clear();
if (!paused) { if (!paused) {
rects.Clear(); rects.Clear();
lines.Clear(); lineIdx = 0;
} }
} }
@ -75,15 +82,20 @@ namespace SemiColinGames {
} }
public static void AddLine(Point start, Point end, Color color) { public static void AddLine(Point start, Point end, Color color) {
lines.Add(new DebugLine(start, end, color)); if (lineIdx >= MAX_LINE_VERTICES) {
return;
}
lineVertices[lineIdx] = new VertexPositionColor(new Vector3(start.X, start.Y, 0), color);
lineVertices[lineIdx + 1] = new VertexPositionColor(new Vector3(end.X, end.Y, 0), color);
lineIdx += 2;
} }
public static void AddLine(int p1x, int p1y, int p2x, int p2y, Color color) { public static void AddLine(int p1x, int p1y, int p2x, int p2y, Color color) {
lines.Add(new DebugLine(new Point(p1x, p1y), new Point(p2x, p2y), color)); AddLine(new Point(p1x, p1y), new Point(p2x, p2y), color);
} }
public static void AddLine(Vector2 start, Vector2 end, Color color) { public static void AddLine(Vector2 start, Vector2 end, Color color) {
lines.Add(new DebugLine(start.ToPoint(), end.ToPoint(), color)); AddLine(start.ToPoint(), end.ToPoint(), color);
} }
public static void DrawToasts(SpriteBatch spriteBatch, SpriteFont font) { public static void DrawToasts(SpriteBatch spriteBatch, SpriteFont font) {
@ -97,10 +109,13 @@ namespace SemiColinGames {
} }
} }
public static void Draw(SpriteBatch spriteBatch) { public static void Draw(
SpriteBatch spriteBatch, GraphicsDevice graphics, BasicEffect lightingEffect) {
if (!Enabled) { if (!Enabled) {
return; return;
} }
// Draw rects.
foreach (var debugRect in rects) { foreach (var debugRect in rects) {
var rect = debugRect.Rect; var rect = debugRect.Rect;
var color = debugRect.Color; var color = debugRect.Color;
@ -117,12 +132,13 @@ namespace SemiColinGames {
spriteBatch.Draw( spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Right - 1, rect.Top, 1, rect.Height), color); whiteTexture, new Rectangle(rect.Right - 1, rect.Top, 1, rect.Height), color);
} }
foreach (var line in lines) {
Point[] points = Line.Rasterize(line.Start, line.End); // Draw lines.
foreach (var point in points) { graphics.SetVertexBuffer(vertexBuffer);
spriteBatch.Draw( vertexBuffer.SetData(lineVertices);
whiteTexture, new Rectangle(point.X, point.Y, 1, 1), line.Color); foreach (EffectPass pass in lightingEffect.CurrentTechnique.Passes) {
} pass.Apply();
graphics.DrawPrimitives(PrimitiveType.LineList, 0, lineIdx / 2);
} }
} }
} }

View File

@ -35,6 +35,7 @@ namespace SemiColinGames {
float angle = -fov / 2 + fovStep * i; float angle = -fov / 2 + fovStep * i;
Vector2 rotated = ray.Rotate(angle); Vector2 rotated = ray.Rotate(angle);
Vector2 closestHit = Vector2.Add(eyePos, rotated); Vector2 closestHit = Vector2.Add(eyePos, rotated);
Debug.AddLine(eyePos, closestHit, Color.Yellow);
float hitTime = 1f; float hitTime = 1f;
Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f); Vector2 halfTileSize = new Vector2(World.TileSize / 2.0f, World.TileSize / 2.0f);

View File

@ -90,7 +90,7 @@ namespace SemiColinGames {
whiteTexture = new Texture2D(GraphicsDevice, 1, 1); whiteTexture = new Texture2D(GraphicsDevice, 1, 1);
whiteTexture.SetData(new Color[] { Color.White }); whiteTexture.SetData(new Color[] { Color.White });
Debug.Initialize(whiteTexture); Debug.Initialize(GraphicsDevice, whiteTexture);
} }
// Called once per game. Unloads all game content. // Called once per game. Unloads all game content.
@ -99,6 +99,8 @@ namespace SemiColinGames {
updateTimer.DumpStats(); updateTimer.DumpStats();
drawTimer.DumpStats(); drawTimer.DumpStats();
base.UnloadContent();
} }
// Updates the game world. // Updates the game world.
@ -182,7 +184,8 @@ namespace SemiColinGames {
world.Draw(spriteBatch); world.Draw(spriteBatch);
// Draw debug rects & lines. // Draw debug rects & lines.
Debug.Draw(spriteBatch); lightingEffect.Projection = camera.Projection;
Debug.Draw(spriteBatch, GraphicsDevice, lightingEffect);
// Aaaaand we're done. // Aaaaand we're done.
spriteBatch.End(); spriteBatch.End();