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);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」