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

x
  • 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.

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

3DGraphics.h


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

    // Private constructor and destructor for one-time creation:
    3DGraphics();
    ~3DGraphics();

    // One and only instance of 3DGraphics
    3DGraphics* m_gp3DGraphics;
public:
    // Static functions to create and destroy
    static bool Create()
    {
        if(m_gpD3DGraphics)
           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:

main.cpp


#include "3DGraphics.h"

if(3DGraphics::Create())
{
// your code

3DGraphics::Destroy();
};

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);
};



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

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