SDL_Resetwindowsize

发布时间 2023-05-26 15:45:26作者: zxddesk

So, I was partially correct about the bug. The window.x and window.y do get set to zero, but normally when a window is unfullscreened, the window manager (at least I think the window manager is responsible for this) sends a ConfigureNotify event to move it to where it was beforehand (or wherever the window manager wants to put it currently).

However, SDL is given no chance to process the ConfigureNotify between the SDL SetWindowFullScreen call and the SetWindowSize call. The SetWindowSize call will then perform what should be a no-op move to where SDL thinks the window already is, in an attempt to to encourage the WM to properly update the size of the window (there is more explanation here). However, this move is not a no-op, because SDL does not have up to date information on what the position of the window is, and ends up overriding the WMs move event.

If you explicitly pump and flush the events, it will work as expected:
(F1 for fullscreen, F2 for flushing events to get the new ConfigureNotify from the wm, F3 for the broken code)

#include <SDL2/SDL.h>

int main(int argc, char **argv) {
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    SDL_Event e;
    while (1) {
        while (SDL_PollEvent(&e) != 0) {
            if (e.type == SDL_QUIT) {
                return 0;
            }
            if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_F1) {
                SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
            }
            if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_F2) {
                SDL_SetWindowFullscreen(window, 0);
                SDL_Delay(50); /* unsure if this is neccesary, but just to be sure that the WM is given enough time to send a configure event */
                SDL_PumpEvents();
                SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
                SDL_SetWindowSize(window, 800, 600);
            }
            if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_F3) {
                SDL_SetWindowFullscreen(window, 0);
                SDL_SetWindowSize(window, 800, 600)
            }
        }
        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }
 }