Object oriented with Irrlicht

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
Kim2
Posts: 14
Joined: Thu May 31, 2007 12:40 am

Object oriented with Irrlicht

Post by Kim2 »

Hello guys. I am working on a basic physical simulation using Irrlicht in combination with Newton but being new to programming I have run into some problems.

Code: Select all

#include "StdAfx.h"
#include "Newton/physicsman.h"

//________________________________________________________
// Default constructor
Plate::Plate()
{

	printf("\nPlate created.\n");
	mass = 2;
	width = 3;
	length = 4;
    //platePos = (0, 0, 0); 

	mesh = smgr->getMesh("data/smallcube.3ds");
	node = smgr->addAnimatedMeshSceneNode(mesh);

	physicsman.createCube();
}

This is a class for a plate object. My problem is that I don't know how to use the Irrlicht pointer vars outside of the Irrlicht.cpp class, so I can't get the mesh loaded or a node established. I plan to pass the scene node through to Newton.

Here is the Irrlich class

Code: Select all

#include "StdAfx.h"

MastEventReceiver receiver;

// window width and height values
int height = 768;
int width = 1024;

void Irrlicht::Init()
{  
	receiver.init();

	// Init the Irrlicht engine
	device = createDevice(EDT_DIRECT3D9, dimension2d<s32>(width, height), 32, false, true, &receiver);
//	ISoundEngine* engine = createIrrKlangDevice();
//			engine->play2D("data/track.wav", true);
	driver = device->getVideoDriver();
	smgr = device->getSceneManager();
	guienv = device->getGUIEnvironment();

	// add the camera
	cam = smgr->addCameraSceneNode();
	cam->setFarValue(90000.0f);

	scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
	camera->setPosition(core::vector3df(-50,50,-150));

	// disable mouse cursor
	device->getCursorControl()->setVisible(false);
}

void Irrlicht::CreateSky()
{
	smgr->addSkyBoxSceneNode(driver->getTexture("data/zolsky05_up.jpg"),
driver->getTexture("data/zolsky05_dn.jpg"),driver->getTexture("data/zolsky05_lf.jpg"),
driver->getTexture("data/zolsky05_rt.jpg"),driver->getTexture("data/zolsky05_fr.jpg"),
driver->getTexture("data/zolsky05_bk.jpg"));
}

void Irrlicht::MainLoop()
{  

	device->setEventReceiver(&receiver);

	Render();

	// Update newton 100 times / second
	//if (device->getTimer()->getTime() > lasttick + 10) {	
	//	lasttick = device->getTimer()->getTime();
	//	NewtonUpdate(nWorld, 0.01f);
	//}

}

void Irrlicht::Render()
{
	receiver.endEventProcess(); 
	ReadInput();

	driver->beginScene(true, true, video::SColor(0,220,220,255));

	smgr->drawAll();
	guienv->drawAll();
		
	driver->endScene();
	receiver.startEventProcess();
}

void Irrlicht::ReadInput()
{
	if(receiver.keyPressed(KEY_SPACE))
	{
		printf("Space pressed.\n");
	}
}

Code: Select all

#include "StdAfx.h"

#if !defined(IRRLICHT_H)
#define IRRLICHT_H

class Irrlicht
{

public:
	// Irrlicht  vars
	IVideoDriver* driver;
	ISceneManager* smgr;
	IGUIEnvironment* guienv;
	IrrlichtDevice *device;
//	ISoundEngine* engine;
	ICameraSceneNode *cam;

    void Init();
	void CreateSky();
	void MainLoop();
	void Render();
	void ReadInput();
};
#endif
And here is my main.cpp:

Code: Select all

#include "StdAfx.h"
#include "Irrlicht.h"
#include "singletons.h"

Irrlicht Sys;
World GameWorld;

int main(int argc, char* argv[])
{
	Sys.Init();
	physicsman.Init();

	Sys.CreateSky();
	GameWorld.Create();
		
	
	// Main Loop
	while(Sys.device->run())
	{
		Sys.MainLoop();
	}

	// Clean up memory
	//Sys.Finish();

	return 0;
}
Also, being new to this, I would appreciate any general advice- am I going about things the right way? I have implemented my physics manager as a singleton. There is meant to be a World class which creates the skybox etc. but I was having the same problem, not knowing how to access the Irrlicht vars.

Thank you very much for your time.

tl;dr how do I use driver, device, smgr etc in any place in my program beyond the class in which they were declared? I thought just including my Irrlicht.h file should allow this?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

you could do something like this:

Code: Select all

#include "StdAfx.h"
#include "Newton/physicsman.h"

#include "Irrlicht.h" // !!! you should use another name !!!
extern Irrlicht Sys;

//______________________________________________________________________________

// Default constructor
Plate::Plate(){

   printf("\nPlate created.\n");
   mass = 2;
   width = 3;
   length = 4;
    //platePos = (0, 0, 0);

   mesh = Sys.smgr->getMesh("data/smallcube.3ds");
   node = Sys.smgr->addAnimatedMeshSceneNode(mesh);

   physicsman.createCube();
}
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Kim2
Posts: 14
Joined: Thu May 31, 2007 12:40 am

Post by Kim2 »

Code: Select all

#include "StdAfx.h"
#include "Newton/physicsman.h"
#include "IrrlichtMan.h"
#include "Plate.h"

extern IrrlichtMan Sys;

// Default constructor
Plate::Plate()
{

	printf("\nPlate created.\n");
	mass = 2;
	width = 3;
	length = 4;
    //platePos = (0, 0, 0); 

	mesh = smgr->getMesh("data/smallcube.3ds");
	node = smgr->addAnimatedMeshSceneNode(mesh);

	physicsman.createCube();
}
I tried that as per your suggestion but it didn't change anything, still get the errors. For example:

"error C2065: 'smgr' : undeclared identifier"

Thanks for your quick response.

Here is the header file for Plate:

Code: Select all


#ifndef PLATE_H
#define PLATE_H

class Plate
{
  public:

		//______________________________________________________________________
		// Default contructor
        //______________________________________________________________________
        Plate();

        //______________________________________________________________________
        // Destructor
        // Gets called when the object goes out of scope
        //______________________________________________________________________	  
        virtual ~Plate() {};

		float getMass() { return mass; }
		int getWidth() { return width; }
		int getLength() { return length; }
		dVector getPivot() { return pivotOrient; }

		IAnimatedMesh *mesh;
		ISceneNode *node;

 private:
		
		float mass;		// mass in grams
		int width;
		int length;

		dVector pivotOrient;	// orientation of pivot point
		dVector pivotPos;       // position of pivot point
		dVector platePos;       // plate COM

}; // end Plate
#endif
Kim2
Posts: 14
Joined: Thu May 31, 2007 12:40 am

Post by Kim2 »

Never mind, got it. Should have been:

Code: Select all

	
mesh = Sys.smgr->getMesh("data/smallcube.3ds");
node = Sys.smgr->addAnimatedMeshSceneNode(mesh);
Thanks for your help!
Post Reply