Skip to content

Commit

Permalink
Make the buffer argument to LayoutString optional.
Browse files Browse the repository at this point in the history
Fix GeometryBatches with 0 draw calls in them.
  • Loading branch information
kg committed Feb 25, 2013
1 parent 427d725 commit 346e4c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
38 changes: 20 additions & 18 deletions Squared/RenderLib/RenderGeometry.cs
Expand Up @@ -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);
Expand Down
17 changes: 12 additions & 5 deletions Squared/RenderLib/RenderText.cs
Expand Up @@ -12,13 +12,20 @@
namespace Squared.Render {
public static class SpriteFontExtensions {
public static ArraySegment<BitmapDrawCall> LayoutString (
this SpriteFont font, string text, ArraySegment<BitmapDrawCall> buffer,
this SpriteFont font, string text, ArraySegment<BitmapDrawCall>? 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<BitmapDrawCall> _buffer;
if (buffer.HasValue)
_buffer = buffer.Value;
else
_buffer = new ArraySegment<BitmapDrawCall>(new BitmapDrawCall[text.Length]);

if (_buffer.Count < text.Length)
throw new ArgumentException("buffer too small", "buffer");

var spacing = font.Spacing;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -100,7 +107,7 @@ public static class SpriteFontExtensions {
}

return new ArraySegment<BitmapDrawCall>(
buffer.Array, buffer.Offset, drawCallsWritten
_buffer.Array, _buffer.Offset, drawCallsWritten
);
}
}
Expand Down

0 comments on commit 346e4c4

Please sign in to comment.