From 346e4c4395af554562e76c742598e9bfaf576101 Mon Sep 17 00:00:00 2001 From: Kevin Gadd Date: Mon, 25 Feb 2013 05:24:15 -0800 Subject: [PATCH] Make the buffer argument to LayoutString optional. Fix GeometryBatches with 0 draw calls in them. --- Squared/RenderLib/RenderGeometry.cs | 38 +++++++++++++++-------------- Squared/RenderLib/RenderText.cs | 17 +++++++++---- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/Squared/RenderLib/RenderGeometry.cs b/Squared/RenderLib/RenderGeometry.cs index 778ffc706..4f8912ec3 100644 --- a/Squared/RenderLib/RenderGeometry.cs +++ b/Squared/RenderLib/RenderGeometry.cs @@ -217,30 +217,32 @@ internal static GeometryDrawCallPreparer } public override void Issue (DeviceManager manager) { - using (manager.ApplyMaterial(Material)) - try { - var buffers = _BufferGenerator.GetBuffer(); + if (Count > 0) { + using (manager.ApplyMaterial(Material)) + try { + var buffers = _BufferGenerator.GetBuffer(); -#if PSM - var g = manager.Device._graphics; + #if PSM + var g = manager.Device._graphics; g.SetVertexBuffer(0, buffers); foreach (var da in _DrawArguments) g.DrawArrays(PSSHelper.ToDrawMode(da.PrimitiveType), da.IndexOffset, da.IndexCount, 1); -#else - manager.Device.SetVertexBuffer(buffers.Vertices); - manager.Device.Indices = buffers.Indices; + #else + manager.Device.SetVertexBuffer(buffers.Vertices); + manager.Device.Indices = buffers.Indices; - foreach (var da in _DrawArguments) - manager.Device.DrawIndexedPrimitives(da.PrimitiveType, 0, da.VertexOffset, da.VertexCount, da.IndexOffset, da.PrimitiveCount); -#endif - } finally { -#if PSM - manager.Device._graphics.SetVertexBuffer(0, null); -#else - manager.Device.SetVertexBuffer(null); - manager.Device.Indices = null; -#endif + foreach (var da in _DrawArguments) + manager.Device.DrawIndexedPrimitives(da.PrimitiveType, 0, da.VertexOffset, da.VertexCount, da.IndexOffset, da.PrimitiveCount); + #endif + } finally { + #if PSM + manager.Device._graphics.SetVertexBuffer(0, null); + #else + manager.Device.SetVertexBuffer(null); + manager.Device.Indices = null; + #endif + } } _DrawArgumentsListPool.Release(ref _DrawArguments); diff --git a/Squared/RenderLib/RenderText.cs b/Squared/RenderLib/RenderText.cs index 5e8a353dc..7433a14b7 100644 --- a/Squared/RenderLib/RenderText.cs +++ b/Squared/RenderLib/RenderText.cs @@ -12,13 +12,20 @@ namespace Squared.Render { public static class SpriteFontExtensions { public static ArraySegment LayoutString ( - this SpriteFont font, string text, ArraySegment buffer, + this SpriteFont font, string text, ArraySegment? buffer, Vector2? position = null, Color? color = null, float scale = 1, float sortKey = 0, int characterSkipCount = 0, int characterLimit = int.MaxValue ) { if (text == null) throw new ArgumentNullException("text"); - if (buffer.Count < text.Length) + + ArraySegment _buffer; + if (buffer.HasValue) + _buffer = buffer.Value; + else + _buffer = new ArraySegment(new BitmapDrawCall[text.Length]); + + if (_buffer.Count < text.Length) throw new ArgumentException("buffer too small", "buffer"); var spacing = font.Spacing; @@ -36,7 +43,7 @@ public static class SpriteFontExtensions { float rectScaleX = 1f / glyphSource.Texture.Width; float rectScaleY = 1f / glyphSource.Texture.Height; - int bufferWritePosition = buffer.Offset; + int bufferWritePosition = _buffer.Offset; int drawCallsWritten = 0; bool firstCharacterOfLine = true; @@ -86,7 +93,7 @@ public static class SpriteFontExtensions { actualPosition.Y + (glyph.Cropping.Y + characterOffset.Y) * scale ); - buffer.Array[bufferWritePosition] = drawCall; + _buffer.Array[bufferWritePosition] = drawCall; bufferWritePosition += 1; drawCallsWritten += 1; @@ -100,7 +107,7 @@ public static class SpriteFontExtensions { } return new ArraySegment( - buffer.Array, buffer.Offset, drawCallsWritten + _buffer.Array, _buffer.Offset, drawCallsWritten ); } }