Skip to content

Commit

Permalink
Flush old size, move, and exposure events from the queue
Browse files Browse the repository at this point in the history
SDL2 removed old size, move, and exposure events from the queue to prevent the queue from overflowing, and a side effect was that this prevented clients from operating on old size and position data. Replicate this behavior in sdl2-compat.
  • Loading branch information
Kontrabant committed Oct 16, 2024
1 parent a305041 commit 00a41c4
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/sdl2_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,48 @@ WindowEventType3To2(Uint32 event_type3)
}
}


typedef struct RemovePendingSizeChangedAndResizedEvents_Data
{
const SDL2_Event *new_event;
bool saw_resized;
} RemovePendingSizeChangedAndResizedEvents_Data;

static int SDLCALL
RemovePendingSizeChangedAndResizedEvents(void *userdata, SDL2_Event *event)
{
RemovePendingSizeChangedAndResizedEvents_Data *resizedata = (RemovePendingSizeChangedAndResizedEvents_Data *)userdata;
const SDL2_Event *new_event = resizedata->new_event;

if (event->type == SDL2_WINDOWEVENT &&
(event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
event->window.event == SDL_WINDOWEVENT_RESIZED) &&
event->window.windowID == new_event->window.windowID) {

if (event->window.event == SDL_WINDOWEVENT_RESIZED) {
resizedata->saw_resized = true;
}

/* We're about to post a new size event, drop the old one */
return 0;
}
return 1;
}

static int SDLCALL
RemoveSupercededWindowEvents(void *userdata, SDL2_Event *event)
{
SDL2_Event *new_event = (SDL2_Event *)userdata;

if (event->type == SDL2_WINDOWEVENT &&
event->window.type == new_event->window.type &&
event->window.windowID == new_event->window.windowID) {
/* We're about to post a new move event, drop the old one */
return 0;
}
return 1;
}

static bool SDLCALL
EventFilter3to2(void *userdata, SDL_Event *event3)
{
Expand Down Expand Up @@ -1647,6 +1689,24 @@ EventFilter3to2(void *userdata, SDL_Event *event3)
event2.window.padding3 = 0;
event2.window.data1 = event3->window.data1;
event2.window.data2 = event3->window.data2;

/* Fixes queue overflow with resize events that aren't processed */
if (event2.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
RemovePendingSizeChangedAndResizedEvents_Data resizedata = {
&event2,
false
};
SDL_FilterEvents(RemovePendingSizeChangedAndResizedEvents, &resizedata);
if (resizedata.saw_resized) { /* if there was a pending resize, make sure one at the new dimensions remains. */
event2.window.event = SDL_WINDOWEVENT_RESIZED;
SDL_PushEvent(&event2);
event2.window.event = SDL_WINDOWEVENT_SIZE_CHANGED; /* then push the actual event next. */
}
} else if (event2.window.event == SDL_WINDOWEVENT_MOVED || event2.window.event == SDL_WINDOWEVENT_EXPOSED) {
/* Flush old move and exposure events */
SDL_FilterEvents(RemoveSupercededWindowEvents, &event2);
}

SDL_PushEvent(&event2);
}
break;
Expand Down Expand Up @@ -6840,6 +6900,8 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)

CheckEventFilter();

SDL_Log("Creating SDL3 Window");

if (flags & SDL2_WINDOW_FULLSCREEN_DESKTOP) {
flags &= ~SDL2_WINDOW_FULLSCREEN_DESKTOP;
flags |= SDL_WINDOW_FULLSCREEN; /* This is fullscreen desktop for new windows */
Expand Down Expand Up @@ -6991,6 +7053,7 @@ SDL_SetWindowIcon(SDL_Window *window, SDL2_Surface *icon)
SDL_DECLSPEC void SDLCALL
SDL_SetWindowSize(SDL_Window *window, int w, int h)
{
SDL_Log("SetWindowSize: %ix%i", w, h);
SDL3_SetWindowSize(window, w, h);
}

Expand Down

0 comments on commit 00a41c4

Please sign in to comment.