instance data struct
parent
0ebad486c5
commit
40ca0402d6
|
@ -0,0 +1,60 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue