Canvas overwrites child window background image

Started by starkhorn, 16 June 2015, 09:41:02

starkhorn

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.

Code (cpp) Select

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]->load(themeConfFile);
childWindowList[i]->setPosition(initPos.getPosX(), initPos.getPosY());
childWindowList[i]->setSize((initSize.getPosX()), (initSize.getPosY()));
childWindowList[i]->setTitleColor(titleText_Color);
childWindowList[i]->setTitle(title_text);
if (passed_texture != NULL)
{
childWindowList[i]->setBackgroundTexture(passed_texture);
}
}
}

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.

Code (cpp) Select


initSubMenuChildWin();
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);
canvas->clear();

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



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?

texus

Quote
Code (cpp) Select
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.

starkhorn

arrggghhhhhh...... ok thanks. Obvious when you point it out. :)

starkhorn