[.net] Passing D3D Device for a class

Started by
2 comments, last by jpetrie 16 years, 10 months ago
Hi! I'm trying to create a Skybox class which would create and draw a skybox. The problem is, how can I tell this class to use my "Microsoft.DirectX.Direct3D.Device device"? I tried to pass it as a parameter for class constructor and methods but it didn't work. I also tried to make this device public but it didn't help either. Here's the class (which is unfinished as you can see):
namespace DirectX_Training
{
    class Skybox
    {
        private string skyboxName;
        private VertexBuffer vertexBuffer;
        private IndexBuffer indexBuffer;
        private CustomVertex.PositionNormalTextured[] vertices;
        private short[] indices;

        //Constructor
        public Skybox(string skyboxName)
        {
            //Skybow will be drawn using vertexBuffer for optimal performance
            vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), 3, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Default);

            //Format and count of vertices
            vertices = new CustomVertex.PositionNormalTextured[5];

            //Here we start defining vertices for the skybox
            vertices[0].Position = new Vector3(-50f, 50f, 50f);
            vertices[1].Position = new Vector3(50f, -50f, 50f);
            vertices[2].Position = new Vector3(-50f, -50f, 50f);

            //And finally, push the information into the vertexBuffer ;)
            vertexBuffer.SetData(vertices, 0, LockFlags.None);



            //Next we have to create indices and indexBuffer
            indexBuffer = new IndexBuffer(typeof(short), 3, device, Usage.WriteOnly, Pool.Default);

            indices = new short[3];
 
            indices[0]=0;
            indices[1]=1;
            indices[2]=2;
 
            indexBuffer.SetData(indices, 0, LockFlags.None);
        }

        public void Draw()
        {
            device.SetStreamSource(0, vertexBuffer, 0);
            device.Indices = indexBuffer;

            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 3, 0, 2);
        }

        public void Erase()
        {

        }
    }
}
I tried to do like this: "public Skybox(string skyboxName, Microsoft.DirectX.Direct3D.Device device)" "Skybox = new Skybox("winter", device)" but it didn't work (as expected, I just tried something). Edit: is here any kind of code tags? That code looks very messy as normal text without coloring and positioning.. EDIT: Added 'source' tags for you. They're listed in the FAQ [wink] [Edited by - jollyjeffers on June 11, 2007 6:47:06 AM]
Advertisement
I'm not aware of any reason why you shouldn't be able to pass references to the device around your code. It's a fairly standard practice.

I'm moving this to the .NET forum as it strikes me that your problem is probably in the usage of C#/.NET than Direct3D. Providing some more details - to be honest, saying "but it didn't work" is next to useless. Help us to help you.


Cheers,
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Ok, I got it working with references. A also noticed that I couldn't pass device as ref to constructor but to all other methods I could. Why's that?

And sorry I don't speak English very well so I have problems presenting my thoughts clearly. :(
Quote:
Ok, I got it working with references. A also noticed that I couldn't pass device as ref to constructor but to all other methods I could. Why's that?

The D3D device is a class (reference) type; you should be able to pass it constructors and use/store it there with no problems. You don't even need the "ref" keyword for this:
public class SomeClass{  Device myDevice;  public SomeClass(Device device)  {    myDevice = device;  }  public void Foo()  {    // use myDevice here  }}


Whatever unexpected behavior you observed is a result of you doing something wrong. However, the code you posted above does not attempt to pass the device (it doesn't even declare it anywhere), so it must be inaccurate. That, coupled with the fact that you still have not described exactly what you mean by general hand-waving such as "it doesn't work," means there is little more I or anybody else can tell you.

Why don't you post the code exactly as you wrote it and post the exact error messages you got when you compiled it. Or, if it was a runtime error, describe exactly what you expected to see, what you saw instead, and when you saw it (use a debugger to pinpoint the line at or around the failure).

This topic is closed to new replies.

Advertisement