コード例 #1
0
        public void DrawBatch(SpriteSortMode sortMode)
        {
            // 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;
            }

            // setup the vertexArray array
            int       startIndex = 0;
            int       index      = 0;
            Texture2D tex        = null;

            // make sure the vertexArray has enough space
            if (_batchItemList.Count * 4 > _vertexArray.Length)
            {
                ExpandVertexArray(_batchItemList.Count);
            }

            //Populate the vertexArray
            for (int i = 0; i < _batchItemList.Count; i++)
            {
                var item = _batchItemList[i];

                // store the SpriteBatchItem data in our vertexArray
                _vertexArray[index++] = item.vertexTL;
                _vertexArray[index++] = item.vertexTR;
                _vertexArray[index++] = item.vertexBL;
                _vertexArray[index++] = item.vertexBR;

                _freeBatchItemQueue.Enqueue(item);
            }

            //Get and fill the PssVertexBuffer
            var vertexBuffer = _device.GetVertexBuffer(_vertexFormat, 4 * InitialVertexArraySize, 6 * InitialVertexArraySize);

            vertexBuffer.SetIndices(_index, 0, 0, 6 * InitialVertexArraySize);
            _device._graphics.SetVertexBuffer(0, vertexBuffer);
            vertexBuffer.SetVertices(_vertexArray, 0, 0, index);


            startIndex = index = 0;

            //Draw each batch in the sprite batch (based on texture changes)
            for (int i = 0; i < _batchItemList.Count; i++)
            {
                var item = _batchItemList[i];
                // if the texture changed, we need to draw and bind the new texture
                bool shouldFlush = item.Texture != tex;
                if (shouldFlush)
                {
                    DrawVertexArray(startIndex, index);
                    startIndex = index;
                    tex        = item.Texture;

                    _device._graphics.SetTexture(0, tex._texture2D);
                }
                index += 4;
            }

            // flush the remaining vertexArray data
            DrawVertexArray(startIndex, index);

            _batchItemList.Clear();
        }