ImGui 在单击按钮后冻结



我在UI应用程序中使用ImGui。我的问题是当我按下按钮时,"if 条件"中的代码将执行。但是一旦我按下按钮,我就无法按下另一个按钮,包括按下的按钮。谁能告诉我有什么问题?

示例代码:

while(window)
{
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
glfwPollEvents();
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
{
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)
{
for (int i = 0; i < 1000000; i++)
{
cout << i;
}

}

在您按下按钮的那一刻,相应的if语句中的所有内容都将被执行。这意味着在成功打印所有i之前,不会更新您的用户界面。完成此操作后,程序将返回到执行 GUI 例程,并且可以再次按下该按钮。

如果要在后台运行i的打印,可以考虑使用线程。通过这个,你可以继续使用你的GUI做其他不依赖于for循环执行的事情。很可能您还想在启动线程后禁用该按钮。在帧的每次更新中,您可以检查打印i的线程是否完成执行。如果是这样,请加入线程并再次启用该按钮。

即时模式 GUI 并不意味着所有工作都可以在 gui 线程中完成。

您可以在按下按钮时触发要执行的操作。 我建议只在 gui 渲染循环中进行 UI 更新和简单逻辑,所有处理都应移动到运行器类型的线程中。假设渲染循环周期的执行时间保持 1/渲染FPS 的四分之一

最新更新