Jump to content
  • Advertisement
Sign in to follow this  
Skeleton_V@T

New (Or at least I think so) way to manage objects

This topic is 4833 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 every bodies!. I'd say I've only learned to develop DirectX-based programs for a few months (to make games, of course) and have digged the Internet for games source code. As far as I've known, when programming games, people often create some global variables (like IDirect3D9, IDirect3DDevice9 etc, etc ...). I myself - for some reasons - don't like global variables - thought of some other ways to deal with global variables and to make my 'Main.cpp' file portable from games to games or with a very little changes. My opinion may not new, but as I see, none of the people have done it: In my 'Main.cpp', I create one (and the only one) class that manages all of my game's element and initialize it in the WinMain (), call it's LiveMyLife () procedure (or so-so). It's that simple, no other global/local variables required, my 'Main.cpp' remains unchanged from games to games. My CGameWindow class is the one which owns ALL the other objects (Objects, Sound, Input etc ...) as member variables. (In fact, with objects I have a CObjectsManager class - but don't need to mention it here). Let's assume I have some classes that need accessing to pD3DDevice (which is the member variable of the CGameWindow), I need to pass this pD3DDevice in the constructor of that object. Normally, if I declared D3DDevice as a global variables, this is easy. But in this case, I will pass pD3DDevice as a *reference* variable to the object, like this: class CSomeClass { public: //Constructor CSomeClass (interface *&IDirect3DDevice9 rpD3DDevice) : m_rpD3DDevice (rpD3DDevice) {} private: //Variables interface IDirect3DDevice9 *&m_rpD3DDevice ; } ; You got the idea ;) ?. When pass it as a reference (and so on to other objects), ALL the member variables is declared and managed by only one class: pCGameWindow. And in the 'Main.cpp', I only have to call delete () one time for pCGameWindow, simple heh ? I *think* this should be easier to manage, it creates a hierachical structure, when other objects need their parent's allocated member variables, just pass it as a reference, and everything is neat and tidy. What do you think, eve'ry (constructive) opinion is highly appreciated (Remember, I'm still learning DirectX, though got accquanted with C++ quite a long time). Thank you in advance :D

Share this post


Link to post
Share on other sites
Advertisement
It should work fine, but you'll have to be careful about deleting your device when other objects are still pointing to the pointer. It also might be cleaner if you use pointer-to-pointers, not pointer-to-references.
I usually just pass the IDirect3DDevice9 pointer around, and AddRef() it when you need to keep a hold of it. That way, if the device is released and an object still has a pointer to it, it won't all die horribly when the object tries to call some function on the device.

Share this post


Link to post
Share on other sites
Thanks for the reply. I'm a speed/optimization/stability hunger, I used to pass pointer-to-pointer to an object (such as textures array), but this way I think is kind of redundancy 'cause the program creates a copy of that pointer ----> that's for memory saving (although it is very small one)
For stability: I only delete the member objects in call to Destructor function. That's it, one new () for Constructor, and one delete () for Destructor. Things should go safety here. When the pCGameWindow's destructing itself, it in call get it's member objects destruct them selves too.

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!