Reputation: 2051
Can anyone please help. Im following a tutorial on drawing primitives from the MSDN found here.
I am trying to re-factor the code a small bit and to that end I have created a class called MyPrimitives in which I have placed all of the code from the tutorial. Then from e.g. Game11 Initialize() I can just call myPrimitive.Initialize() etc.
But I get a NullReferenceException at this line of code in MyPrimitives CreateVertexBuffer() method:
vertexBuffer = new VertexBuffer(
graphics.GraphicsDevice,
vertexDeclaration,
number_of_vertices,
BufferUsage.None
);
And CreateVertexBuffer() is called from MyPrimitives Initialize() method like so:
public void Initialize()
{
CreateVertexBuffer();
}
And THIS Initialize() is called from Game1 Initialize() like so:
protected override void Initialize()
{
myPrimitiveDrawer = new MyPrimitiveDrawer();
myPrimitiveDrawer.Initialize();
base.Initialize();
}
I know the problem is cause I have not set my graphis [GraphicsDeviceManager graphics] to an instance of an object, but how do I actually do this? I have tried:
public void Initialize()
{
graphics = new GraphicsDeviceManager(this); // Tried this
CreateVertexBuffer();
}
But that just gives an error of invalid arguments.
Does anyone have any suggestion please?
Upvotes: 1
Views: 549
Reputation: 678
Start with your main game class and make it extend Microsoft.Xna.Framework.Game
public class Game : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
protected override void Initialize()
{
graphics = new GraphicsDeviceManager(this);
base.Initialize();
}
}
Then you just need to make sure that all of your classes uses the same GraphicsDeviceManager when the project expand.
Upvotes: 2