Example #1
0
        /// <summary>
        /// Resize and recreate the missing indices for the index and vertex position color buffers.
        /// </summary>
        /// <param name="numBatchItems"></param>
        private unsafe void EnsureArrayCapacity(int numBatchItems)
        {
            int neededCapacity = 6 * numBatchItems;

            if (_index != null && neededCapacity <= _index.Length)
            {
                // Short circuit out of here because we have enough capacity.
                return;
            }
            short[] newIndex = new short[6 * numBatchItems];
            int     start    = 0;

            if (_index != null)
            {
                _index.CopyTo(newIndex, 0);
                start = _index.Length / 6;
            }

            fixed(short *indexFixedPtr = newIndex)
            {
                var indexPtr = indexFixedPtr + (start * 6);

                for (var i = start; i < numBatchItems; i++, indexPtr += 6)
                {
                    /*
                     *  TL    TR
                     *   0----1 0,1,2,3 = index offsets for vertex indices
                     *   |   /| TL,TR,BL,BR are vertex references in SpriteBatchItem.
                     *   |  / |
                     *   | /  |
                     *   |/   |
                     *   2----3
                     *  BL    BR
                     */
                    // Triangle 1
                    *(indexPtr + 0) = (short)(i * 4);
                    *(indexPtr + 1) = (short)(i * 4 + 1);
                    *(indexPtr + 2) = (short)(i * 4 + 2);
                    // Triangle 2
                    *(indexPtr + 3) = (short)(i * 4 + 1);
                    *(indexPtr + 4) = (short)(i * 4 + 3);
                    *(indexPtr + 5) = (short)(i * 4 + 2);
                }
            }

            _index = newIndex;

            _vertexArray = new VertexPositionColorTexture[4 * numBatchItems];

            indexBuffer?.Dispose();
            vertexBuffer?.Dispose();
            indexBuffer = new IndexBuffer(_device, IndexElementSize.SixteenBits, _index.Length, BufferUsage.WriteOnly);
            indexBuffer.SetData(_index);
            vertexBuffer = new VertexBuffer(_device, VertexPositionColorTexture.VertexDeclaration, _vertexArray.Length, BufferUsage.WriteOnly);
        }
Example #2
0
 public override void Dispose()
 {
     disposeChilderen();
     if (indexBuffer != null)
     {
         indexBuffer.Dispose();
         indexBuffer = null;
     }
     base.Dispose();
 }
Example #3
0
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources
 /// </summary>
 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 protected override void Dispose(bool disposing)
 {
     if (!IsDisposed)
     {
         //Dispose of managed resources
         if (disposing)
         {
             if (_indexBuffer != null)
             {
                 _indexBuffer.Dispose();
             }
         }
     }
     base.Dispose(disposing);
 }