SDL_Resetwindowsize

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);
    }
 }

posted on   zxddesk  阅读(34)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示