public void End(Effect effect) { if (Batches.Count == 0) { return; } if (sortMode != SpriteBatch.SpriteSortMode.Immediate && sortMode != SpriteBatch.SpriteSortMode.Deffered) { Batches.Sort((x, y) => { int first = y.sortingKey.CompareTo(x.sortingKey); return(first); }); } //Array.Sort (Batches, 0, Batches.Count); Texture currentTexture = Batches.First().texture; int batchStart = 0, batchCount = 0; ushort bufferIndex = 0; ushort indicesIndex = 0; foreach (BatchItem item in Batches) { if (item.texture != currentTexture || batchCount == MAX_BATCH) { Flush(effect, currentTexture, batchStart, batchCount); currentTexture = item.texture; batchStart = batchCount = 0; indicesIndex = bufferIndex = 0; } indexData[indicesIndex++] = (ushort)(bufferIndex + 0); indexData[indicesIndex++] = (ushort)(bufferIndex + 1); indexData[indicesIndex++] = (ushort)(bufferIndex + 2); indexData[indicesIndex++] = (ushort)(bufferIndex + 1); indexData[indicesIndex++] = (ushort)(bufferIndex + 3); indexData[indicesIndex++] = (ushort)(bufferIndex + 2); vertexData[bufferIndex++] = new VertexPositionColorTexture(item.positions[0], item.color, item.texTopLeft); vertexData[bufferIndex++] = new VertexPositionColorTexture(item.positions[1], item.color, new Vector2(item.texBottomRight.X, item.texTopLeft.Y)); vertexData[bufferIndex++] = new VertexPositionColorTexture(item.positions[2], item.color, new Vector2(item.texTopLeft.X, item.texBottomRight.Y)); vertexData[bufferIndex++] = new VertexPositionColorTexture(item.positions[3], item.color, item.texBottomRight); batchCount++; } if (batchCount > 0) { Flush(effect, currentTexture, batchStart, batchCount); } }