Jump to content
  • Advertisement
Sign in to follow this  
chameleon_789

[C#] creating a global device

This topic is 4084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!