Jump to content
  • Advertisement
Sign in to follow this  
ekba89

Making an object accessible by other classes

This topic is 2133 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

Since we all know global variables are bad I am trying to find a way to make an object accessible by others. So in particular my graphics device which contains basically directx interface. So a lot of my classes are using this graphics device class in different functions so I don't think passing the pointer in the function is the best idea. So I was thinking about making a base class for all the classes using graphics device. And making this class contain the graphics device. But I dont know if this is the best idea so I am looking for suggestions. I didnt want to implement singleton since there might be more than one graphics device.

Share this post


Link to post
Share on other sites
Advertisement

Its better to send in your dependencies to the constructor of the class that needs to use the class than using singletons.

 

As the users of the class would not be a graphics devices its wrong to use a base class.. its not a "is a" relationship

Share this post


Link to post
Share on other sites
I use "friend" classes for this and keep full conistency in the types of variables (private, public).
When the pointer is the only thing I need of the device class, I just use a pointer to the device. With this you have to keep in mind that you have enough checks on the validity (valid or not) of the pointer.

If you want you can share your high over classes design, so I can react on it based on how I do it at the moment (not that thats perfect though, just a creative hobbyist over here :))

Share this post


Link to post
Share on other sites


So a lot of my classes are using this graphics device class in different functions

A graphics device is quite a low level concept. What kind of classes are these? Are you certain they need to talk to the graphics device directly? This kind of problem is sometimes caused by inappropriate dependencies. Consider introducing architectures such as MVC (model, view, controller) to reduce the direct dependencies.

Share this post


Link to post
Share on other sites

Its better to send in your dependencies to the constructor of the class that needs to use the class than using singletons.

 

As the users of the class would not be a graphics devices its wrong to use a base class.. its not a "is a" relationship

 

That said, a singleton sounds better than deriving from the Graphics device class.  (Though I agree, passing in the graphics device at the objects creation/initialization/setup is probably the way to go.)

Share this post


Link to post
Share on other sites

 


So a lot of my classes are using this graphics device class in different functions

A graphics device is quite a low level concept. What kind of classes are these? Are you certain they need to talk to the graphics device directly? This kind of problem is sometimes caused by inappropriate dependencies. Consider introducing architectures such as MVC (model, view, controller) to reduce the direct dependencies.

 

For now classes like mesh, shader, material uses the graphics device class.

 

I use "friend" classes for this and keep full conistency in the types of variables (private, public).
When the pointer is the only thing I need of the device class, I just use a pointer to the device. With this you have to keep in mind that you have enough checks on the validity (valid or not) of the pointer.

If you want you can share your high over classes design, so I can react on it based on how I do it at the moment (not that thats perfect though, just a creative hobbyist over here smile.png)

I dont have any design at the moment. I just started a new game engine with directx 11. I made a small engine with directx 9 before but I was sending my graphics device pointer to all the functions that are using graphics device and I didnt like that approach. So I am trying to find a good approach before moving any further in the engine. I dont understand what you mean by friend. If I friend my graphics device class in lets say my material class material class will have access to all the internals of graphics device which is not good.

 

 

Its better to send in your dependencies to the constructor of the class that needs to use the class than using singletons.

 

As the users of the class would not be a graphics devices its wrong to use a base class.. its not a "is a" relationship

I think I will go with sending it in constructor approach if I cant find anything better than that.

Share this post


Link to post
Share on other sites

You probably want to give the material to a high level renderer that will bind your material using your graphics device (low level renderer that communicates with dx11, dx9, opengl or whatever)

 

Mesh and material would be high level stuff while your low level renderer would work with stuff like vbos and textures

Edited by Nanook

Share this post


Link to post
Share on other sites

If you take rip-off's comment and Nanook's comments together, they're quite spot on here.  It sounds to me as though you've approached separating code into classes using an extreme separation of concerns.  What I might suggest is you have a rendering system that does the main bulk of what it implies, renders.  You have a set of other classes that hold data specific to certain rendering needs such as vertex buffers, index buffers, vertex declarations, shader programs, etc.  

 

If you think about the bigger picture here, your render system and all these classes that hold data can be abstracted one level farther to where you have a common interface the engine exposes for them with implementation specific classes for DX9, DX11, and OpenGL.  Your API asks the render system to allocate you an object, the implementation specific class does so and gives you the object back via interface.  You work with a vertex buffer the same way regardless if its DX9, DX11, OpenGL or w/e.  

 

Now you no longer need to be concerned with passing this device handle everywhere because its centralized, primarily used by the specific render system implementation.  On those corner cases where it makes sense for a separate class to perhaps hold a reference, just pass it as a constructor argument.  

 

This doesn't necessarily blur the lines with separation of concerns and single responsibility principals and keeps your code quite clean.

Edited by crancran

Share this post


Link to post
Share on other sites

while your meshes and materials and textures may contain specific dx constructs (such as buffers) they should not be in charge of drawing themselves... These resources should be referenced by your game objects or entities and you should go through your entities every frame and have them submit draw requests to the renderer where they can give the appropriate information for rendering

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!