c++sdlimgui

How to make imgui menu bar stick to the top of SDL window?


I have a small game written in SDL, I'm trying to add top bar by Imgui widgets to the top of my application.

void Application::render()
{
#ifndef NDEBUG
    ImGui_ImplSDLRenderer3_NewFrame();
    ImGui_ImplSDL3_NewFrame();
    ImGui::NewFrame();
    ImGui::Begin("My Menu Bar", NULL, ImGuiWindowFlags_MenuBar );
    if (ImGui::BeginMenuBar())
    {
        if (ImGui::BeginMenu("File"))
        {
            if (ImGui::MenuItem("Open..", "Ctrl+O")) {  }
            if (ImGui::MenuItem("Save", "Ctrl+S"))   {  }
            if (ImGui::MenuItem("Close", "Ctrl+W"))  {  }
            ImGui::EndMenu();
        }
        ImGui::EndMenuBar();
    }
    ImGui::End();
    ImGui::Render();
#endif

    SDL_RenderClear(m_renderer->getRenderer());
    SDL_SetRenderTarget(m_renderer->getRenderer(), nullptr);

    m_stateMachine->render();

#ifndef NDEBUG
    ImGui_ImplSDLRenderer3_RenderDrawData( ImGui::GetDrawData() );
#endif

    SDL_RenderPresent(m_renderer->getRenderer());
}

It works, but Imgui creates another window and draw it's widgets to it. How to make it show at the top of existing window?


Solution

  • As HolyBlackCat mentioned, ImGui::BeginMainMenuBar() + ImGui::EndMainMenuBar() works for this case