diff --git a/Data/InstanceData.cs b/Data/InstanceData.cs new file mode 100644 index 0000000..97b7746 --- /dev/null +++ b/Data/InstanceData.cs @@ -0,0 +1,60 @@ +using System.Collections.Generic; +using Microsoft.Xna.Framework.Graphics; + +namespace Kav +{ + public class InstanceData where T : struct, IVertexType + { + public T[] InstanceDataArray { get; } + public DynamicVertexBuffer VertexBuffer { get; } + public int InstanceCount { get; private set; } + + public InstanceData(GraphicsDevice graphicsDevice, int size) + { + InstanceDataArray = new T[size]; + + VertexBuffer = new DynamicVertexBuffer( + graphicsDevice, + typeof(T), + size, + BufferUsage.WriteOnly + ); + + InstanceCount = 0; + } + + public void AddAndSetData(IEnumerable data) + { + InstanceCount = 0; + foreach (var datum in data) + { + AddData(datum); + } + + if (InstanceCount == 0) { throw new System.Exception(); } + + SetData(); + } + + public void AddData(T datum) + { + InstanceDataArray[InstanceCount] = datum; + InstanceCount += 1; + } + + public void SetData() + { + VertexBuffer.SetData( + InstanceDataArray, + 0, + InstanceCount, + SetDataOptions.NoOverwrite + ); + } + + public void Clear() + { + InstanceCount = 0; + } + } +}