Delay in SDL_QUIT event being processed

I’m using SDL 2.0.3 to create OpenGL contextx and I noticed that if I resize the window a couple of times and then press X to exit, it takes a significant time for the window to actually close. I created a new project from scratch and wrote a simple application but the problem persists. Here is my code:

Code:
#include <GL\glew.h>

#include <SDL.h>
#include <SDL_opengl.h>

#include

int main(int argc, char *argv[]) {

int width = 600, height = 600;

SDL_Init(SDL_INIT_VIDEO);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

SDL_Window *window;
window = SDL_CreateWindow("OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);

SDL_GLContext context;
context = SDL_GL_CreateContext(window);

SDL_GL_SetSwapInterval(1);

glewExperimental = GL_TRUE;
glewInit();

SDL_Event main_event;
SDL_PollEvent(&main_event);

while (main_event.type != SDL_QUIT) {
	SDL_PollEvent(&main_event);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	SDL_GL_SwapWindow(window);
}

return 0;

}

Any idea why this might be happening? Thanks!

It seems like it delays all events and not just SDL_QUIT. It also only seems to happen if I resize to make the window larger. I added
Code:
std::cout << main_event.type << std::endl;

in the main loop to test this.

You’re only processing a single event in each loop before you call
SDL_GL_SWapWindow.

Since you have the swap interval set to 1, this will block for up to
16ms until the next vblank interval before returning.

You need to change your code so you process all events until
SDL_PollEvent returns 0, and then call SDL_GL_SwapWindow after that.

Something along the lines of:

while(1){
while(SDL_PollEvent(&main_event)){
if(main_event.type == SDL_QUIT){
return 0;
}
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(window);

}On 04/06/14 20:11, Sythical wrote:

It seems like it delays all events and not just SDL_QUIT. It also only seems to happen if I resize to make the window larger. I added
Code:
std::cout << main_event.type << std::endl;

in the main loop to test this.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thank you, that fixed the issue. But even then, why does resizing the window floods the event queue?

From a small test, it looks like there are at least 4 events that are
generated when the window is resized:

SDL_WINDOWEVENT_MOVED,
SDL_WINDOWEVENT_SIZE_CHANGED,
SDL_WINDOWEVENT_RESIZED,
SDL_WINDOWEVENT_EXPOSED

  • some more if the keyboard / mouse focus changes e.t.c.

So that’s probably why. It shouldn’t be anything to worry about if
you’re now polling all the events per frame though.On 04/06/14 21:14, Sythical wrote:

Thank you, that fixed the issue. But even then, why does resizing the window floods the event queue?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org