If you are using a single thread, setting up an event watcher using SDL_AddEventWatch
is not enough. This is because SDL_PumpEvents
(which is called directly or indirectly by sdl) will not return until resizing is done, effectively blocking your thread.
To workaround this, you can setup a callback in your event watcher:
void Window::HandleWindowEvent(const SDL_WindowEvent e)
{
switch(e.event)
{
...
case SDL_WINDOWEVENT_MOVED:
if (_lastWindowMetrics.left != e.data1 || _lastWindowMetrics.top != e.data2)
{
_lastWindowMetrics.left = e.data1;
_lastWindowMetrics.top = e.data2;
_windowEvents.emplace_back(_lastWindowMetrics);
_refresh(); // user callback that may be used to repaint the window
}
break;
case SDL_WINDOWEVENT_RESIZED:
case SDL_WINDOWEVENT_SIZE_CHANGED:
if (_lastWindowMetrics.width != e.data1 || _lastWindowMetrics.height != e.data2)
{
_lastWindowMetrics.width = e.data1;
_lastWindowMetrics.height = e.data2;
_windowEvents.emplace_back(_lastWindowMetrics);
_refresh(); // user callback that may be used to repaint the window
}
break;
...
}
}
I use the watcher mechanism to handle all windows events and only use SDL_PumpEvents
and not SDL_PollEvents
.
This is quite similar to the mechanism offered by glfwSetWindowRefreshCallback
(if you are used to glfw).
SDL_WINDOWEVENT_SIZE_CHANGED
– PusherSDL_SetWindowsMessageHook
which will call supplied function on every Windows message, beforeTranslateMessage
. – Zinciferous