From 40ca0402d60142c106867c1400640ae237af7ce6 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 10 Dec 2020 17:38:18 -0800 Subject: [PATCH] instance data struct --- Data/InstanceData.cs | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Data/InstanceData.cs 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; + } + } +}