How to properly refresh SDL2 window after resizing

I wrote a small SDL2 application that reacts to resizing by rearranging its content. It works pretty well on Windows 10, but not on my Linux machine.

Here's an MCVE that (hopefully) reproduces the problem:

#include <SDL2/SDL.h>

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *w = SDL_CreateWindow("MCVE",
            SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 250, 250,
            SDL_WINDOW_ALLOW_HIGHDPI|SDL_WINDOW_RESIZABLE);

    SDL_Renderer *r = SDL_CreateRenderer(w, -1, SDL_RENDERER_ACCELERATED);

    SDL_Surface *shape = SDL_CreateRGBSurface(0, 500, 500, 32, 0, 0, 0, 0);
    SDL_Rect rect = { .x = 100, .y = 100, .h = 300, .w = 300 };
    SDL_FillRect(shape, &rect, SDL_MapRGB(shape->format, 0xff, 0, 0));
    SDL_Texture *t = SDL_CreateTextureFromSurface(r, shape);
    SDL_RenderClear(r);
    SDL_RenderCopy(r, t, 0, 0);
    SDL_RenderPresent(r);

    SDL_Event ev;
    while (SDL_WaitEvent(&ev))
    {
        switch (ev.type)
        {
        case SDL_QUIT:
            SDL_Quit();
            return 0;
        case SDL_WINDOWEVENT:
            if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
            {
                SDL_RenderClear(r);
                SDL_RenderCopy(r, t, 0, 0);
                SDL_RenderPresent(r);
            }
        }
    }
    return 1;
}

      

compile command:

gcc -std=c11 -Wall -Wextra -Wno-unused-parameter -pedantic -osdlresize sdlresize.c -lSDL2main -lSDL2

      

Note that any error checking and cleanup has been omitted for brevity only, real code for reference .

What I get on my Linux machine when the window is resized looks like this:

screenshot of resized window

If you don't see the image: content is not updating properly, it shows an area on the right and bottom with the content of what is below my desktop window.

Is there something wrong with my processing SDL_WINDOWEVENT_SIZE_CHANGED

? The same thing happens if I process SDL_WINDOWEVENT_RESIZED

instead ...

+3


source to share





All Articles