proper way to init/deinit the engine

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
gtimworks
Posts: 21
Joined: Sun Nov 03, 2013 8:07 pm

proper way to init/deinit the engine

Post by gtimworks »

I am working on a game prototype. For each level I am trying to init Irrlicht engine by calling

Code: Select all

  _pDevice = createDeviceEx(param);
 
and by the end of the level I call

Code: Select all

  _pDevice->drop();
 
is this the proper way to init/deinit Irrlicht engine? I put the code in a loop side by side, and it looks like I am seeing memory leak. The total memory usage keeps growing. By the way I am using Irrlicht 1.9 prerelease, on iOS.

so, what is the correct way to initialize/teardown the engine? or, should I just have the engine initialized once? then when I move from one level to the other, how do I reset the engine context?
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: proper way to init/deinit the engine

Post by hendu »

Why would you do that? That destroys and recreates the window each time, along with other needless init/deinit things, causing you pointless level load delays (and flickering!).
should I just have the engine initialized once? then when I move from one level to the other, how do I reset the engine context?
Yes, do it once. You can clear out textures and meshes through the scene manager and the respective caches.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: proper way to init/deinit the engine

Post by CuteAlien »

Usually you create the device once and release it once per application. Although you shouldn't get a memory-leak in your case. Maybe it needs a call to _pDevice->closeDevice before the drop call.

More often it's sufficient to clear the scenemanager. If you also need to release textures and meshes you can also call IVideoDriver:: removeAllTextures and ISceneManager::getMeshCache()->clear(). For gui it's IGUIEnvironment::clear().
If you want to clear only textures for models, but not for example for the gui then you should remove textures one-by-one and have to remember yourself somewhere which ones to remove and which to keep.

If there's enough memory then the best way is to actually keep meshes and textures in memory all the time. For a game with many huge scenes that is impossible, but for many games it works.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
gtimworks
Posts: 21
Joined: Sun Nov 03, 2013 8:07 pm

Re: proper way to init/deinit the engine

Post by gtimworks »

Thanks guys.

Following the suggestion I keep the device open across different levels. So I try to clean up the resources without shutting down the device. One of the issue I am facing is "addHighLevelShaderMaterialFromFiles". I am using OGLES2 so internally it creates a COGLES2MaterialRenderer. However I don't know what is the correct way to trigger the destruction of that class.

So, is there a way to clean it up? or I should keep all the materials cross levels too? It is kind of messy to keep them in memory since the user data could be changed from level to level but I don't see a way to update the material callback class...
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: proper way to init/deinit the engine

Post by CuteAlien »

I think there is currently no way to remove materials. Generally you should be fine by adding materials just once (unless you write really a lot of shaders for each level). I don't know now if there was some technical reason why there is no cleanup for shaders in engine, I wondered about that myself in the past.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
gtimworks
Posts: 21
Joined: Sun Nov 03, 2013 8:07 pm

Re: proper way to init/deinit the engine

Post by gtimworks »

When function "addHighLevelShaderMaterial" is called, one of the parameter is "userData", which will be referenced inside the callback function. I don't see a problem to have those materials stay in memory but the userData will definitely change from time to time. Ideally there should be some API to allow changing the userData on the fly.

For now I will use an indirect pointer so that I could change the userData on my side.
Post Reply