ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
59.4k stars 10.12k forks source link

How to display the same texture with different scale modes? #7616

Open achabense opened 3 months ago

achabense commented 3 months ago

Version/Branch of Dear ImGui:

Version 1.90.6

Back-ends:

imgui_impl_sdl2.cpp + imgui_impl_sdlrenderer2.cpp

Compiler, OS:

Windows 10 + MSVC 2022

Full config/build information:

No response

Details:

I want to display the same texture in different scale modes. The screenshot shows the effect of test_fn below. Can I achieve the same effect without creating multiple textures?

Screenshots/Video:

image

Minimal, Complete and Verifiable Example code:

static void test_fn(SDL_Renderer* renderer) {
    static SDL_Texture *nearest = nullptr, *linear = nullptr;
    const int w = 33, h = 25;
    const int zoom = 4;

    if (!nearest) {
        nearest = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, w, h);
        SDL_SetTextureScaleMode(nearest, SDL_ScaleModeNearest);
        linear = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, w, h);
        SDL_SetTextureScaleMode(linear, SDL_ScaleModeLinear);
        Uint32 pixels[h][w];
        bool b = false;
        for (auto& line : pixels) {
            for (Uint32& p : line) {
                p = b ? 0 : -1;
                b = !b;
            }
        }
        SDL_UpdateTexture(nearest, nullptr, pixels, w * sizeof(Uint32));
        SDL_UpdateTexture(linear, nullptr, pixels, w * sizeof(Uint32));
    }

    if (ImGui::Begin("Test", nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoCollapse)) {
        ImGui::Image(nearest, ImVec2(w * zoom, h * zoom));
        ImGui::SameLine();
        ImGui::Image(linear, ImVec2(w * zoom, h * zoom));
    }
    ImGui::End();
}
achabense commented 3 months ago

Here is the background of this issue: I wanted to display a zoom window in nearest scale mode for a texture shown in linear mode. The problem was worked around by making a new texture with different scale mode, but I think the solution is wasteful.