Window size not changing when leaving fullscreen

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.

Have you checked if SDL_SyncWindow returns true?

In my experience this frequently indicates the presence an uninitialized variable. A debugger will change what random value uninitialized memory will contain, sometimes in ways that are consistent from run to run, whereas outside the debugger it will be unpredictable.

Your sample code doesn’t show where input.fullscreen or state->isFullscreen gets set, for example, so those might be a good place to start looking.

Another common issue: try a search of your code for any other place where your code might be setting a window size.

The SDL_EVENT_WINDOW_RESIZED, and SDL_EVENT_WINDOW_LEAVE_FULLSCREEN event handlers are likely spots, but just do a general search for the SDL_SetWindowSize function in all of your project files.

Yes. I should have mentioned it in the post, but I also tried asserting the SDL_SyncWindow calls and it did not stop the program.

You might want to try moving the code that follows the SDL_SetWindowFullscreen call and instead run it when receiving a SDL_EVENT_WINDOW_LEAVE_FULLSCREEN event. You could also move the SDL_GetWindowSurface code and run it in response to a SDL_EVENT_WINDOW_RESIZED event. This way you can get rid of all the SDL_SyncWindow calls. Maybe this works better, I don’t know…

// in response to the key press...
SDL_SetWindowFullscreen(window, false);

// when receiving a SDL_EVENT_WINDOW_LEAVE_FULLSCREEN event...
SDL_SetWindowSize(window, displayDefaultW_px, displayDefaultH_px);
state->isFullscreen = false;

// when receiving a SDL_EVENT_WINDOW_RESIZED event...
SDL_Surface* surface = SDL_GetWindowSurface(window);
state->displayW_px = surface->w;
state->displayH_px = surface->h;

Normally I don’t think it should be necessary to call SDL_SetWindowSize when leaving fullscreen mode because I would expect SDL_SetWindowFullscreen(window, false); to restore the previous size that was used before entering fullscreen mode (or that you specified when calling SDL_CreateWindow if windowed mode has never been used). If this doesn’t happen when leaving out the SDL_SetWindowSize call then you might want to consider reporting it as a bug.

And another thing… The docs for SDL_GetWindowSurface says:

You may not combine this with 3D or the rendering API on this window.

Since you use SDL_WINDOW_VULKAN I thought you might be using the render, gpu or vulkan api for rendering. Maybe that could cause some problems. Just a thought…

Anyway, you can’t set the format of a surface just like that:

surface->format = SDL_PIXELFORMAT_RGB24;

Note how the format comment in the SDL_Surface docs says “read-only”.