public void DrawBatch(SpriteSortMode sortMode, Effect effect)
{
// nothing to do
if ( _batchItemList.Count == 0 )
return;
// sort the batch items
switch ( sortMode )
{
case SpriteSortMode.Texture :
_batchItemList.Sort( CompareTexture );
break;
case SpriteSortMode.FrontToBack :
_batchItemList.Sort ( CompareDepth );
break;
case SpriteSortMode.BackToFront :
_batchItemList.Sort ( CompareReverseDepth );
break;
}
// Determine how many iterations through the drawing code we need to make
int batchIndex = 0;
int batchCount = _batchItemList.Count;
unchecked
{
_device._graphicsMetrics._spriteCount += (ulong)batchCount;
}
// Iterate through the batches, doing short.MaxValue sets of vertices only.
while(batchCount > 0)
{
// setup the vertexArray array
var startIndex = 0;
var index = 0;
Texture2D tex = null;
int numBatchesToProcess = batchCount;
if (numBatchesToProcess > MaxBatchSize)
{
numBatchesToProcess = MaxBatchSize;
}
EnsureArrayCapacity(numBatchesToProcess);
// Draw the batches
for(int i = 0; i < numBatchesToProcess; i++, batchIndex++)
{
SpriteBatchItem item = _batchItemList[batchIndex];
// if the texture changed, we need to flush and bind the new texture
var shouldFlush = !ReferenceEquals(item.Texture, tex);
if (shouldFlush)
{
FlushVertexArray(startIndex, index, effect, tex);
tex = item.Texture;
startIndex = index = 0;
_device.Textures[0] = tex;
}
// store the SpriteBatchItem data in our vertexArray
_vertexArray[index++] = item.vertexTL;
_vertexArray[index++] = item.vertexTR;
_vertexArray[index++] = item.vertexBL;
_vertexArray[index++] = item.vertexBR;
// Release the texture and return the item to the queue.
item.Texture = null;
_freeBatchItemQueue.Enqueue(item);
}
// flush the remaining vertexArray data
FlushVertexArray(startIndex, index, effect, tex);
// Update our batch count to continue the process of culling down
// large batches
batchCount -= numBatchesToProcess;
}
_batchItemList.Clear();
}