Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix window resize graphical glitches #1216

Open
DanRStevens opened this issue Feb 12, 2025 · 1 comment
Open

Fix window resize graphical glitches #1216

DanRStevens opened this issue Feb 12, 2025 · 1 comment

Comments

@DanRStevens
Copy link
Collaborator

There are graphical glitches when dragging to resize the window.

This can be illustrated with the test-graphics project. Hit F2 to enable window resizing, then drag the window borders. Dragging from the bottom produces the most extreme glitches, with lots of bouncing and flickering.

I suspect there is a delay between the resize event and when OpenGL render parameters get updated. I believe this leads to some frames being drawn with the old parameters, rather than the updated window dimensions.

Dragging either the bottom or top borders causes the image to bounce and flicker, and can leave an unpainted area along the bottom edge. Dragging from the left or right can leave an unpainted area along the right edge, though without any bounce.

OpenGL has an origin in the bottom left, whereas most UI drawing is done relative to the top left. This perhaps explains some of the asymmetry in the glitches. When dragging the bottom or top borders, there is a mismatch between OpenGL's bottom left draw origin, and the UI's top left origin. During that period of mismatch, things are drawn at a different vertical offset than normal, causing the up and down bouncing, and flickering.

A moving left side produces fewer glitches since drawing is expected to be relative to the left side, though it does leave an area with no painting along the right edge for a brief moment.

@DanRStevens
Copy link
Collaborator Author

DanRStevens commented Feb 26, 2025

We may have a slight issue with when we handle drawing versus polling for events.

From the SDL documentation:
https://wiki.libsdl.org/SDL2/SDL_PollEvent

The common practice is to fully process the event queue once every frame, usually as a first step before updating the game's state

Meanwhile we have this game loop in NAS2D, we we interleave drawing, dispatching events, and swapping the draw buffer:

void Game::go(State* state)
{
	// ...
	// Game Loop
	while (stateManager.update()) // Draw and dispatch events
	{
		Utility<Renderer>::get().update(); // Swap back buffer
	}
}
bool StateManager::update()
{
	if (mActiveState)
	{
		State* nextState = mActiveState->update(); // Draw to back buffer
		// ...
		Utility<EventHandler>::get().pump(); // Dispatch events, including resize
		// ...
void RendererOpenGL::update()
{
	SDL_GL_SwapWindow(underlyingWindow);
}

So contrary to the SDL recommendation, we do:

  1. Draw to the backbuffer
  2. Dispatch events (including resize)
  3. Swap frame buffers

This seems like an invitation to cause flickering for resize events.

With that said, moving the EventHandler::pump() call so it happens before the drawing doesn't solve the issue.


Saw this thread from a few months ago:
https://discourse.libsdl.org/t/text-flickering-on-window-resize-in-sdl2/54777

Kind of sounds like the resize flicker may have something to do with SDL rather than how we use it. It might also be a Linux SDL issue.

There's also a recent issue:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant