good object oriented design

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
biotech
Posts: 37
Joined: Sat Jan 31, 2004 1:46 pm

good object oriented design

Post by biotech »

how can i design my classes so scene manager can be accessed by other classes.

i have now the class in which all the models are loaded and rendered,but things got to complicated,i have many models and textures and i thought that the best should be to write clasess that will handle similiar things.like player class,that should handle all player model loading,weapons class that should handle all weapons loading,etc...but how can i access scnene manager from these classess?i tried many things,but i think that i should rewrite all from the scratch :( so what is the best way to do this.

please reply thanx.
ich bin ein berliner
YeahBabyYeah

Post by YeahBabyYeah »

I alway make a class that holds a pointer to the driver smgr etc.
then I make the class a Singleton so I can access it from every part of the code.

Actually I think it would be nice if the IrrDevice was a singleton :wink:
biotech
Posts: 37
Joined: Sat Jan 31, 2004 1:46 pm

Post by biotech »

please could you explain some more
or write a little of pseudo code,please
thanks
ich bin ein berliner
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

pass it to object as constructor argument:

Code: Select all

class Example
{
   irr::scene::IAnimatedMesh *mesh;
   irr::scene::IAnimatedMeshSceneNode *node;

public:
   Example(irr::IrrlichtDevice *device,  const irr::c8 *filename)
   {
      mesh=device->getSceneManager()->getMesh(filename);
      node=evice->getSceneManager()->addAnimatedMeshSceneNode(mesh);
   }

      
   ~Example() {}
};
mm765
Posts: 172
Joined: Fri May 28, 2004 10:12 am

Post by mm765 »

you should look up inheritance , composition and polymorphism in a book or google for it to find examples how to do it.
see this site for a good book (thinking in c++ by bruce eckel), which is also available for download.
:!:

Post by :!: »

check this out

http://home.earthlink.net/~huston2/dp/patterns.html

and many more on google "C++ design pattern" :)
Thulsa Doom
Posts: 63
Joined: Thu Aug 05, 2004 9:40 am
Location: Germany

Post by Thulsa Doom »


Hi,

I use this base class:

Code: Select all


class Constants 
{ 
public: 
   Constants(irr::IrrlichtDevice* Device) : device(Device) 
   { 
      drvr   =   device->getVideoDriver(); 
      smgr =   device->getSceneManager(); 
   }; 
   irr::IrrlichtDevice*                    device; 
   irr::video::IVideoDriver*           drvr; 
   irr::scene::ISceneManager*      smgr; 
}; 

Has someone tried another approach?
http://irrlicht.sourceforge.net/phpBB2/ ... php?t=3423
Guest

Post by Guest »

Maybe I don't understand the question, but if the only thing you want to do ist to have the scene manager available for everyone, why don't you simply make smgr a global variable? And then declare it on the beggining of each .cpp filr? Like:

irr::whatever::SceneManager *smgr;
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post by Robomaniac »

a) Globals are bad
b) its bad design
c) u'd get redefinition errors


Look at ICE
The Robomaniac
Project Head / Lead Programmer
Centaur Force
cmoibenlepro
Posts: 237
Joined: Thu May 27, 2004 3:18 pm
Location: Canada

Post by cmoibenlepro »

a) Globals are bad
b) its bad design
good to know :wink: (I was doing the error of using globals...)

why it's a bad design? :?:
Serapth
Posts: 5
Joined: Mon Aug 16, 2004 2:41 pm

Post by Serapth »

As to why globals are bad...

Well, for one, they are global. This runs into a whole potential slew of redefinition errors, etc...

No gaurantee of state. Its hard to gaurantee when a global is initialized, so either you have potentially uninit'ed variables, or you have to put a ton of "just in case" tests to see if its properly set, or you have potentially volitile code.

As above, with a global variable, it could also be deleted or corupted at any point in your code, which for one makes debugging hell.

Hard to tell, or properly control when, or if, memory is released.

Global makes for name collision issues, as the global variable namespace will be available at every scope. Using a unique identifier such as g_myVar is a fairly effective work around.

In almost every case, a singleton is a better method to use then a global. Really all a singleton is, is a global object wrapper around an instance variable. The largest benefits are 1) you are gauranteed that the object varaible has been initialized 2) memory isnt allocated until first use 3) slightly more namespace friendly.

The biggest downside, is the minor overhead to the call... which can be minimalized to a large extent in most cases. If however, the varaible is going to be called a number of times in a fairly tight loop, you are better of creating a local reference and using it.


Cheers.
Post Reply