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.

LVL 2
DaFouAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DaFouAuthor Commented:
Not c# but shows how its supposed to be done. Thanks Moo-Juice!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.