irrlicht world struct

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
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

irrlicht world struct

Post by c.h.r.i.s »

Hi! I have made a world struct:

Code: Select all

struct world
{
irr::IrrlichtDevice* device;
irr::gui::IGUIEnvironment* guienv;
ISceneManager* smgr;
IVideoDriver* driver;
irr::newton::IWorld* p_world;
irr::newton::IBody* fairy_body;
irr::scene::CCameraRPGSceneNode* mycam;
irr::scene::ITriangleSelector* selector;
irr::scene::ISceneNode* selectedSceneNode;
irr::scene::ISceneNode* lastSelectedSceneNode;
irr::scene::IAnimatedMeshSceneNode* fairy;
IGUIListBox* console;
irr::gui::IGUIButton* button_quit;
irr::gui::IGUIButton* button_tutorial;
irr::gui::IGUIButton* button_takephoto;
irr::gui::IGUIButton* button_editor;
irr::gui::IGUIButton* button_normalmode;
irr::gui::IGUIButton* button_newobject;
irr::gui::IGUIButton* button_deleteobject;
irr::gui::IGUIButton* button_texture;
irr::scene::IAnimatedMeshSceneNode* mymodel;
int mouse_over_gui;
int file_dialog_open;
int object_count;
int editor_active;
int file_open_type;
int object_selected;
ISoundEngine* mysoundengine;
VxCompass* pgCompass;
};
Than i have made functions that will make, draw and destroy the world:

Code: Select all

void init_world(world myworld)
{ 
myworld.device = createDevice(EDT_DIRECT3D9, dimension2d<s32>(400, 300), 32,	false, false, false, 0);
myworld.device->setWindowCaption(L"blabla");
//...and more stuf...(load, world, sound, physics...)
}

void run_world(world myworld)
{
	while(myworld.device->run())
	{	
	//update physics
	myworld.p_world->update();
	myworld.driver->beginScene(true, true, SColor (0,200,200,200));
	myworld.smgr->drawAll();
	myworld.guienv->drawAll();
	myworld.driver->endScene();
                }
}

void destroy_world(world myworld)
{
myworld.mysoundengine->stopAllSounds(); // delete sound engine
myworld.mysoundengine->drop(); // delete sound engine
myworld.device->drop();
}
Now i will define my world and call this functions:

Code: Select all

world theworld;
init_world(theworld);
run_world(theworld);
destroy_world(theworld);
Debugger stops in function run_world at line:
while(myworld.device->run())
and says:

Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'c:\bla.exe'.
Additional Information: The runtime has encountered a fatal error. The address of the error was at 0x79ef572b, on thread 0xcc4. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

Whats wrong?
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

and you don't get an option to debug your code ?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

This is going to be a long thread... The obvious problem is that all of your functions take your world by value. So inside init_world you get a local copy of myworld, you modify it, and return, leaving the original uninitialized. Here is a simple example of the exact problem, but simplified...

Code: Select all

struct world
{
  int thing;
};

void init_world(world myworld)
{
  myworld.thing = 10;
}

int main(void)
{
  world myworld;
  init_world(myworld);

  printf("world.thing=%d\n", myworld.thing);
  return 0;
}
You need your functions to take world references [world& myworld] or world pointers [world* myworld]. This way the insides of the functions have access to the original object and not a copy.

If you care to learn about this stuff, you would need to search on 'pass by value' and 'pass by reference'.

Travis
Eldritch
Posts: 33
Joined: Mon Feb 26, 2007 12:33 pm

Post by Eldritch »

I feel I need to ask a very noobish question: is it possible to have multiple devices and just switch between them like in this world struct example?

*hides in case anti-noob squad appears* :oops:
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I've personally never tried it until now, but no it doesn't seem to work.

Travis
c.h.r.i.s
Posts: 41
Joined: Mon Jan 22, 2007 8:38 pm

Post by c.h.r.i.s »

OK! Thanks a lot! This will work:

Code: Select all

void init_world(world *myworld)
{
//... 
}
void run_world(world *myworld)
{
//... 
}
void destroy_world(world *myworld)
{
//... 
}
world theworld;
init_world(&theworld);
run_world(&theworld);
destroy_world(&theworld);
But i have still one problem: I need to give a pointer of "myworld" to the event receiver which is called in init_world(). How can i do this?
Post Reply