PDCurses icon indicating copy to clipboard operation
PDCurses copied to clipboard

When resizing the SDL2 window, the surface isn't refreshed

Open bjadamson opened this issue 2 years ago • 0 comments

I believe I narrowed down a bug that I found using SDL 2.28.2. I found the bug present in both pdcurses release 3.9.0 and latest master commit. I'm unsure if it's present in other versions of SDL2. (I found this bug because whenever I resized my screen, my SDL callback was being spammed with messages about the SDL surface not being updated).

When I resize my SDL window, the surface is invalidated internally by SDL: https://github.com/libsdl-org/SDL/blob/a85cf62c1239081403bb6944b7c07b5aa560e005/src/video/SDL_video.c#L3064

I found out it isn't sufficient to update the surface myself in my resize routine, because of the way wgetch is implemented. This is because part of the work wgetch does in the SDL version is to call SDL_UpdateWindowSurface. However, this call fails because the surface hasn't been updated by the time the call to SDL_UpdateWindowSurface was made. https://github.com/wmcbrine/PDCurses/blob/5c62af03e9a05e3a3ae8c8354c1234b772dcf4b0/sdl2/pdcdisp.c#L575

I fixed this by refreshing the window surface whenever the window size changed event is detected.

if (SDL_WINDOWEVENT == event.type && SDL_WINDOWEVENT_SIZE_CHANGED == event.window.event)
{
  pdc_screen = SDL_GetWindowSurface(pdc_window);
}

Here's the full function with my fix:

void PDC_pump_and_peep(void)
{
    SDL_Event event;

    if (SDL_PollEvent(&event))
    {
        if (SDL_WINDOWEVENT == event.type && SDL_WINDOWEVENT_SIZE_CHANGED == event.window.event)
        {
          pdc_screen = SDL_GetWindowSurface(pdc_window);
        }
        if (SDL_WINDOWEVENT == event.type &&
            (SDL_WINDOWEVENT_RESTORED == event.window.event ||
             SDL_WINDOWEVENT_EXPOSED == event.window.event ||
             SDL_WINDOWEVENT_SHOWN == event.window.event))
        {
            SDL_UpdateWindowSurface(pdc_window);
            rectcount = 0;
        }
        else
            SDL_PushEvent(&event);
    }
}

With this change, my SDL error callback function is no longer called everytime I resize the window. Please do let me know your thoughts on this, thanks for your time.

bjadamson avatar Oct 18 '23 04:10 bjadamson