[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

passing Device device in all drawing functions directx9 c#

I am programming some dx9 project. The device parameter anyone must use for drawing functions is created in the GraphicsSample class as protected and passed to the GraphicsClass at the default SDK projects.

Everytime I make a class where something must be drawn the Device parameter seems to end up in the constructor because there is something like the following code happening: (in short there are things to be loaded into device)

eg. class SimpleSprite
public SimpleSprite(Device device)
      texture = TextureLoader.FromFile(device, filename);
      using (Surface s = texture.GetSurfaceLevel(0))
            SurfaceDescription desc = s.Description;
            textureSize = new Rectangle(0, 0, desc.Width, desc.Height);
      sprite = new Sprite(device);

This is getting pretty annoying because if I have a "one to many" relationship I seem to make functions with always the device as parameter (hope at least some of the problem is clear).

Q: How can I get rid of the device parameter? Is it just passed by all graphics classes when anything is drawn. Can't I just make it a global parameter or can I make a base class that contain a child class with the device parameter present.

1 Solution
You could create a static device wrapper class that manages this sort of thing for you.  It might look like this:


class 3DGraphics
    IDirect3D9* m_pD3D;
    IDirect3DDevice9* m_pD3DDevice; // Device ptr

    // Private constructor and destructor for one-time creation:

    // One and only instance of 3DGraphics
    3DGraphics* m_gp3DGraphics;
    // Static functions to create and destroy
    static bool Create()
           return(false); // Ensures only one instance exists...

        m_gpD3DGraphics->m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);

        // create your device here, to m_gpD3DGraphics->m_pD3DDevice

       return(true); // return false if it screws up!
    static void Destroy()
        // Clean up your device & directx9 objects here

    // function to get the device
    static IDirect3DDevice9* GetD3DDevice() {return(m_gpD3DGraphics->m_PD3DDevice);};

then, where you do your creation and destruction of DirectX, do this instead:


#include "3DGraphics.h"

// your code


And whenever you want to get the device... whatever .cpp file you're in

#include "3DGraphics.h"

void someclass::somefunction()
    3DGraphics::GetD3DDevice()->SetRenderState(blah, blah);

DaFouAuthor Commented:
Not c# but shows how its supposed to be done. Thanks Moo-Juice!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now