Page 1 of 1

How do you remove GUI elements from memory

Posted: Thu Aug 27, 2009 8:42 am
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.

Posted: Thu Aug 27, 2009 9:18 am
by U238
Maybe

Code: Select all

IGUIElement::remove() 

Posted: Thu Aug 27, 2009 9:27 am
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.

Posted: Thu Aug 27, 2009 6:36 pm
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.

Posted: Thu Aug 27, 2009 6:56 pm
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.

Posted: Thu Aug 27, 2009 7:18 pm
by lokiare
Ok, after fixing the bug that somehow skips the destructor, I put in ::remove() and it works fine now. Thank you.