Use the convenience function SDL_GetWindowFromEvent()

This commit is contained in:
Sam Lantinga 2024-08-02 20:36:35 -07:00
parent 8d748d64e8
commit f7c8d66ccb
3 changed files with 27 additions and 27 deletions

View file

@ -1620,7 +1620,7 @@ static void SDLTest_PrintEvent(const SDL_Event *event)
case SDL_EVENT_WINDOW_SAFE_AREA_CHANGED: { case SDL_EVENT_WINDOW_SAFE_AREA_CHANGED: {
SDL_Rect rect; SDL_Rect rect;
SDL_GetWindowSafeArea(SDL_GetWindowFromID(event->window.windowID), &rect); SDL_GetWindowSafeArea(SDL_GetWindowFromEvent(event), &rect);
SDL_Log("SDL EVENT: Window %" SDL_PRIu32 " changed safe area to: %d,%d %dx%d\n", SDL_Log("SDL EVENT: Window %" SDL_PRIu32 " changed safe area to: %d,%d %dx%d\n",
event->window.windowID, rect.x, rect.y, rect.w, rect.h); event->window.windowID, rect.x, rect.y, rect.w, rect.h);
break; break;
@ -1661,7 +1661,7 @@ static void SDLTest_PrintEvent(const SDL_Event *event)
SDL_Log("SDL EVENT: Window %" SDL_PRIu32 " display changed to %" SDL_PRIs32, event->window.windowID, event->window.data1); SDL_Log("SDL EVENT: Window %" SDL_PRIu32 " display changed to %" SDL_PRIs32, event->window.windowID, event->window.data1);
break; break;
case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED: case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
SDL_Log("SDL EVENT: Window %" SDL_PRIu32 " display scale changed to %d%%", event->window.windowID, (int)(SDL_GetWindowDisplayScale(SDL_GetWindowFromID(event->window.windowID)) * 100.0f)); SDL_Log("SDL EVENT: Window %" SDL_PRIu32 " display scale changed to %d%%", event->window.windowID, (int)(SDL_GetWindowDisplayScale(SDL_GetWindowFromEvent(event)) * 100.0f));
break; break;
case SDL_EVENT_WINDOW_OCCLUDED: case SDL_EVENT_WINDOW_OCCLUDED:
SDL_Log("SDL EVENT: Window %" SDL_PRIu32 " occluded", event->window.windowID); SDL_Log("SDL EVENT: Window %" SDL_PRIu32 " occluded", event->window.windowID);
@ -2089,7 +2089,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
switch (event->type) { switch (event->type) {
case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED: case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
if (state->auto_scale_content) { if (state->auto_scale_content) {
SDL_Window *window = SDL_GetWindowFromID(event->window.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
float scale = SDL_GetDisplayContentScale(SDL_GetDisplayForWindow(window)); float scale = SDL_GetDisplayContentScale(SDL_GetDisplayForWindow(window));
int w = state->window_w; int w = state->window_w;
@ -2103,7 +2103,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
break; break;
case SDL_EVENT_WINDOW_FOCUS_LOST: case SDL_EVENT_WINDOW_FOCUS_LOST:
if (state->flash_on_focus_loss) { if (state->flash_on_focus_loss) {
SDL_Window *window = SDL_GetWindowFromID(event->window.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
SDL_FlashWindow(window, SDL_FLASH_UNTIL_FOCUSED); SDL_FlashWindow(window, SDL_FLASH_UNTIL_FOCUSED);
} }
@ -2111,7 +2111,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
break; break;
case SDL_EVENT_WINDOW_CLOSE_REQUESTED: case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
{ {
SDL_Window *window = SDL_GetWindowFromID(event->window.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
SDL_HideWindow(window); SDL_HideWindow(window);
} }
@ -2127,7 +2127,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
/* Add hotkeys here */ /* Add hotkeys here */
case SDLK_PRINTSCREEN: case SDLK_PRINTSCREEN:
{ {
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
for (i = 0; i < state->num_windows; ++i) { for (i = 0; i < state->num_windows; ++i) {
if (window == state->windows[i]) { if (window == state->windows[i]) {
@ -2139,7 +2139,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_EQUALS: case SDLK_EQUALS:
if (withControl) { if (withControl) {
/* Ctrl-+ double the size of the window */ /* Ctrl-+ double the size of the window */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
int w, h; int w, h;
SDL_GetWindowSize(window, &w, &h); SDL_GetWindowSize(window, &w, &h);
@ -2150,7 +2150,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_MINUS: case SDLK_MINUS:
if (withControl) { if (withControl) {
/* Ctrl-- half the size of the window */ /* Ctrl-- half the size of the window */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
int w, h; int w, h;
SDL_GetWindowSize(window, &w, &h); SDL_GetWindowSize(window, &w, &h);
@ -2164,7 +2164,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_RIGHT: case SDLK_RIGHT:
if (withAlt) { if (withAlt) {
/* Alt-Up/Down/Left/Right switches between displays */ /* Alt-Up/Down/Left/Right switches between displays */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
int num_displays; int num_displays;
const SDL_DisplayID *displays = SDL_GetDisplays(&num_displays); const SDL_DisplayID *displays = SDL_GetDisplays(&num_displays);
@ -2195,7 +2195,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
} }
if (withShift) { if (withShift) {
/* Shift-Up/Down/Left/Right shift the window by 100px */ /* Shift-Up/Down/Left/Right shift the window by 100px */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
const int delta = 100; const int delta = 100;
int x, y; int x, y;
@ -2222,7 +2222,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_O: case SDLK_O:
if (withControl) { if (withControl) {
/* Ctrl-O (or Ctrl-Shift-O) changes window opacity. */ /* Ctrl-O (or Ctrl-Shift-O) changes window opacity. */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
float opacity = SDL_GetWindowOpacity(window); float opacity = SDL_GetWindowOpacity(window);
if (withShift) { if (withShift) {
@ -2253,7 +2253,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
} else if (withControl) { } else if (withControl) {
if (withShift) { if (withShift) {
/* Ctrl-Shift-C copy screenshot! */ /* Ctrl-Shift-C copy screenshot! */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
for (i = 0; i < state->num_windows; ++i) { for (i = 0; i < state->num_windows; ++i) {
if (window == state->windows[i]) { if (window == state->windows[i]) {
@ -2299,7 +2299,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_F: case SDLK_F:
if (withControl) { if (withControl) {
/* Ctrl-F flash the window */ /* Ctrl-F flash the window */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
SDL_FlashWindow(window, SDL_FLASH_BRIEFLY); SDL_FlashWindow(window, SDL_FLASH_BRIEFLY);
} }
@ -2308,7 +2308,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_G: case SDLK_G:
if (withControl) { if (withControl) {
/* Ctrl-G toggle mouse grab */ /* Ctrl-G toggle mouse grab */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
SDL_SetWindowMouseGrab(window, !SDL_GetWindowMouseGrab(window)); SDL_SetWindowMouseGrab(window, !SDL_GetWindowMouseGrab(window));
} }
@ -2317,7 +2317,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_K: case SDLK_K:
if (withControl) { if (withControl) {
/* Ctrl-K toggle keyboard grab */ /* Ctrl-K toggle keyboard grab */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
SDL_SetWindowKeyboardGrab(window, !SDL_GetWindowKeyboardGrab(window)); SDL_SetWindowKeyboardGrab(window, !SDL_GetWindowKeyboardGrab(window));
} }
@ -2326,7 +2326,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_M: case SDLK_M:
if (withControl) { if (withControl) {
/* Ctrl-M maximize */ /* Ctrl-M maximize */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
SDL_WindowFlags flags = SDL_GetWindowFlags(window); SDL_WindowFlags flags = SDL_GetWindowFlags(window);
if (flags & SDL_WINDOW_MAXIMIZED) { if (flags & SDL_WINDOW_MAXIMIZED) {
@ -2354,7 +2354,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_T: case SDLK_T:
if (withControl) { if (withControl) {
/* Ctrl-T toggle topmost mode */ /* Ctrl-T toggle topmost mode */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
SDL_WindowFlags flags = SDL_GetWindowFlags(window); SDL_WindowFlags flags = SDL_GetWindowFlags(window);
if (flags & SDL_WINDOW_ALWAYS_ON_TOP) { if (flags & SDL_WINDOW_ALWAYS_ON_TOP) {
@ -2368,7 +2368,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_Z: case SDLK_Z:
if (withControl) { if (withControl) {
/* Ctrl-Z minimize */ /* Ctrl-Z minimize */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
SDL_MinimizeWindow(window); SDL_MinimizeWindow(window);
} }
@ -2377,7 +2377,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_RETURN: case SDLK_RETURN:
if (withControl) { if (withControl) {
/* Ctrl-Enter toggle fullscreen */ /* Ctrl-Enter toggle fullscreen */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
SDL_WindowFlags flags = SDL_GetWindowFlags(window); SDL_WindowFlags flags = SDL_GetWindowFlags(window);
if (!(flags & SDL_WINDOW_FULLSCREEN) || if (!(flags & SDL_WINDOW_FULLSCREEN) ||
@ -2390,7 +2390,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
} }
} else if (withAlt) { } else if (withAlt) {
/* Alt-Enter toggle fullscreen desktop */ /* Alt-Enter toggle fullscreen desktop */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
SDL_WindowFlags flags = SDL_GetWindowFlags(window); SDL_WindowFlags flags = SDL_GetWindowFlags(window);
if (!(flags & SDL_WINDOW_FULLSCREEN) || if (!(flags & SDL_WINDOW_FULLSCREEN) ||
@ -2407,7 +2407,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_B: case SDLK_B:
if (withControl) { if (withControl) {
/* Ctrl-B toggle window border */ /* Ctrl-B toggle window border */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
const SDL_WindowFlags flags = SDL_GetWindowFlags(window); const SDL_WindowFlags flags = SDL_GetWindowFlags(window);
const SDL_bool b = (flags & SDL_WINDOW_BORDERLESS) ? SDL_TRUE : SDL_FALSE; const SDL_bool b = (flags & SDL_WINDOW_BORDERLESS) ? SDL_TRUE : SDL_FALSE;
@ -2418,7 +2418,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_A: case SDLK_A:
if (withControl) { if (withControl) {
/* Ctrl-A toggle aspect ratio */ /* Ctrl-A toggle aspect ratio */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
if (window) { if (window) {
float min_aspect = 0.0f, max_aspect = 0.0f; float min_aspect = 0.0f, max_aspect = 0.0f;
@ -2436,7 +2436,7 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
break; break;
case SDLK_0: case SDLK_0:
if (withControl) { if (withControl) {
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID); SDL_Window *window = SDL_GetWindowFromEvent(event);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Test Message", "You're awesome!", window); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Test Message", "You're awesome!", window);
} }
break; break;

View file

@ -408,7 +408,7 @@ static void loop(void)
break; break;
case SDL_EVENT_FINGER_DOWN: case SDL_EVENT_FINGER_DOWN:
{ {
SDL_Window *window = SDL_GetWindowFromID(event.tfinger.windowID); SDL_Window *window = SDL_GetWindowFromEvent(&event);
if (SDL_TextInputActive(window)) { if (SDL_TextInputActive(window)) {
SDL_Log("Stopping text input for window %" SDL_PRIu32 "\n", event.tfinger.windowID); SDL_Log("Stopping text input for window %" SDL_PRIu32 "\n", event.tfinger.windowID);
SDL_StopTextInput(window); SDL_StopTextInput(window);
@ -420,7 +420,7 @@ static void loop(void)
} }
case SDL_EVENT_MOUSE_BUTTON_DOWN: case SDL_EVENT_MOUSE_BUTTON_DOWN:
if (event.button.button == SDL_BUTTON_RIGHT) { if (event.button.button == SDL_BUTTON_RIGHT) {
SDL_Window *window = SDL_GetWindowFromID(event.button.windowID); SDL_Window *window = SDL_GetWindowFromEvent(&event);
if (SDL_TextInputActive(window)) { if (SDL_TextInputActive(window)) {
SDL_Log("Stopping text input for window %" SDL_PRIu32 "\n", event.button.windowID); SDL_Log("Stopping text input for window %" SDL_PRIu32 "\n", event.button.windowID);
SDL_StopTextInput(window); SDL_StopTextInput(window);

View file

@ -167,7 +167,7 @@ static void loop(void)
SDLTest_CommonEvent(state, &event, &done); SDLTest_CommonEvent(state, &event, &done);
if (event.type == SDL_EVENT_WINDOW_RESIZED) { if (event.type == SDL_EVENT_WINDOW_RESIZED) {
SDL_Window *window = SDL_GetWindowFromID(event.window.windowID); SDL_Window *window = SDL_GetWindowFromEvent(&event);
if (window) { if (window) {
SDL_Log("Window %" SDL_PRIu32 " resized to %" SDL_PRIs32 "x%" SDL_PRIs32 "\n", SDL_Log("Window %" SDL_PRIu32 " resized to %" SDL_PRIs32 "x%" SDL_PRIs32 "\n",
event.window.windowID, event.window.windowID,
@ -176,7 +176,7 @@ static void loop(void)
} }
} }
if (event.type == SDL_EVENT_WINDOW_MOVED) { if (event.type == SDL_EVENT_WINDOW_MOVED) {
SDL_Window *window = SDL_GetWindowFromID(event.window.windowID); SDL_Window *window = SDL_GetWindowFromEvent(&event);
if (window) { if (window) {
SDL_Log("Window %" SDL_PRIu32 " moved to %" SDL_PRIs32 ",%" SDL_PRIs32 " (display %s)\n", SDL_Log("Window %" SDL_PRIu32 " moved to %" SDL_PRIs32 ",%" SDL_PRIs32 " (display %s)\n",
event.window.windowID, event.window.windowID,