imgui-backends icon indicating copy to clipboard operation
imgui-backends copied to clipboard

Window resize

Open Xaklse opened this issue 9 years ago • 16 comments

In case your SFML application isn't in fullscreen mode, if you resize the render window, the widgets become deformed and mouse input isn't handled correctly.

This is what I have in imgui-rendering-SFML.h (no pushGLStates/popGLStates and usage of glViewport, exactly as examples found in imgui):

namespace ImGui
{
    namespace ImImpl
    {
        static sf::RenderTarget* ImImpl_rtarget;
        static sf::Texture* ImImpl_fontTex;

        static void ImImpl_RenderDrawLists(ImDrawData* draw_data)
        {
            if (draw_data->CmdListsCount == 0)
                return;

            GLint last_texture;
            glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
            GLint last_viewport[4];
            glGetIntegerv(GL_VIEWPORT, last_viewport);
            glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glDisable(GL_CULL_FACE);
            glDisable(GL_DEPTH_TEST);
            glEnable(GL_SCISSOR_TEST);
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
            glEnable(GL_TEXTURE_2D);

            ImGuiIO& io = ImGui::GetIO();
            glViewport(0, 0, (GLsizei)io.DisplaySize.x, (GLsizei)io.DisplaySize.y);
            glMatrixMode(GL_PROJECTION);
            glPushMatrix();
            glLoadIdentity();
            glOrtho(0.0f, io.DisplaySize.x, io.DisplaySize.y, 0.0f, -1.0f, +1.0f);
            glMatrixMode(GL_MODELVIEW);
            glPushMatrix();
            glLoadIdentity();

            sf::RenderStates states;
            states.blendMode = sf::BlendMode(sf::BlendMode::SrcAlpha, sf::BlendMode::OneMinusSrcAlpha);

#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
            for (int n = 0; n < draw_data->CmdListsCount; n++)
            {
                const ImDrawList* cmd_list = draw_data->CmdLists[n];
                const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->VtxBuffer.front();
                const ImDrawIdx* idx_buffer = &cmd_list->IdxBuffer.front();
                glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
                glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
                glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));

                for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
                {
                    const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
                    if (pcmd->UserCallback)
                    {
                        pcmd->UserCallback(cmd_list, pcmd);
                    }
                    else
                    {
                        sf::Vector2u win_size = ImImpl_rtarget->getSize();
                        sf::Texture::bind(ImImpl::ImImpl_fontTex);
                        glScissor((int)pcmd->ClipRect.x, (int)(win_size.y - pcmd->ClipRect.w), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
                        glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, GL_UNSIGNED_SHORT, idx_buffer);
                    }
                    idx_buffer += pcmd->ElemCount;
                }
            }
#undef OFFSETOF

            // Restore modified state
            glDisableClientState(GL_COLOR_ARRAY);
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);
            glBindTexture(GL_TEXTURE_2D, last_texture);
            glMatrixMode(GL_MODELVIEW);
            glPopMatrix();
            glMatrixMode(GL_PROJECTION);
            glPopMatrix();
            glPopAttrib();
            glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
        }
    }

Hope this helps if you get this error.

Xaklse avatar Dec 15 '15 20:12 Xaklse

Hello. I'll take a look how it can be adopted :)

IhorSyerkov avatar Apr 27 '22 07:04 IhorSyerkov

Looks like you make you component work with my card. If so, and nothing else required from my side I can at lest add link to your project in README.md. Thanks 😄

IhorSyerkov avatar Apr 27 '22 07:04 IhorSyerkov

Yes I'm indeed using your card (wouldn't have spammed you otherwise :angel:).

The only thing that's not working is the up and down arrow but it should be simple enough for someone else to do that binding and raise a PR if they feel like it. All I was interested in was the sit and up positions so these 2 are working for sure and the current height is displayed correctly as well.

Good idea for the readme :)

maxime1992 avatar Apr 27 '22 16:04 maxime1992