Page 1 of 1

Move code

Posted: Wed Apr 26, 2006 2:25 pm
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.

Posted: Wed Apr 26, 2006 2:31 pm
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.

Posted: Wed Apr 26, 2006 2:37 pm
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.

Posted: Wed Apr 26, 2006 3:27 pm
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.

Posted: Wed Apr 26, 2006 8:22 pm
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.

Posted: Thu Apr 27, 2006 7:42 am
by mmh771
Move the Stars to the Left, not to the Right!..

IVideoDriver *driver (WRONG)
IVideoDriver* driver (Right)

Posted: Thu Apr 27, 2006 7:57 am
by Ced666
@mmh771: Both are perfectly correct ! This doesn't change anything.

Posted: Thu Apr 27, 2006 7:59 am
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.

Posted: Thu Apr 27, 2006 10:37 am
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...

Posted: Thu Apr 27, 2006 11:04 am
by hybrid
Hey Acki, I think that is exactly what I've posted 16 hours ago :wink:

Posted: Thu Apr 27, 2006 11:27 am
by Acki
Right...
But it doesn't change the fact that this is the error... ;)

Posted: Thu Apr 27, 2006 11:45 am
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.