LoginSignup
2
4

More than 5 years have passed since last update.

imgui で UTF-8 なテキスト(漢字等)を表示する

Posted at

https://github.com/ocornut/imgui で UFT-8なテキストを表示するための方法のメモです

環境

Windows 10 pro
DxLib 3.1.6f
visual studio professional 2015
Unicode 文字セットを使用する

初期化

フォントの設定を行います。


E_RESULT init_draw()
{
    g_pd3dDevice = (LPDIRECT3DDEVICE9)GetUseDirect3DDevice9();
    ImGui_ImplDX9_Init(mainWindowHandle, g_pd3dDevice);

        //フォントの設定を行う。IPAゴシック 14ポイント
    ImGuiIO& io = ImGui::GetIO();
    io.Fonts->AddFontFromFileTTF("resource\\ipag.ttf", 14.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());

    return E_RESULT_SUCCESS;
}

表示の実際

サンプルプログラムを見ると文字コード直接埋め込みでした。

imgui_demo.cpp
        // 384行目付近を抜粋
        ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)");
        ImGui::Text("Kanjis: \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e (nihongo)");



これでは面倒なので調べたところ、なんてことはありませんでした。
u8リテラル指定だけで表示できます。

E_RESULT    exec_draw_imgui()
{
    RenderVertex();
    getInputEvent();
    ImGui_ImplDX9_NewFrame();

        // 漢字を u8 リテラルで指定するところがポイントです
    {
        static float f = 0.0f;
        ImGui::Begin(u8"メインメニュー", &show_another_window);
        ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
        ImGui::ColorEdit3("clear color", (float*)&clear_col);
        ImGui::End();
    }

    // Rendering
    g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
    g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
    g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
    D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_col.x*255.0f), (int)(clear_col.y*255.0f), (int)(clear_col.z*255.0f), (int)(clear_col.w*255.0f));

    ImGui::Render();
    g_pd3dDevice->Present(NULL, NULL, NULL, NULL);

    RefreshDxLibDirect3DSetting();

    return E_RESULT_SUCCESS;
}
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4