Sign in to follow this  
chameleon_789

[C#] creating a global device

Recommended Posts

Hi.. first off, I'm a noob at both C# and DX coding so bear with me if I miss out something important. I'm trying to write a simple app which creates a grid of vertices and manipulates them in realtime. I've created a user control which is a DX area based on a picturebox which works OK, my problem is that no matter how I declare / initialize the device, whenever I try to create a vertex buffer I get a NullReferenceException error (in the locals window, the device I'm referencing is null). I've tried lots of different methods to no avail but currently, I declare the device in a static class and pass that to the user control to initialize it.. I assumed that because it's static it would be globally accessible with this code : vb = new VertexBuffer(typeof(CustomVertex.PositionColored), gW * gH, D3DDevice.DXDevice, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default); // (D3DDevice.DXDevice is the static device I declared). I've also tried to get the device from the user control in which it's initialized, but that doesn't work either. Is there a better method of declaring/initializing/accessing a Global device?

Share this post


Link to post
Share on other sites
Hmm.. some code might help paint a clearer picture.

I declare the device like this :


public static class D3DInterface
{
public static Device m_device;
}



Then pass the global device to this function in my d3ddrawable class with this code :

d3ddrawable.initD3D(D3DInterface.m_device)



public bool initD3D(Device dev)
{
try
{

m_device = dev; //link to the global device handle

// etc etc..

return true;
}
catch (DirectXException)
{
return false;
}
}


Which works, I get a working DX area. Now, when I try to create a vertexbuffer in a seperate class, passing the global device I passed to the aforementioned function, or using the device returned from d3ddrawable gives a NullExceptionError. I am missing something obvious? I've only learned scraps here and there from random tutorials so I wouldn't be surprised if I have missed the point entirely..

Share this post


Link to post
Share on other sites
You don't need to pass your device as a method parameter as it's global anyways.
You could just do something like d3ddrawable.initD3D(D3DInterface.m_device) which you already did and keep on referencing the device as that.

I hope this helps.
Take care.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this