I am writing a library that works alongside XNA. I have a base class for primitives, from which I plan to build Planes, Cubes and other primitive-types. Ideally I’d like my base class to do the rendering, regardless of the vertex-type used.
Relevant code:
public abstract class Primitive<VT> where VT : IVertexType
{
private void Draw(GraphicsDevice graphics)
{
graphics.DrawUserIndexedPrimitives<VT>(primitiveType_,
vertices_,
0,
vertices_.Length,
indices_,
0,
primitiveCount_);
}
}
Now, other classes than derive from this, using the appropriate vertex-type:
public abstract class Plane<VT> : Primitive<VT> where VT : IVertextTpye { ... }
public class PlaneColored : Primitive<VertexPositionColor> { .... }
public class PlaneTextured : Primitive<VertexPositionTexture> { .... }
The problem is, I get a compile error on the DrawUserIndexPrimitives<> call:
Error 1 The type 'VT' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Microsoft.Xna.Framework.Graphics.GraphicsDevice.DrawUserIndexedPrimitives<T>(Microsoft.Xna.Framework.Graphics.PrimitiveType, T[], int, int, short[], int, int)' C:\dev\Projects\2010\XNAParts\XNAParts\Parts\Primitive.cs 88
And I can’t change the construct to a struct otherwise the DrawUserIndexPrimitives’ generic parameter won’t work (as it’s not a struct).
Is there any way around this?
Thanks in advance!
How about just changing
Primitiveto require thatVTis a struct?and similarly
You claim that “DrawUserIndexPrimitives generic parameter won’t work (as it’s not a struct)” but it’s not clear what you mean by that. Which parameter? I suspect the above is what you want, but it’s not really clear.