gui remove help

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

gui remove help

Post by Seven »

I have been working on a gui windowing system and have run into a few glitches. Hopefully someone can point me in the right direction.

in this code, i make a list of all of the texture files available in a directory and create a guiimage for each. the images are placed into an array called ToolbarControls and then displayed as needed.
all of this works fine. the issue is when i change directories, i need to remove all of the toobarcontrols and rebuild based on the new directory. when I do this, i get leftover images that dont get removed.

Code: Select all

 
        void setDirectory(stringc dirname)
        {
            destroyControls();
            setSelectedItem(-1);
 
            const irr::io::path OriginalWorkingDirectory = getDesktop()->getEngine()->getDevice()->getFileSystem()->getWorkingDirectory();
            getDesktop()->getEngine()->getDevice()->getFileSystem()->changeWorkingDirectoryTo(dirname);
            irr::io::IFileList* Files = getDesktop()->getEngine()->getDevice()->getFileSystem()->createFileList();
            getDesktop()->getEngine()->getDevice()->getFileSystem()->changeWorkingDirectoryTo(OriginalWorkingDirectory);
 
            for (unsigned int i = 0; i < Files->getFileCount(); i++)
            {
                if (!Files->isDirectory(i))
                {
                    const irr::io::path filename = Files->getFullFileName(i);
                    ITexture* texture = Environment->getVideoDriver()->getTexture(filename.c_str());
                    if (texture)
                    {
                        IGUIImage* image = (IGUIImage*)addControl(Environment->addImage(rect<s32>(0, 0, 130, 30), this, -1, L"", false));
                        image->setImage(texture);
                        image->setScaleImage(true);
                    }
                }
            }
            Files->drop();
        }
    };
 
        virtual IGUIElement* addControl(IGUIElement* e)
        {
            m_ToolbarControls.push_back(e);
            calculateLayout();
            return e;
        }
 
        void destroyControls()
        {
            for (u32 x = 0; x < m_ToolbarControls.size(); x++)
            {
                m_ToolbarControls[x]->remove();
                m_ToolbarControls.erase(x);
            }
            m_ToolbarControls.clear();
            calculateLayout();
        }
 

any thoughts on why this code

Code: Select all

 
        void destroyControls()
        {
            for (u32 x = 0; x < m_ToolbarControls.size(); x++)
            {
                m_ToolbarControls[x]->remove();
                m_ToolbarControls.erase(x);
            }
            m_ToolbarControls.clear();
            calculateLayout();
        }
 
does not actually destroy the images?
CuteAlien
Admin
Posts: 10021
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: gui remove help

Post by CuteAlien »

The m_ToolbarControls.erase(x) will modify the array. So after you delete x=0 the second element is now index 0, but you go on and delete x=1 next. So basically you miss every second element. Instead only call remove for the elements and clear the arrray after the loop with clear().
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
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: gui remove help

Post by Seven »

I should have known that. thanks for the assist!
Post Reply