passing Device device in all drawing functions directx9 c#

Posted on 2004-11-11
Last Modified: 2010-04-07
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.

Question by:DaFou
    LVL 1

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

    LVL 2

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
    Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now