SFML / imgui-sfml

Dear ImGui backend for use with SFML
MIT License
1.14k stars 172 forks source link

[Linux] Rendering a rendertexture to ImGui::Image #11

Closed kongo555 closed 8 years ago

kongo555 commented 8 years ago

Hello! I have simillar problem to #8 and #10 but window.pushGLStates() and window.popGLStates() doesn't help. I'm rendering my scene to texture and then try to display it in imgui. It start to bug on renderTexture.display() method. I call it before ImGui::Render()

    renderTexture.clear();
    map->render(renderTexture);
    simulation->render(renderTexture, delta);
    renderTexture.display();

    ImGui::SFML::Update(elapsed);
    window->clear();
    gui->test();
    gui->render(sprite);
    ImGui::Render();

In test() and render(sprite) i call just imgui functions. I'm using 2.4 sfml version.

Thanks in advance for help :)

EDIT: It turns out that i don't have to use renderTexture.display() and then everything is fine.

eliasdaler commented 8 years ago

Hmm, actually you have to use renderTexture.display()! Can you please find the minimal code which reproduces the problem? It will help me solve potentially undiscovered bugs.

Btw, I've tried doing the same thing as you, didn't have any problems.

kongo555 commented 8 years ago
int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "");
    window.setVerticalSyncEnabled(true);
    ImGui::SFML::Init(window);

    bool show = true;

    sf::RenderTexture renderTexture;
    sf::Sprite sprite;

    if (!renderTexture.create(32, 32))
    {
        std::cout << "error renderTexture\n";
        // error...
    }
    sprite =  sf::Sprite(renderTexture.getTexture());
    sf::RectangleShape tileTexture(sf::Vector2f(32, 32));
    tileTexture.setFillColor(sf::Color::White);
    tileTexture.setPosition(0, 0);
    renderTexture.draw(tileTexture);
    renderTexture.display();

    sf::Clock deltaClock;
    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            ImGui::SFML::ProcessEvent(event);

            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        ImGui::SFML::Update(deltaClock.restart());
        ShowText(show);
        ShowImage(show, sprite);

        window.clear();
        ImGui::Render();

        window.display();
    }

    ImGui::SFML::Shutdown();
}

static void ShowText(bool& p_open)
{
    ImGui::SetNextWindowPos(ImVec2(10,10));
    if (!ImGui::Begin("Example: Text", &p_open))
    {
        ImGui::End();
        return;
    }
    ImGui::Text("Simple overlay\non the top-left side of the screen.");
    ImGui::Separator();
    ImGui::Text("Mouse Position: (%.1f,%.1f)", ImGui::GetIO().MousePos.x, ImGui::GetIO().MousePos.y);
    ImGui::End();
}

static void ShowImage(bool& p_open, sf::Sprite sprite)
{
    ImGui::SetNextWindowPos(ImVec2(100,100));
    if (!ImGui::Begin("Example: Image", &p_open))
    {
        ImGui::End();
        return;
    }
    ImGui::Image(sprite);
    ImGui::End();
}

When i comment renderTexture.display(); everything works, without it i have buged window.

eliasdaler commented 8 years ago

Calling display is needed here. And indeed, calling it there causes some problems. It looks like this commit solves the issue. Can you try it out?

kongo555 commented 8 years ago

Everything is fine now :)

eliasdaler commented 8 years ago

Awesome! I've also managed to remove reset/push/popGLStates calls in the latest version. Please try it out, and see if it works fine for you as well. :)