Canvas overwrites child window background image
« on: 16 June 2015, 09:41:02 »
So I create a child window with a background image. I then want to display an image on the child window which can divide up the child window in different sections. Then exact position will change depending on the data - so I decided to use a canvas.

First I create the child window with these 2 functions. initPos and initSize are just structs that have x and y values. There is a childwindow vector of all childwindows within the class.

void drawEngine::initSubMenuChildWin()
        initPos.setPos(gameWindow.getSize().x * 0.5, gameWindow.getSize().y * 0);
        initSize.setPos((gameWindow.getSize().x - (gameWindow.getSize().x * 0.5)), getLowerScreenPos().getPosY());
        setupChildWindow("Finance Menu", sf::Color::Red, financeChildWin, &subMenuBackGround);

void drawEngine::setupChildWindow(string title_text, sf::Color titleText_Color, string passed_childName, sf::Texture *passed_texture)
        int listPreSize = childWindowList.size();

        if (!IsChildWinAlreadyInChildList(passed_childName))
                childWindowList.push_back(tgui::ChildWindow::Ptr(gui, passed_childName));

        for (int i = listPreSize; i < childWindowList.size(); i++)
                childWindowList[i]->setPosition(initPos.getPosX(), initPos.getPosY());
                childWindowList[i]->setSize((initSize.getPosX()), (initSize.getPosY()));
                if (passed_texture != NULL)

tgui::ChildWindow::Ptr drawEngine::getChildWindow(string passed_widgetName)
        string retrievedWidgetName;
        for (int i = 0; i < childWindowList.size(); i++)
                if (gui.getWidgetName(childWindowList[i], retrievedWidgetName))
                        if (retrievedWidgetName == passed_widgetName)
                                return childWindowList[i];
    return NULL;


Then in my code I use this to put the canvas in the childwindow and draw the sprite within the canvas.

        float childSizeX = getChildWindow(financeChildWin)->getSize().x;
        float childSizeY = getChildWindow(financeChildWin)->getSize().y;

        sf::Sprite sprite(largeTitleBar);
        tgui::Canvas::Ptr canvas(*getChildWindow(financeChildWin));
        canvas->setSize(childSizeX, childSizeY);

        sprite.setPosition(40, getChildWindow(financeChildWin)->getSize().y * 0.5);


When I call this the largeTitleBar sprite displays fine, but the background image of the childwindow is gone - i.e. all black.
If I change the above so the canvas size and position are the same as the sprite, the largeTitleBar sprite does not get displayed and instead the black canvas gets displayed.

So I guess I'm struggling to understand how to use canvas properly. Any ideas/suggestions?



  • *****
  • 1343
    • View Profile
    • Texus's Blog
Re: Canvas overwrites child window background image
« Reply #1 on: 16 June 2015, 10:44:57 »
sprite.setPosition(40, getChildWindow(financeChildWin)->getSize().y * 0.5);
It took me some time to understand what was going wrong, but this is where the mistake lies.

The position is relative to the canvas. So if the canvas covers the entire child window it will clear the entire window with its black color (which is the default of the clear function) and the sprite will be inside it. When you make the canvas smaller the sprite is drawn outside the canvas and thus not visible.

Re: Canvas overwrites child window background image
« Reply #2 on: 17 June 2015, 05:52:38 »
arrggghhhhhh...... ok thanks. Obvious when you point it out. :)

Re: Canvas overwrites child window background image
« Reply #3 on: 17 June 2015, 08:55:46 »
Oh and thank you yet again. :)