can this be done?

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
Athlon_Jedi
Posts: 156
Joined: Wed Jul 21, 2004 4:29 am
Location: Mishawaka, In

can this be done?

Post by Athlon_Jedi »

is it possable to make say

smgr = device->getSceneManager();

a global variable so that it can be declared once like say in your main.cpp and called from any other class or node or source file simply by doing something like:

smgr->addWhatEverNode(
stuff->getThis(..//stuff to get),
thing->doThis(..//Thingtodo);


or is this a RTFM on my part?
blayde
Posts: 45
Joined: Fri Jul 16, 2004 12:49 pm
Contact:

Post by blayde »

I dont think is an irr' question as much as it is a c++ q.

I'm just an irr' newbie, but I know a bit about c++, and I dont see any reason why your smgr couldn't be passed as an argument to any method/function you wanted to use it in. It seems that would do what you want.
Athlon_Jedi
Posts: 156
Joined: Wed Jul 21, 2004 4:29 am
Location: Mishawaka, In

yep ...brain left me again on that one.

Post by Athlon_Jedi »

i think i suffered a minor malfunction of the cerebral cortex on that one.....

or perhaps it was just a dumb attack!

anyhow i knew that but thanks,,, just goes to show kids,,,,,,dont code when ur high lol.... :oops: :oops: :lol: :lol: :lol:
ManakelNEVERLOGS

Post by ManakelNEVERLOGS »

In this case you should pass the device to all your classes instead of the scene manager since all irrlicht can be obtained from device and device is a singleton.

I think it would be more powerfull and clean. (my two cents:-))
Thulsa Doom
Posts: 63
Joined: Thu Aug 05, 2004 9:40 am
Location: Germany

Post by Thulsa Doom »

Hi,

I use this base class:

Code: Select all

class Constants
{
public:
	Constants(irr::IrrlichtDevice* Device) : device(Device)
	{
		drvr =	device->getVideoDriver();
		smgr =	device->getSceneManager();
	};
	irr::IrrlichtDevice*			device;
	irr::video::IVideoDriver*		drvr;
	irr::scene::ISceneManager*		smgr;
};
Has someone tried another approach?
saigumi
Posts: 921
Joined: Fri Aug 22, 2003 11:31 am
Location: St. Louis, MO USA
Contact:

Post by saigumi »

That's pretty much what I do.

I have a class called CHub that holds references to all the parts of my game, the Irrlicht device + other parts, the Raknet device, the Audiere device, the inventory handler, the player handler, the game states, etc...

However, I use it as a single instance...

(excess code removed)

Hub.h:

Code: Select all

#pragma once

class CHub
{

private:
	static bool instanceFlag;
    static CHub *hub_mgr;

public:
	static CHub* getInstance();

	//!Global Variables
	irr::IrrlichtDevice* irr_device;
	irr::video::IVideoDriver* video_driver;

	//!Scene Management
	irr::gui::IGUIEnvironment* gui_env;
	irr::scene::ISceneManager* scene_mgr;
	StateManager* state_mgr;
};

Hub.cpp

Code: Select all

#include "Hub.h"

bool CHub::instanceFlag = false;
CHub* CHub::hub_mgr = NULL;

/*******************************************************************************
Constructor
*******************************************************************************/
CHub::CHub(void)
{
	//!Initialize Irrlicht
	device = irr::createDevice(irr::video::EDT_DIRECTX8, irr::core::dimension2d<irr::s32>(1024, 768), 32, false, false, this);
	video_driver = device->getVideoDriver();
	scene_mgr = device->getSceneManager();
	gui_env = device->getGUIEnvironment();
}

/*******************************************************************************
Get an Instance
*******************************************************************************/
CHub* CHub::getInstance()
{
    if(! instanceFlag)
    {
        hub_mgr = new CHub();
        instanceFlag = true;
        return hub_mgr;
    }
    else
    {
        return hub_mgr;
    }
}

/*******************************************************************************
Destructor
*******************************************************************************/
CHub::~CHub(void)
{
	instanceFlag = false;
	device->drop();

}
Then, all I need in any other class to have access to the hub is to:

CHub* hub = CHub::getInstance();

The class will instantiate once and every class in your application can share it.
Last edited by saigumi on Thu Aug 05, 2004 5:16 pm, edited 1 time in total.
Crud, how do I do this again?
Thulsa Doom
Posts: 63
Joined: Thu Aug 05, 2004 9:40 am
Location: Germany

Post by Thulsa Doom »

ThanX saigumi, :D

using static variables is exactly I was looking for!
Tyn
Posts: 932
Joined: Thu Nov 20, 2003 7:53 pm
Location: England
Contact:

Post by Tyn »

Yeah, I'm doing that now. I also handle all objects that are created with new through here, which are then stored for deletion and await a command in the class destructor. Seems the easist way to manage the memory, dunno if anyone else has been doing it differently.
Post Reply