How do you remove GUI elements from memory

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
lokiare
Posts: 40
Joined: Wed Feb 04, 2009 5:40 pm

How do you remove GUI elements from memory

Post by lokiare »

I've searched and looked around the forums and looked through the documentation. I haven't found a good way of removing loaded resources once I know I'm done with them. what I'm specifically trying to remove are things like unused GUI elements, and Textures. I don't want them loaded into memory as this affects the overall speed of the game.

How do you remove GUI elements and Textures?

I've tried ->drop(), but this doesn't seem to do it.
I've also tried guienv->clear(). This also doesn't work.

Here are pieces of my code to see if you can find problems:

Code: Select all

// Get rid of the gui elements
	guienv->clear();
	guienv->getRootGUIElement()->getElementFromId(GUI_ID_BUTTON_QUIT, true)->drop();
	guienv->getRootGUIElement()->getElementFromId(GUI_ID_BUTTON_NEW, true)->drop();
	guienv->getRootGUIElement()->getElementFromId(GUI_ID_BUTTON_LOAD, true)->drop();
	guienv->getRootGUIElement()->getElementFromId(GUI_ID_BUTTON_SETUP, true)->drop();
	guienv->getRootGUIElement()->getElementFromId(GUI_ID_BUTTON_DIFFICULTY_NORMAL, true)->drop();
	guienv->getRootGUIElement()->getElementFromId(GUI_ID_BUTTON_DIFFICULTY_HARD, true)->drop();
	guienv->clear();

Code: Select all

	// Main menu buttons
	IGUIButton * buttonNew = guienv->addButton(rect<s32>(10, 200, 210, 200 + 64),0, GUI_ID_BUTTON_NEW, L"New Game", L"Starts a new Game");
	IGUIButton * buttonLoad = guienv->addButton(rect<s32>(10, 300, 210, 300 + 64),0, GUI_ID_BUTTON_LOAD, L"Load Game", L"Loads a previously saved game");
	IGUIButton * buttonSetup = guienv->addButton(rect<s32>(10, 400, 210, 400 + 64),0, GUI_ID_BUTTON_SETUP, L"Setup", L"Sets up options for the game");
	IGUIButton * buttonQuit = guienv->addButton(rect<s32>(10, 500, 210, 500 + 64),0, GUI_ID_BUTTON_QUIT, L"Quit", L"Exits the game");

	// New Game Difficulty buttons
	IGUIButton * buttonDifficultyNormal = guienv->addButton(rect<s32>(10, 200, 210, 200 + 64),0, GUI_ID_BUTTON_DIFFICULTY_NORMAL, L"Normal", L"Sets the games difficulty to normal");
	IGUIButton * buttonDifficultyHard =	guienv->addButton(rect<s32>(10, 300, 210, 300 + 64),0, GUI_ID_BUTTON_DIFFICULTY_HARD, L"Hard", L"Sets the games difficulty to hard");

	// Turn all of them invisible until we are ready to render
	buttonNew->setVisible(false);
	buttonLoad->setVisible(false);
	buttonSetup->setVisible(false);
	buttonQuit->setVisible(false);
	buttonDifficultyNormal->setVisible(false);
	buttonDifficultyHard->setVisible(false);

	// Load up an image for the button
	buttonBackground = NULL;
	buttonBackground = driver->getTexture("Assets/ButtonBackground.png");
	if (!buttonBackground)
	{
		device->getGUIEnvironment()->addMessageBox(L"Error", L"Cannot Load \'Assets/buttonBackground.png\'");
	}

	// Change some properties of the buttons
	buttonNew->setImage(buttonBackground);
	buttonLoad->setImage(buttonBackground);
	buttonSetup->setImage(buttonBackground);
	buttonQuit->setImage(buttonBackground);
	buttonDifficultyNormal->setImage(buttonBackground);
	buttonDifficultyHard->setImage(buttonBackground);
Those are the creation and deletion elements of the code. I'm switching between states, and no matter what I do the buttons are stuck on the screen. I know I can turn their visibility off, but I want them gone, not invisible.
U238
Posts: 14
Joined: Mon Aug 17, 2009 1:01 pm
Location: Taganrog, Russia

Post by U238 »

Maybe

Code: Select all

IGUIElement::remove() 
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

If you want to get rid of the texture completely, you have to ask the texture cache for removal of it. But first make sure your GUI element or scene node is remove()d as shown above.
lokiare
Posts: 40
Joined: Wed Feb 04, 2009 5:40 pm

Post by lokiare »

IGUIElement::remove() doesn't work. The documentation says it just removes it from the parent which might mean it just unattaches it, but doesn't get rid of it.
lokiare
Posts: 40
Joined: Wed Feb 04, 2009 5:40 pm

Post by lokiare »

I've found a bug, my destructor is never called. For some reason my array retains my gamestate even though I tell it to ::erase() it.
lokiare
Posts: 40
Joined: Wed Feb 04, 2009 5:40 pm

Post by lokiare »

Ok, after fixing the bug that somehow skips the destructor, I put in ::remove() and it works fine now. Thank you.
Post Reply