Sign in to follow this  

Managing the Direct3D Device

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I have a Sprite class. And in this sprite class there is an IDirect3DTexture8 pointer and a IDirect3DVertexBuffer8 pointer. To create these objects I need access to the IDirect3DDevice in some fassion. Passing a copy of the Device pointer to every Sprite instance seems like a bad way to manage this, and I was curious if there are any better ways to organize the code. Maybe pass a pointer to an already created vertex buffer and texture as constructor parameters? But the sprite also needs access to the device to SetTransforms and to render itself and the like. Any references, links, books, etc would be helpful. Thanks

Share this post


Link to post
Share on other sites
When I had things orginized like this I just made the device [or rather a class that contained the device] global.

Recently I changed things so that the sprite class doesn't hold its own texture and buffer info, it just interfaces with something else that handles that sort of stuff.

Share this post


Link to post
Share on other sites
You have three options if you require the device every frame.

1) Pass it in as you are doing. Saves memory (if you have a lot of sprites) but increases function complexity.
2) Pass it in to the constructor and save it. Increases memory usage but gives quick access with less function complexity.
3) Use a Singleton class that gives access to these important variables. Such as pGraphicsManager->GetDevice(). Adds indirection, but saves memory and function complexity.

I'd pick the way that best suits you and go with that. Another option would be to create a global variable Device and set it to the address of pGraphicsManager->pDevice after you load it. This removes the indirection, but it increases the chance for programmer error. You may forget to update the Device variable after you create the device or somewhere else.

Good luck,
Chris

Share this post


Link to post
Share on other sites
Yep. Or have a "Factory" which plonks the device pointer into the Sprite objects it makes.

It's all a matter of which OO design constructs fit your overall app architecture best and what's going to be more extensible/maintainable within your plans.

At the end of the day, all the methods suggested either turn into a pointer internal to every sprite or a pointer external to every sprite.

IMO with OO you need to consider what fits best with the bigger picture: how the object is going to be used in the context of the whole system, and how it may be extended - if you don't do that you'll regularly find yourself painted into corners or uncertain of which way to go.

Share this post


Link to post
Share on other sites

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this