Skip to content

using RenderTexture within ImGui::Begin() and ImGui::End()? #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Thanh-Binh opened this issue Mar 17, 2025 · 0 comments
Open

using RenderTexture within ImGui::Begin() and ImGui::End()? #317

Thanh-Binh opened this issue Mar 17, 2025 · 0 comments

Comments

@Thanh-Binh
Copy link

Dear all,
I tried to update my old application from SFML2.6 in SFML3.0.0 with the lastest ImGUI, but I can not display anything within ImGui::Begin() and ImGui::End()?
Belows is my code (slightly modified your example).
Could you please help me?

Thanks.

`
#include "imgui.h" // necessary for ImGui::*, imgui-SFML.h doesn't include imgui.h

#include "imgui-sfml/imgui-SFML.h" // for ImGui::SFML::* functions and SFML-specific overloads

#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow window(sf::VideoMode({1280, 720}), "ImGui + SFML = <3");
window.setFramerateLimit(60);
if (!ImGui::SFML::Init(window))
return -1;

sf::RenderWindow childWindow(sf::VideoMode({640, 480}), "ImGui-SFML Child window");
childWindow.setFramerateLimit(60);
if (!ImGui::SFML::Init(childWindow))
    return -1;

sf::RenderTexture rt;

sf::Clock deltaClock;
while (window.isOpen())
{
    // Main window event processing
    while (const auto event = window.pollEvent())
    {
        ImGui::SFML::ProcessEvent(window, *event);
        if (event->is<sf::Event::Closed>())
        {
            if (childWindow.isOpen())
            {
                childWindow.close();
            }
            window.close();
            ImGui::SFML::Shutdown(); // will shutdown all windows
            return 0;                // return here so that we don't call Update/Render
        }
    }

    // Child window event processing
    if (childWindow.isOpen())
    {
        while (const auto event = childWindow.pollEvent())
        {
            ImGui::SFML::ProcessEvent(childWindow, *event);
            if (event->is<sf::Event::Closed>())
            {
                childWindow.close();
                ImGui::SFML::Shutdown(childWindow);
            }
        }
    }

    // Update
    const sf::Time dt = deltaClock.restart();
    ImGui::SFML::Update(window, dt);
    if (childWindow.isOpen())
    {
        ImGui::SFML::Update(childWindow, dt);
    }

    // Add ImGui widgets in the first window
    ImGui::SFML::SetCurrentWindow(window);
    ImGui::Begin("Hello, world!");
    ImGui::Button("Look at this pretty button");
    ImGui::End();

    // Add ImGui widgets in the child window
    if (childWindow.isOpen())
    {
        ImGui::SFML::SetCurrentWindow(childWindow);
        ImGui::Begin("Works in a second window!");
        ImGui::Button("My Example button");

        sf::RectangleShape rs_horizontal;
        rs_horizontal.setPosition(sf::Vector2f(0.0f, 0));
        rs_horizontal.setSize(sf::Vector2f(100, 100));
        rs_horizontal.setFillColor(sf::Color::White);

        rt.draw(rs_horizontal);
        rt.display();
        ImGui::Image(rt);

        ImGui::End();
    }

    // Main window drawing
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    window.clear();
    window.draw(shape);
    ImGui::SFML::Render(window);
    window.display();

    // Child window drawing
    if (childWindow.isOpen())
    {
        sf::CircleShape shape2(50.f);
        shape2.setFillColor(sf::Color::Red);

        childWindow.clear();
        childWindow.draw(shape2);
        ImGui::SFML::Render(childWindow);
        childWindow.display();
    }
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant