Move code

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
gustav05
Posts: 10
Joined: Tue Mar 21, 2006 3:19 pm
Location: Germany

Move code

Post by gustav05 »

Hello!

I had a problem:
I will move the code for the terrain creating:

Code: Select all

  scene::ITerrainSceneNode* terrain =  
    smgr->addTerrainSceneNode("heightmap.tga");
  terrain->setScale(core::vector3df(40, 4.4f, 40));
  terrain->setMaterialFlag(video::EMF_LIGHTING, false);
  terrain->setMaterialTexture(0, driver->getTexture("stein8.jpg"));
  terrain->setMaterialTexture(1, driver->getTexture("leaves.jpg"));
  terrain->setMaterialType(video::EMT_DETAIL_MAP);
  terrain->scaleTexture(80.0f, 40.0f);
in the header-file "scene.h". This terrain creating procedure I want call with a simple function, like "makeTerrain();" than in the main.cpp. But I don`t know how to realize this, the compiler gives me varius error messages.

EDIT:
BTW: Is it a better way to make a class instead to call a function?

I hope you can help me.

Thank you.
Ced666
Posts: 86
Joined: Fri Jan 13, 2006 10:29 am
Location: Belgium

Post by Ced666 »

Show the code where the errors are and post the exact error messages.
BTW: Is it a better way to make a class instead to call a function?
Depends. If you want to add a class just for adding a class, then no, it is useless. I think you first need some more experience in object oriented programming to see the advantages.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You should move it to a .cpp file, not to a .h file. Because it is an implementation of the method, not it's declaration/signature. Even if you use classes and want implicit inlining the posted code seems to be inappropriate for inclusion in the class definition. And yes, knwoledge of the syntax might help here already. Or some experience with at least a non-Basic language.
gustav05
Posts: 10
Joined: Tue Mar 21, 2006 3:19 pm
Location: Germany

Post by gustav05 »

It is not so that I never heard anything about OOP. I already trusted in OOP.
But there I had a problem that confusing me.

The code of scene.cpp:

Code: Select all

void makeScene()
{ 
  scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode("heightmap.tga");;
  terrain->setScale(core::vector3df(40, 4.4f, 40));
  terrain->setMaterialFlag(video::EMF_LIGHTING, false);
  terrain->setMaterialTexture(0, driver->getTexture("stein8.jpg"));
  terrain->setMaterialTexture(1, driver->getTexture("leaves.jpg"));
  terrain->setMaterialType(video::EMT_DETAIL_MAP);
  terrain->scaleTexture(80.0f, 40.0f);
}
main.cpp

Code: Select all

#include <cstdlib>
#include <iostream.h>
#include <irrlicht.h>

#include "scene.cpp"

using namespace std;
using namespace irr;
using namespace core;
using namespace gui;
using namespace io;
using namespace scene;
using namespace video;

int main(int argc, char *argv[])
{
    IrrlichtDevice *device = createDevice(EDT_OPENGL, 
    dimension2d<s32>(640, 480), 16, false, false, false, 0);
  IVideoDriver *driver = device->getVideoDriver();
  ISceneManager *smgr = device->getSceneManager();
  IGUIEnvironment *guienv = device->getGUIEnvironment();
  makeScene();
[...]
}
The error messages:
`smgr' undeclared (first use this function)
´driver' undeclared (first use this function)
and so on
I knew what the messages mean, but I don`t know why they appear and how to fix them.

Thank you.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Sorry to say, but you obviously don't know what they mean, nor what OOP is. You define the variable smgr in the scope of the main function. Thus, smgr (and all others) are only available within the loop. So either pass them to the other methods, or declare them global and move the declarations into a header file.
And your coding has nothing to do with OOP.
mmh771
Posts: 39
Joined: Thu Apr 27, 2006 7:02 am

Post by mmh771 »

Move the Stars to the Left, not to the Right!..

IVideoDriver *driver (WRONG)
IVideoDriver* driver (Right)
Ced666
Posts: 86
Joined: Fri Jan 13, 2006 10:29 am
Location: Belgium

Post by Ced666 »

@mmh771: Both are perfectly correct ! This doesn't change anything.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Unless you're programming Fortran or some other weird language you can safely ignore whitespaces. Use them to structure your code. In fact this is not true in each situation, e.g. nested templates need a space between the closing angles, but in your example there is definitely no difference.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

You declared and created the device, scene manager and gui environment within main and then you call makeScene()...
But the devices are only avilable within the main function !!!
Declare them as global vars or pass them to the makeScene function...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Hey Acki, I think that is exactly what I've posted 16 hours ago :wink:
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Right...
But it doesn't change the fact that this is the error... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
gustav05
Posts: 10
Joined: Tue Mar 21, 2006 3:19 pm
Location: Germany

Post by gustav05 »

Yes I know that it has nothing to do with OOP.

Thank you for your help. That was the thing I forgot, that the scene manager was declared only in the main function. poop happens.
Thanks.
Post Reply