メインコンテンツへスキップ

imgui 使ってみた

    マルチプラットフォームのGUIフレームワークです
    テストアプリ用途によさげ

    さすがフランス人エスプリが効いてる

    1.どこにあるの?

    git clone https://github.com/ocornut/imgui.git -b v1.75

    /imgui/examples にサンプルプログラムあり

    2.どうやって使うの?

    VisualStudioの場合、imgui_examples.slnを開く

    画像1

    [本筋と関係ない話]
    .natvisってなんじゃらほいと思ってググったら、Debugger Visualizerというデバッガでいい感じに表示してもらうためのVSの機能らしい

    3.自分のアプリに組み込みたい

    [Win32 / DirectX11 の場合]

    1.空の[Win32 プロジェクト]作成

    2.プロジェクトに以下のcppを追加

    [imgui 本体]
    imgui.cpp
    imgui_draw.cpp
    imgui_widget.cpp
    
    [imgui プラットフォームごとの固有実装]
    imgui_impl_dx11.cpp
    imgui_impl_win32.cpp

    3.追加のインクルードディレクトリにクローンしたimguiを追加

    4.追加のライブラリディレクトリに$(DXSDK_DIR)Lib/x86を追加
    ※Win10ならDirectX SDKいらない希ガス…
    (追記:WindowsSDKが入っていれば$(DXSDK_DIR)なくても大丈夫です)
    ※プラットフォームx64の場合は/x64に変えてね

    5.追加のライブラリファイルにd3d11.libを追加

    6.example_win32_directx11/main.cppを持ってきて、一部改変(というか不要なのを削除)

       // Main loop
       MSG msg;
       ZeroMemory(&msg, sizeof(msg));
       while (msg.message != WM_QUIT)
       {
           // Poll and handle messages (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.
           if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
           {
               ::TranslateMessage(&msg);
               ::DispatchMessage(&msg);
               continue;
           }
    
           // Start the Dear ImGui frame
           ImGui_ImplDX11_NewFrame();
           ImGui_ImplWin32_NewFrame();
           ImGui::NewFrame();
    
           /* ここに追加したいGUIを書く */
           ImGui::Text("Hello, world");
    
           // Rendering
           ImGui::Render();
           g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
           g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, (float*)&clear_color);
           ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
    
           g_pSwapChain->Present(1, 0); // Present with vsync
           //g_pSwapChain->Present(0, 0); // Present without vsync
       }
    

    画像2

    [Win32 / DirectX12 の場合]

    /imgui/examples/example_win32_dx12/ を参照してください
    拙Win10環境で動作することを確認しました

    その他

    どんなGUIパーツがあるかは以下がよくまとまっています

    また、高いポータビリティから、以下のRust向けの本でも使われているみたいです

    謎の企業でも使われているっぽい!

    ちなみにMITライセンスです いいね!

    あなたへのおすすめ