61 lines
1.4 KiB
C#
61 lines
1.4 KiB
C#
|
using System.Collections.Generic;
|
||
|
using Microsoft.Xna.Framework.Graphics;
|
||
|
|
||
|
namespace Kav
|
||
|
{
|
||
|
public class InstanceData<T> 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<T> 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;
|
||
|
}
|
||
|
}
|
||
|
}
|