C++ -> Functions/Splitting up code etc.

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
seppuku arts
Posts: 17
Joined: Mon Oct 30, 2006 12:39 pm

C++ -> Functions/Splitting up code etc.

Post by seppuku arts »

Okay, I should have done C++ tutorials before starting anything in Irrlicht but the tutorials were so good I found myself learning C++ at the same time ;)

But anyway, I want to be able to access code anywhere, like you would in a 'void' but for its insides to be applicable anywhere, for example;

The top part of my code (the rest of main is cut off)

Code: Select all

void setup()
{
      IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2d<s32>(800, 600), 32, false, true, true, 0);
    
       video::IVideoDriver* driver = device->getVideoDriver();
       scene::ISceneManager* smgr = device->getSceneManager();
       IGUIEnvironment* guienv = device->getGUIEnvironment(); 
}

int main()
{
    
    setup();
    

    
    
             
    
    //Load the quake 3 Map
    //device->getFileSystem()->addZipFileArchive("media/map-20kdm2.pk3");
    //Load the mesh inside the pk3 file
    scene::IAnimatedMesh* q3levelmesh = smgr->getMesh("room1.dmf");
    scene::ISceneNode* q3node = 0;
Here one of my errors is 'smgr' undeclared (first use this function), but in the void it is declared

It seems that the void is not letting smgr to be available to the rest of the code.
Running into walls is not a good idea...Trust me
seppuku arts
Posts: 17
Joined: Mon Oct 30, 2006 12:39 pm

Post by seppuku arts »

Nevermind (Second time I've done that ;))

I declared all of the variables globally in the code; so something like this;

Code: Select all

IrrlichtDevice *device = 0;
video::IVideoDriver* driver = 0;
scene::ISceneManager* smgr = 0;
IGUIEnvironment* guienv = 0;
scene::IAnimatedMesh* q3levelmesh = 0;
scene::ISceneNode* q3node = 0;
scene::ITriangleSelector* selector = 0;
void setup()
{
       device = createDevice(EDT_OPENGL, dimension2d<s32>(800, 600), 32, false, true, true, 0);
    
       driver = device->getVideoDriver();
       smgr = device->getSceneManager();
       guienv = device->getGUIEnvironment();
       
       
       
    
}

void loadscene()
{
     //Load the quake 3 Map
    //device->getFileSystem()->addZipFileArchive("media/map-20kdm2.pk3");
    //Load the mesh inside the pk3 file
    q3levelmesh = smgr->getMesh("room1.dmf");
    
    if (q3levelmesh)
       q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));
       
       if (q3node)
          q3node->setPosition(core::vector3df(-1300,-144,-1249));
          
          
              
    
   
    	
	
	if (q3node)
	{		
 
		q3node->setPosition(core::vector3df(-1370,-130,-1400));

		selector = smgr->createOctTreeTriangleSelector(q3levelmesh->getMesh(0), q3node, 128);
		q3node->setTriangleSelector(selector);
		selector->drop();
	}
	
}
    

int main()
{
    setup();
    loadscene();
Running into walls is not a good idea...Trust me
Coolkat88
Posts: 30
Joined: Tue Oct 03, 2006 3:14 pm

Post by Coolkat88 »

that would work. but you may find it easier to pass by reference and using pointers so when you DO get more advanced with whatever project you are working on, you will be able to handle the device and what not alittle easier.. especially if for say you are using other header files you could potentially find yourself in a situation that in another function you are making a call to one of your global variables that when the function is CALL it's already a declared function but the compiler looks at the code and will not see the variable that you are trying to use.

just something to look into..

Code: Select all

void setup(IrrlichtDevice *device)
{
      device->createDevice(...);
}

int main()
{
    IrrlichtDevice *device;
   setup(device);

   while(device->run())
   {
          //...
   }

   device->drop();
}
sorta.. kinda.. yeah.
seppuku arts
Posts: 17
Joined: Mon Oct 30, 2006 12:39 pm

Post by seppuku arts »

Thanks, I tried your method, however, using the DevC++ hello world tutorial (I tried this to test it before executing in my own code)

But it crashes, the commands that are in the void are:

Code: Select all

     irrDevice = createDevice(EDT_SOFTWARE, dimension2d<s32>(512, 384),32, false,false,0);
     irrDevice->setWindowCaption(L"Dev-C++ and the Irrlicht Engine!");
And they seem to work fine as the window does execute with that caption, my assumption is that the problem lies within the loop 'while (irrDevice->run())

I think I'll keep my globals until I am more coherent with C++, I've got some good beginner C++ tutorials, I'll study them and hopefully I'll know what I need to know once globals become an issue. But thats dude.
Running into walls is not a good idea...Trust me
Warchief
Posts: 204
Joined: Tue Nov 22, 2005 10:58 am

Post by Warchief »

I wouldnt call that C++ code, i would call it C code.
Post Reply