C++ OOP question

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
StickyBit
Posts: 94
Joined: Mon Jul 05, 2004 3:40 am
Location: Sønderup, Denmark
Contact:

C++ OOP question

Post by StickyBit »

Hi Guys

Still learning C++, and I just ran my head against the wall. Say I would like to make X copies of the same scenenode - then everyone would say "you just need to make a class". Okay.. I dig that, but how? :-)

This is some example code that I would like to resuse:

Code: Select all

mesh = smgr->getMesh("house4/house4.b3d");
node = smgr->addOctTreeSceneNode(mesh); 
node->setScale(core::vector3df(1.5f, 1.5f, 1.5f));
node->setPosition(core::vector3df(0, 0, 0));
node->setMaterialFlag(video::EMF_FOG_ENABLE, false);
node->setMaterialFlag(video::EMF_LIGHTING, true);
selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128);
node->setTriangleSelector(selector);
metaselector->addTriangleSelector(selector);
selector->drop();
... where the used vars is created like this, somewhere higher up the code:

scene::ISceneManager* smgr = device->getSceneManager();
scene::IAnimatedMesh* mesh = 0;
scene::ISceneNode* node = 0;
scene::IMetaTriangleSelector* metaselector = smgr->createMetaTriangleSelector();
scene::ITriangleSelector* selector;

Ok. So far so good. Now this is close to what I would like to be able to do outside the class (let call it items) containing the "House" node creationcode:

Code: Select all

house = items->addhouse(4);
house->position(x, y, z);
house->scale(i, j, k);
house->rotation(bla bla bla);
house->drop();

house = items->addhouse(8);
house->position(x, y, z);
house->scale(i, j, k);
house->rotation(bla bla bla);
house->drop();

// And when the houses works, lots of other items will be added.

lamp = items->addlamp();
lamp->position(x, y, z);
lamp->scale(i, j, k);
lamp->rotation(bla bla bla);
lamp->drop();
You follow? (offcourse you do) :-)

How do I white that code? How do I gain access to the vars used in the main program, from within the class? Could someone please help me out here?

Regards Stickbit
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

i don'T get what the number in addIHouse is for....so i just made the funktion like u wanted but yeah the number has no effect at all

Code: Select all

class CItem
{
       public:
                CItem(irr::IrrlichtDevice* device) : m_device(device)
                {}
                ~CItem(void){}
                irr::scene::ISceneNode* addHouse(irr::s32 i)
                {
                       irr::scene::ISceneManager* smgr = m_device->getSceneManager();
                       irr::scene::IAnimatedMesh* mesh = 0;
                       irr::scene::ISceneNode* node = 0;
                       irr::scene::IMetaTriangleSelector* metaselector =                                               smgr->createMetaTriangleSelector();
                       irr::scene::ITriangleSelector* selector; 
                       mesh = smgr->getMesh("house4/house4.b3d");
                       node = smgr->addOctTreeSceneNode(mesh);
                       node->setScale(core::vector3df(1.5f, 1.5f, 1.5f));
                       node->setPosition(core::vector3df(0, 0, 0));
                       node->setMaterialFlag(video::EMF_FOG_ENABLE, false);
                       node->setMaterialFlag(video::EMF_LIGHTING, true);
                       selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128);
                       node->setTriangleSelector(selector);
                       metaselector->addTriangleSelector(selector);
                       selector->drop();
                       return node;
                }
                irr::IrrlichtDevice* m_device;
};
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
strale
Posts: 119
Joined: Wed Nov 23, 2005 1:58 pm
Location: Lambrugo Italy
Contact:

Post by strale »

If you do not want to let upper class/code see the Irrlicht driver

Code: Select all

 
  CItem(irr::IrrlichtDevice* device) : m_device(device)
you could use a singleton to embedded the Irrlicht functionality you want to use into the classes that use the graphic

so the code could get like this :

Code: Select all

 
class CItem 
{ 
       public: 
                CItem() 
                {  
                   GUIClass * SingeltonGui = GUIClass::GetGuiClass() ; 
                   ......
                 } 
GUIClass is the singleton that has all the functrionality of the irrlicht driver and you have the same driver wherever you istantiate the GUIClass
StickyBit
Posts: 94
Joined: Mon Jul 05, 2004 3:40 am
Location: Sønderup, Denmark
Contact:

Post by StickyBit »

Thanks Sudi .. It looks just like what I need - but I´m not sure how to use it. :-)

I´ve tried different things .. but I just can´t figure out how to use the class.

Code: Select all

irr:scene::ISceneNode* house = CItem::addHouse(s32(5));
.. was as close as I could get - but it complains about a missing object - which probaly is fair anough. A hint please.

Btw .. the number is just for choosing between diffent houses.

Regards Stickybit
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

I guess you have forgotten to declare the CItem object.

Code: Select all

CItem* item = new CItem(device);
irr:scene::ISceneNode* house = item->addHouse(s32(5));
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

jepp Perceval is right
...do what he did
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
StickyBit
Posts: 94
Joined: Mon Jul 05, 2004 3:40 am
Location: Sønderup, Denmark
Contact:

Post by StickyBit »

Thanks guys. It actually works quite well .. so I will use this setup for holding all my reuseable models. There is one error though: The metaselector dosn´t seem to have any effect - since I can walk though the faces of the models.

The code currently looks like this:

Code: Select all

   class CItem { 

      public: 

         CItem(irr::IrrlichtDevice* device) : m_device(device) {} 

         ~CItem(void){} 

         irr::scene::ISceneNode* addHouse(irr::s32  House,
                                          vector3df Scale, 
                                          vector3df Position,
                                          vector3df Rotation) { 

            irr::scene::ISceneManager* smgr = m_device->getSceneManager(); 
            irr::scene::IAnimatedMesh* mesh = 0; 
            irr::scene::ISceneNode* node = 0; 
            irr::scene::IMetaTriangleSelector* metaselector = smgr->createMetaTriangleSelector(); 
            irr::scene::ITriangleSelector* selector; 

            char Model[50];              
            sprintf(Model, "Houses/House%d/house%d.b3d", House, House);
                       
            mesh = smgr->getMesh(Model); 
            node = smgr->addOctTreeSceneNode(mesh); 
                       
            node->setScale(Scale); 
            node->setPosition(Position);
            node->setRotation(Rotation); 

            node->setMaterialFlag(video::EMF_FOG_ENABLE, false); 
            node->setMaterialFlag(video::EMF_LIGHTING, true); 
                       
            selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128); 
            node->setTriangleSelector(selector); 
            metaselector->addTriangleSelector(selector); 
            selector->drop(); 
                       
            return node; 

         } 

         irr::IrrlichtDevice* m_device; 

   };


   CItem* items = new CItem(device);
       
   node = items->addHouse(s32(1), core::vector3df(1, 1, 1),
                                  core::vector3df(1000, 500, 0),
                                  core::vector3df(0, 180, 0));
   node = items->addHouse(s32(2), core::vector3df(1, 1, 1), 
                                  core::vector3df(1000, 500, 2000), 
                                  core::vector3df(0, 180, 0));

   // And so on ...

I guess that the following line:

Code: Select all

irr::scene::IMetaTriangleSelector* metaselector = smgr->createMetaTriangleSelector();

.. should be changed into something like ... smgr->getMetaTriangleSelector(); ... for reusing the existing metaselector, but as far as I can se - no such function exists. Do you agree with me on that one?

Regards Stickybit
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

I see two solutions :
- you declare the metaselector as global
- or you create it where you want in the code and you pass it as an argument in addHouse()
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Code: Select all

class CItem {

      public:

         CItem(irr::IrrlichtDevice* device, irr::scene::IMetaTriangleSelector* metaselector) : m_device(device), m_metaselector(metaselector) {}

         ~CItem(void){}

         irr::scene::ISceneNode* addHouse(irr::s32  House,
                                          vector3df Scale,
                                          vector3df Position,
                                          vector3df Rotation) {

            irr::scene::ISceneManager* smgr = m_device->getSceneManager();
            irr::scene::IAnimatedMesh* mesh = 0;
            irr::scene::ISceneNode* node = 0;
            irr::scene::ITriangleSelector* selector;

            char Model[50];             
            sprintf(Model, "Houses/House%d/house%d.b3d", House, House);
                       
            mesh = smgr->getMesh(Model);
            node = smgr->addOctTreeSceneNode(mesh);
                       
            node->setScale(Scale);
            node->setPosition(Position);
            node->setRotation(Rotation);

            node->setMaterialFlag(video::EMF_FOG_ENABLE, false);
            node->setMaterialFlag(video::EMF_LIGHTING, true);
                       
            selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node, 128);
            node->setTriangleSelector(selector);
            m_metaselector->addTriangleSelector(selector);
            selector->drop();
                       
            return node;

         }

         irr::IrrlichtDevice* m_device;
         irr::scene::IMetaTriangleSelector* m_metaselector;

   }; 
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Post Reply