Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tristan10

Wrong Code Design?

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

Hi everyone, I''ve build a class in c++ called CDraw which hides the DirectDraw specifics for other objects. CDraw has methods like SetResolution, Blt, SetColor etc. How do I give objects that need to have acces to these functions acces? My current method is giving these objects a pointer to CDraw in their constructors, but since their can be 1000s of instances of objects using CDraw in a game, this seems like bad code practicing. Further more, these objects may need acces to CInput, CShow, CPlay etc. later on as well. What''s the common method of giving all these classes acces to them? Tristan

Share this post


Link to post
Share on other sites
Advertisement
you could use singletons, but I prefer to make a global instance of the rendering/input/scene classes. This is since

1) there is one and only going to be one instance of these objects in the engine.
2) You can then do Render.DoSomething() instead of if( NULL!=Render) Render->DoSomething()
3) You explicitly set the constructor/destructor order of these global instances, which may be important. i.e. memory manager class gets created before the debug class ... etc

I have 2 files Globals.h/.cpp.

The globals.h just has the externs to the classes instances.

ie
extern CRender Render;

Then in the .cpp have the instance.

CRender Render;

But it depends on what you prefer.

Share this post


Link to post
Share on other sites
That sound like exactly what I need:
thanks a lot! I''ll start digging into the
"extern" keyword to see how it works.

Again: thanks for putting me on track.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!