My program initially starts in windowed at an arbitrary resolution of 1920 x 1080, and I want it to swap between windowed and fullscreen with a button press. When it goes into fullscreen, it behaves as expected, but when leaving fullscreen I get very strange behavior that changes depending on if I am running the debugger or not.
While running the program on its own, the window will leave fullscreen but remain at my monitor’s size of 2560 x 1600. However, if the program is running in the debugger and I am stepping through each line individually, it behaves as expected.
This is the code for initializing the SDL_Window used for the program display.
// Display init //
const char8 windowTitle[] = "Game Window";
const uint32 displayDefaultW_px = 1920;
const uint32 displayDefaultH_px = 1080;
state->displayScale = 1080.0;
state->displayW_px = displayDefaultW_px;
state->displayH_px = displayDefaultH_px;
/* Initialize internal struct that will hold the first display frame data */
SDL_Window* window = SDL_CreateWindow(windowTitle, state->displayW_px, state->displayH_px, SDL_WINDOW_VULKAN);
SDL_SetWindowTitle(window, windowTitle);
SDL_Surface* surface = SDL_GetWindowSurface(window);
surface->format = SDL_PIXELFORMAT_RGB24;
This is the code in the main loop to handle changing fullscreen status. It runs after collecting all user input from SDL and before any other functions like updating program logic and sending video/audio data to SDL. The if block handles going from windowed to fullscreen, and works correctly; the window becomes fullscreen and the display in the window fills up the newly acquired space. The else if block handles the reverse case of going back to windowed, and is where the problems happen.
if (input.fullscreen == pressed && !state->isFullscreen) {
SDL_SetWindowFullscreen(window, true);
SDL_SyncWindow(window);
surface = SDL_GetWindowSurface(window);
state->displayW_px = surface->w;
state->displayH_px = surface->h;
state->isFullscreen = true;
}
else if (input.fullscreen == pressed && state->isFullscreen) {
SDL_SetWindowFullscreen(window, false);
SDL_SyncWindow(window);
SDL_SetWindowSize(window, displayDefaultW_px, displayDefaultH_px);
SDL_SyncWindow(window);
surface = SDL_GetWindowSurface(window);
state->displayW_px = surface->w;
state->displayH_px = surface->h;
state->isFullscreen = false;
}
I’m not sure why this doesn’t work as expected and even more confused as to why it works just fine when stepping through the debugger. My first thought was that there was some sort of timing/parallelism issue, but I figured that SDL_SyncWindow should have prevented those.