*

Soul

  • *
  • 11
    • View Profile
drawing sprites
« on: 03 May 2014, 23:33:01 »
Hey how can i draw sf::sprite on childwindow?

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: drawing sprites
« Reply #1 on: 04 May 2014, 10:40:34 »
If you have an sf::Texture that you want to display as a background of the child window, then you can use the setBackgroundTexture function.

Otherwise you have to create a Canvas widget inside the child window. The Canvas class is a wrapper around sf::RenderTexture, so you use it in the same way.
Code: [Select]
tgui::Canvas::Ptr canvas(*child);
canvas->setSize(child->getSize().x, child->getSize().y);

// This code is executed every time the sprite/texture changes.
// Either once when the sprite is a static image, or every frame when constantly changing the image.
canvas->clear();
canvas->draw(sprite);
canvas->display();

*

Soul

  • *
  • 11
    • View Profile
Re: drawing sprites
« Reply #2 on: 04 May 2014, 10:56:33 »
thanks for answer, anyway i think second renderwindow will be better to that what i want code :P, by the way
how can i use slider/scrollbar with sprite image? i mean, when i scroll slider/scrollbar the sprite is move up or down?

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: drawing sprites
« Reply #3 on: 04 May 2014, 11:16:41 »
It doesn't requires much code, but its not very easy.
Code: [Select]
void moveSprite(tgui::Slider::Ptr slider, sf::Sprite& sprite, int minY, int maxY)
{
    // Puts the sprite on position minY when the slider has value 0,
    // on position maxY when the slider has its maximum value,
    // and somewhere inbetween otherwise.
    // Note that this formula will only work when the minimum of slider is 0, otherwise it has to be changed.
    sprite.setPosition(sprite.getPosition().x, minY + (slider->getValue() / (float)slider->getMaximum()) * (maxY - minY));
}

// Let the slider call the moveSprite function when its value changes.
// The sprite will be placed between Y position 20 and 100, the X position will remain unchanged here.
slider->bindCallback(std::bind(moveSprite, slider, std::ref(sprite), 20, 100), tgui::Slider::ValueChanged);

This will only work when moveSprite is a free function. If it is part of a class, you need some different parameters.
Code: [Select]
// pointerToMyClassInstance will be 'this' when this line is inside a function of MyClass.
slider->bindCallback(std::bind(&MyClass::moveSprite, pointerToMyClassInstance, slider, std::ref(sprite), 20, 100), tgui::Slider::ValueChanged);

You might want to read the tutorial about callbacks.
« Last Edit: 04 May 2014, 11:21:16 by texus »

*

Soul

  • *
  • 11
    • View Profile
Re: drawing sprites
« Reply #4 on: 04 May 2014, 11:58:09 »
hmmm i don't understand this source code : O
Code: [Select]
void Menu::LoadMenu(sf::RenderWindow &wnd)
{
    W=&wnd;
    gui.setWindow(wnd);
    gui.setGlobalFont("Font/djvs.ttf");
    gui.add(menu);
    tilegui.setWindow(T);
    tilegui.setGlobalFont("Font/djvs.ttf");
    tilegui.add(slider);
    menu->load(THEME_CONFIG_FILE);
    menu->setSize(wnd.getSize().x, 20);
    menu->addMenu("File");
    menu->addMenu("Layers");
    menu->addMenu("Tiles");
    menu->addMenu("Objects");
    menu->addMenuItem("File", "Create");
    menu->addMenuItem("File", "Load");
    menu->addMenuItem("File", "Save");
    menu->addMenuItem("File", "Exit");
    menu->bindCallback(tgui::MenuBar::MenuItemClicked);
    gui.add(window);
    window->bindCallback(tgui::ChildWindow::Closed);
    window->setCallbackId(0);
    Create->setCallbackId(1);
    Load->setCallbackId(2);
    menu->setCallbackId(3);
    slider->load(THEME_CONFIG_FILE);
    slider->setVerticalScroll(false);
    slider->setPosition(400, 0);
    slider->setSize(20, 300);
    slider->setMaximum(5);
    slider->setValue(0);
    slider->bindCallback(std::bind(this->moveTile, this, slider, std::ref(tilespr), 20, 100), tgui::Slider::ValueChanged);
}

void Menu::moveTile(tgui::Slider::Ptr slider, sf::Sprite& sprite, int minY, int maxY)
{
    if(slider->getValue()==minY)
    {
        sprite.setPosition(0,minY);
    }
    if(slider->getValue()==maxY)
    {
        sprite.setPosition(0,slider->getValue());
    }
    sprite.setPosition(sprite.getPosition().x, minY + (slider->getValue() / (float)slider->getMaximum()) * (maxY - minY));
}

i have this error
C:\Users\EasyNote\Desktop\cpp\mapeditor\src\Menu.cpp|34|error: no matching function for call to 'bind(<unresolved overloaded function type>, Menu* const, tgui::Slider::Ptr&, std::reference_wrapper<sf::Sprite>, int, int)'|

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: drawing sprites
« Reply #5 on: 04 May 2014, 12:08:36 »
Code: [Select]
this->moveTileThat won't work, it has to be "&Menu::moveTile".

You might need to know a little about c++11 and more specifically about std::function, std::bind and std::ref to fully understand this code. Without c++11 (or boost) it wouldn't be even possible to write something like this. The callback system is the only reason why tgui no longer supports older compilers.

*

Soul

  • *
  • 11
    • View Profile
Re: drawing sprites
« Reply #6 on: 04 May 2014, 16:42:38 »
i don't know what i do wrong, but it don't work, anyway never mind, i make other solution