SDL: SDL has unexpected behavior when switching window sizes (X11/Wayland)
(using SDL2) Consider the source at the bottom of this post. It should switch between maximized and windowed mode every second while also changing the window draw color. I expect the log to be the following:
INFO: Window Size : width: 120, height: 200
INFO: Maximized Size : width: 1920, height: 1048
INFO: Window Size : width: 120, height: 200
INFO: Maximized Size : width: 1920, height: 1048
INFO: Window Size : width: 120, height: 200
INFO: Maximized Size : width: 1920, height: 1048
INFO: Window Size : width: 120, height: 200
INFO: Maximized Size : width: 1920, height: 1048
...
Instead, it is the following (for X11):
INFO: Window Size : width: 120, height: 200
INFO: Maximized Size : width: 128, height: 200
INFO: Window Size : width: 1920, height: 1048
INFO: Maximized Size : width: 1920, height: 1048
INFO: Window Size : width: 1920, height: 1048
INFO: Maximized Size : width: 128, height: 200
INFO: Window Size : width: 1920, height: 1048
INFO: Maximized Size : width: 1920, height: 1048
INFO: Window Size : width: 1920, height: 1048
INFO: Maximized Size : width: 128, height: 200
INFO: Window Size : width: 1920, height: 1048
INFO: Maximized Size : width: 1920, height: 1048
INFO: Window Size : width: 1920, height: 1048
INFO: Maximized Size : width: 128, height: 200
...
For wayland, the startup logs are slightly different, but it ends in the same steady state.
INFO: Window Size : width: 120, height: 200
INFO: Maximized Size : width: 120, height: 200
INFO: Window Size : width: 1920, height: 1048
INFO: Maximized Size : width: 120, height: 200
INFO: Window Size : width: 1920, height: 1048
INFO: Maximized Size : width: 1920, height: 1048
INFO: Window Size : width: 1920, height: 1048
INFO: Maximized Size : width: 120, height: 200
...
Sprinkling more calls to handle_events in the main loop does not solve the issue. In contrary, it makes the behavior more erratic.
source:
#include "SDL.h"
#include <stdio.h>
static void SetNextColor(SDL_Renderer *renderer) {
static int c = 0;
SDL_SetRenderDrawColor(renderer, (c&0x1)*0xff, (c&0x2)*0xff, (c&0x4)*0xff, 0xff);
c += 1;
}
static int handle_events(void) {
int res = 0;
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
res = 1;
break;
default:
break;
}
}
return 0;
}
int main(int argc, char *argv[]) {
SDL_assert(SDL_Init(SDL_INIT_VIDEO) == 0);
SDL_Window *window = SDL_CreateWindow("title", 0, 0, 120, 200, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS);
SDL_assert(window != NULL);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_assert(renderer != NULL);
int w=0;
int h=0;
while (1) {
SetNextColor(renderer);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_GetWindowSize(window, &w, &h);
SDL_Log("Window Size : width: %d, height: %d", w, h);
SDL_Delay(1000);
SDL_MaximizeWindow(window);
if (handle_events()) {break;}
SetNextColor(renderer);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_GetWindowSize(window, &w, &h);
SDL_Log("Maximized Size : width: %d, height: %d", w, h);
SDL_Delay(1000);
SDL_RestoreWindow(window);
if (handle_events()) {break;}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 21 (14 by maintainers)
Commits related to this issue
- x11: Attempt to wait for SDL_MaximizeWindow to complete before returning. Fixes #7070. — committed to libsdl-org/SDL by icculus a year ago
- Squashed commit of the following: commit 433911307237afaec03e2dcb976f4dd1efb62782 Author: Sylvain <sylvain.becker@gmail.com> Date: Thu Jun 22 10:48:12 2023 +0200 SDL_DBus_AppendDictWithKeyValu... — committed to 1bsyl/SDL by 1bsyl a year ago
- Squashed commit of the following: commit c2350b23720e4e33aab926b37fd3229ec8e3d846 Author: Sylvain <sylvain.becker@gmail.com> Date: Thu Jun 22 18:22:54 2023 +0200 Revert "Squashed commit of the... — committed to 1bsyl/SDL by 1bsyl a year ago
- x11: Blocking for window maximization should wait 100 ms, not 1000. Reference Issue #7070. — committed to libsdl-org/SDL by icculus a year ago
- x11: Blocking for window maximization should wait 100 ms, not 1000. Reference Issue #7070. (cherry picked from commit 3030fd815c4d05b5286f7cb9c8a8a2b42cff7bb1) — committed to libsdl-org/SDL by icculus a year ago
- x11: Attempt to wait for SDL_MaximizeWindow to complete before returning. Fixes #7070. (cherry picked from commit 379a6f4dabc448d37a5823724d507967e2e069eb) — committed to sulix/SDL by icculus a year ago
Hmm…my comment up there says “100 milliseconds” but I definitely committed it as 1000.
We should probably drop that to 100. A full second seems like too much.