You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SDLRenderer2后端的DearImGUI文本显示为方块问题求助

解决SDLRenderer2后端DearImGUI字体显示方块问题

我编写了一个基于SDLRenderer2后端的DearImGUI程序,运行后文本显示为方块而非正常字体样式,但字体本身不存在问题,目前尚未明确故障原因。

程序代码

#include <SDL_main.h>
#include <SDL.h>
#include <iostream>
#include <imgui.h>
#include <imgui_impl_sdl2.h>
#include <imgui_impl_sdlrenderer2.h>

int main(int, char**)
{
    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
    {
        std::cerr << "Error: " << SDL_GetError() << std::endl;
        return -1;
    }

    // Create SDL window
    SDL_Window* window = SDL_CreateWindow("ImGui SDL2 Software Renderer example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_RESIZABLE);
    if (!window)
    {
        std::cerr << "Error: " << SDL_GetError() << std::endl;
        return -1;
    }

    // Create SDL renderer
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
    if (!renderer)
    {
        std::cerr << "Error: " << SDL_GetError() << std::endl;
        return -1;
    }

    // Initialize ImGui
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    ImGui::StyleColorsDark();

    // Initialize ImGui backends
    ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
    ImGui_ImplSDLRenderer2_Init(renderer);

    // Build font atlas
    io.Fonts->AddFontDefault();
    unsigned char* tex_pixels = NULL;
    int tex_width, tex_height;
    io.Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_width, &tex_height);

    // Main loop
    bool done = false;
    while (!done)
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
                done = true;
            if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
                done = true;
            ImGui_ImplSDL2_ProcessEvent(&event);
        }

        // Start ImGui frame
        ImGui_ImplSDL2_NewFrame();
        ImGui::NewFrame();

        // Show ImGui demo window
        ImGui::ShowDemoWindow();

        // Render
        ImGui::Render();
        SDL_SetRenderDrawColor(renderer, 114, 144, 154, 255);
        SDL_RenderClear(renderer);
        ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData());
        SDL_RenderPresent(renderer);
    }

    // Cleanup
    ImGui_ImplSDLRenderer2_Shutdown();
    ImGui_ImplSDL2_Shutdown();
    ImGui::DestroyContext();

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

运行效果

程序运行后,界面中所有文本本该显示的位置都呈现为白色方块,无法正常渲染字体。

Code::Blocks配置文件

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
    <FileVersion major="1" minor="6" />
    <Project>
        <Option title="NiaAppDevTemplate" />
        <Option compiler="gcc" />
        <Option use_console_runner="0" />
        <Build>
            <Target title="Debug">
                <Option output="bin/Debug/NiaAppDevTemplate.exe" prefix_auto="1" extension_auto="1" />
                <Option object_output="obj/Debug/" />
                <Option type="1" />
                <Option compiler="gcc" />
                <Compiler>
                    <Add option="-g" />
                </Compiler>
                <Linker>
                    <Add option="-lmingw32" />
                    <Add option="-lSDL2main" />
                    <Add option="-lSDL2" />
                    <Add option="-mwindows" />
                </Linker>
            </Target>
            <Target title="Release">
                <Option output="bin/Release/NiaAppDevTemplate.exe" prefix_auto="1" extension_auto="1" />
                <Option object_output="obj/Release/" />
                <Option type="1" />
                <Option compiler="gcc" />
                <Compiler>
                    <Add option="-O2" />
                </Compiler>
                <Linker>
                    <Add option="-s" />
                    <Add option="-lmingw32" />
                    <Add option="-lSDL2main" />
                    <Add option="-lSDL2" />
                    <Add option="-mwindows" />
                </Linker>
            </Target>
        </Build>
        <Compiler>
            <Add option="-Wall" />
            <Add option="-fexceptions" />
            <Add directory="Libraries/SDL2-2.30.3/x86_64-w64-mingw32/include/SDL2" />
            <Add directory="Libraries/imgui-1.90.5/backends" />
            <Add directory="Libraries/imgui-1.90.5" />
            <Add directory="C:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/um" />
            <Add directory="C:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/shared" />
            <Add option="-Dmain=SDL_main" />
        </Compiler>
        <Linker>
            <Add option="-LLibraries/SDL2-2.30.3/x86_64-w64-mingw32/lib" />
            <Add library="SDL2" />
            <Add library="setupapi" />
            <Add library="ole32" />
            <Add library="imm32" />
            <Add library="user32" />
            <Add library="gdi32" />
            <Add library="winmm" />
            <Add library="dsound" />
            <Add library="kernel32" />
            <Add library="shell32" />
            <Add library="advapi32" />
            <Add library="version" />
        </Linker>
        <Unit filename="Libraries/imgui-1.90.5/imgui.cpp" />
        <Unit filename="Libraries/imgui-1.90.5/imgui_demo.cpp" />
        <Unit filename="Libraries/imgui-1.90.5/imgui_draw.cpp" />
        <Unit filename="Libraries/imgui-1.90.5/backends/imgui_impl_sdlrenderer2.cpp" />
        <Unit filename="Libraries/imgui-1.90.5/backends/imgui_impl_sdl2.cpp" />
        <Unit filename="Libraries/imgui-1.90.5/imgui_tables.cpp" />
        <Unit filename="Libraries/imgui-1.90.5/imgui_widgets.cpp" />
        <Unit filename="main.cpp" />
        <Extensions>
            <lib_finder disable_auto="1" />
        </Extensions>
    </Project>
</CodeBlocks_project_file>

问题原因及修复方案

问题根源

代码中仅生成了字体 atlas 的像素数据,但未将其创建为SDL纹理,也未告知ImGui该纹理的ID。SDLRenderer2后端无法找到用于渲染字体的纹理,因此只能显示方块占位符。

修复步骤

  1. 在获取字体像素数据后,添加创建SDL纹理并关联到ImGui的代码:
    在io.Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_width, &tex_height);之后插入:
// 创建SDL纹理存储字体数据
SDL_Texture* font_texture = SDL_CreateTexture(
    renderer,
    SDL_PIXELFORMAT_RGBA32,
    SDL_TEXTUREACCESS_STATIC,
    tex_width,
    tex_height
);
if (!font_texture) {
    std::cerr << "Error creating font texture: " << SDL_GetError() << std::endl;
    return -1;
}

// 将字体像素数据复制到纹理
SDL_UpdateTexture(font_texture, NULL, tex_pixels, tex_width * 4);

// 设置纹理混合和过滤模式,确保字体渲染正常
SDL_SetTextureBlendMode(font_texture, SDL_BLENDMODE_BLEND);
SDL_SetTextureScaleMode(font_texture, SDL_ScaleModeLinear);

// 告诉ImGui使用这个纹理来渲染字体
io.Fonts->SetTexID((ImTextureID)font_texture);
  1. 在程序退出的清理阶段,销毁字体纹理:
    在ImGui_ImplSDLRenderer2_Shutdown();之前插入:
// 销毁字体纹理
SDL_DestroyTexture((SDL_Texture*)io.Fonts->GetTexID());

完成以上修改后,重新编译运行程序,字体即可正常显示。

内容的提问来源于stack exchange,提问作者user179283

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 14:42:03